微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于组geom_rect 或 geom_mark_rect的 g​​gplot 背景阴影?

如何解决基于组geom_rect 或 geom_mark_rect的 g​​gplot 背景阴影?

我想根据排名的分组因素(强、中、弱)为我的 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"))

剧情:

Plot created with the above code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。