Home > Back-end >  group and fill in geom_col
group and fill in geom_col

Time:12-23

I need to do this plot

enter image description here

My understanding is that i need to use fill for model names name (base vs mother) and group for Year.

I tried

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

but it does not work...

enter image description here

How to fix this? :(

my data looks like this

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"

CodePudding user response:

like so?

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

enter image description here

CodePudding user response:

You could do

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))

enter image description here

CodePudding user response:

I agree faceting might be helpful here, unless, do you need it to look exactly the same (no facets?). Otherwise I'd use facet.grid and move the strips to the bottom of the graph. I added a negative value so make it appear more similar and you can play with the theme to change the styles...

Adding negative value and changing "name"

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"))

Plot

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

  • Related