R语言 分组并填充geom_col

vyswwuz2  于 2022-12-25  发布在  其他
关注(0)|答案(3)|浏览(256)

我需要做这个情节

我的理解是,我需要对模型名称name(base与mother)使用fill,对Year使用group。
我试过了

test%>%ggplot(aes(as_factor(sector), value, fill=name, group=factor(YEAR)))+
  geom_col(position = "dodge", width=0.5)

但它不起作用...

如何解决这个问题?:(
我的数据如下所示

structure(list(value = c(45.7835023085923, 46.727175387221, 47.6992761579977, 
48.0597275867616, 50.7882757046681, 50.8768402521772, 42.7273124207896, 
43.9851413648616, 47.5599896653421, 47.8361505231604, 51.0693121296854, 
51.2675797116211, 45.0282059530599, 46.0840505213407), name = c("value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total", "value_add_base_sector_total", 
"value_add_mother_sector_total"), YEAR = c(2011, 2011, 
2011, 2011, 2011, 2011, 2016, 2016, 2016, 2016, 2016, 2016, 2021, 
2021), sector = c("Catholic", "Catholic", "Government", "Government", 
"Independent", "Independent", "Catholic", "Catholic", "Government", 
"Government", "Independent", "Independent", "Catholic", "Catholic"
)), row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"
4ngedf3f

4ngedf3f1#

像这样?

ggplot(data = mydata, aes(x = as.factor(YEAR), y = value, fill = as.factor(YEAR))) + 
  geom_col() + 
  facet_wrap(~sector, nrow = 1, strip.position = "bottom")

ulydmbyx

ulydmbyx2#

你可以

ggplot(df, aes(factor(YEAR), value, fill = name)) +
  geom_col(position = position_dodge(width = 0.75), width = 0.5) +
  facet_grid(.~sector, switch = 'x') +
  scale_fill_manual(NULL, values = c('#bebebe', '#1e3763')) +
  theme_minimal() +
  theme(legend.position = 'top',
        strip.placement = 'outside',
        panel.spacing.x = unit(0, 'mm'),
        axis.title.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        strip.text = element_text(face = 2))

owfi6suc

owfi6suc3#

我同意刻面在这里可能是有用的,除非,你需要它看起来完全一样(没有刻面?)。否则我会使用facet.grid并将条带移动到图表的底部。我添加了一个负值,使它看起来更相似,你可以玩主题来更改样式...

添加负值并更改“名称”

df2 <- df2 %>% mutate(value=ifelse(name=="value_add_mother_sector_total"&YEAR=="2016"&sector=="Catholic", -40, value)) %>% mutate(name=ifelse(name=="value_add_mother_sector_total", "MLSH", "Base"))

ggplot(data=df2, aes(x=YEAR , y=value, fill=name)) + geom_bar(stat="identity", position="dodge", color="black", width=1) + geom_hline(yintercept = 0, size=.5)+ labs(x=NULL, y= "Value")+ scale_fill_manual(name=NULL, values=c("Base"="navy","MLSH"= "gray80"))  + theme(panel.border = element_rect(size = 1, color="black",fill=NA))+ theme(axis.text.x = element_text(size = 20),axis.text.y = element_text(size = 20)) + theme(axis.title.x = element_text(size = 22, face="bold", vjust=-.5),axis.title.y = element_text(size = 22, face="bold", vjust = -1.5)) + facet_wrap(~sector,strip.position="bottom") + scale_x_continuous(breaks=c(2011, 2016, 2021)) +theme_calc() + scale_y_continuous(expand = c(0, 0), limits = c(-45, 55),breaks = seq(-45, 55, 5))+ theme(legend.position = "bottom")+ theme(axis.text=element_text(size=14),axis.title=element_text(size=16, face="bold"), strip.text = element_text(size=15, face="bold"),strip.placement = "outside",panel.grid = element_line(colour="gray90")) +  guides(fill = guide_legend(nrow = 1))

enter image description here

相关问题