问题描述
我想根据排名的分组因素(强、中、弱)为我的 ggplot 的背景着色。我使用过 geom_mark_rect() 但如果有其他方法会很棒。我假设另一种选择是使用重要性值()来为背景着色,而不是使用 geom_mark_rect()。
我看了以下答案: Make the background of a graph different colours in different regions
dput(数据)
structure(list(Category = c("Place","Place","Place"),Ranking =
c("Strong","Strong","Moderate","Weak","Weak"),Features = c("F2_Pre","F3_Pre","F1_Pre","F2_Fol","f0_Pre","f0_Fol","CPP_Pre","F1_Fol","SD","Release","F3_Fol","CPP_Fol","CoG","H1*-H2*_Pre","H1*-H2*_Fol","Closure"),Importance = c(24,12,11,8,7,6,5,3,2,1,0)),class =
c("spec_tbl_df","tbl_df","tbl","data.frame"),row.names = c(NA,-16L),spec =
structure(list(cols = list(Category = structure(list(),class =
c("collector_character","collector")),Ranking =
structure(list(),class = c("collector_character",Features = structure(list(),Importance = structure(list(),class =
c("collector_double","collector"))),default = structure(list(),class =
c("collector_guess",skip = 1L),class = "col_spec"))
代码如下:
library(ggplot2)
library(tidyverse)
library(ggforce)
ggplot(data,aes(x = reorder(Features,-Importance),y = Importance,fill = Importance)) +
geom_bar(stat = "identity",width=0.9,position = position_dodge(width=0.1)) +
geom_mark_rect(aes(fill = Importance,group = "fill",label=Ranking,filter = Ranking == c("Strong")),label.fontsize = 8,label.buffer = unit(10,'mm'),label.hjust = 8,show.legend = FALSE)+
geom_mark_rect(aes(fill = Importance,filter = Ranking == c("Moderate")),filter = Ranking == c("Weak")),show.legend = FALSE)+
xlab("Acoustic Features")+
ylab("Importance")+
coord_cartesian(ylim=c(0,40)) +
theme_bw() +
theme(axis.title.y = element_text(size = rel(1.0),color="black",vjust = 0.4,family= "Arial"))+
theme(axis.title.x = element_text(size = rel(1.0),family= "Arial"))+
theme(axis.text.y = element_text(size = rel(1.0),family= "Arial"))+
theme(axis.text.x = element_text(size = rel(1.0),vjust = 0.6,angle=45,family= "Arial"))
剧情:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)