2020年12月21日星期一

Stacked and grouped chart ggplot2 combined for all entries

I have a similar problem to this one: Combine Stacked and Grouped chart ggplot2

I have data that looks like this:

enter image description here

What I want to create is genom_bar chart in ggplot that look like this:

enter image description here

Where the blue area represents the gender part of the age_group. My code looks like this:

  p <- ggplot(data, aes(x=`Green cars are nice`, y=round(percentage, digits = 2), fill = Age_group)) +      geom_bar(stat = "identity", position=position_dodge()) +      facet_grid(.~residence) +      xlab(filename) +      ylab(NULL) +      theme_bw() +      theme(axis.text.x = element_text(size = 12, angle = 45, vjust = 1, hjust=1)) +      theme(        axis.title.x = element_text(size = 14),        axis.text.x = element_text(size = 14),        axis.title.y = element_text(size = 14),        axis.text.y = element_text(size = 14)) +      guides(fill=guide_legend(title=NULL))              p  

What do I need to add in order to make the bar look like in my paint made picture? I had troubles understanding the code shown in the example from SO that I posted.

Edit:

Berlin  18-39   Woman   Agree   1.689550205 0.009283243  Berlin  18-39   Woman   Disagree    3.942283812 0.0216609  Berlin  18-39   Woman   Don't know  0.563183402 0.003094414  Berlin  40-70   Woman   Agree   1.689550205 0.009283243  Berlin  40-70   Woman   Disagree    3.942283812 0.0216609  Berlin  40-70   Woman   Don't know  0.563183402 0.003094414  London  18-39   Woman   Agree   1.689550205 0.009283243  London  18-39   Woman   Disagree    3.942283812 0.0216609  London  18-39   Woman   Don't know  0.563183402 0.003094414  London  40-70   Woman   Agree   1.689550205 0.009283243  London  40-70   Woman   Disagree    3.942283812 0.0216609  London  40-70   Woman   Don't know  0.563183402 0.003094414  Berlin  18-39   Man Agree   1.689550205 0.009283243  Berlin  18-39   Man Disagree    3.942283812 0.0216609  Berlin  18-39   Man Don't know  0.563183402 0.003094414  Berlin  40-70   Man Agree   1.689550205 0.009283243  Berlin  40-70   Man Disagree    3.942283812 0.0216609  Berlin  40-70   Man Don't know  0.563183402 0.003094414  London  18-39   Man Agree   1.689550205 0.009283243  London  18-39   Man Disagree    3.942283812 0.0216609  London  18-39   Man Don't know  0.563183402 0.003094414  London  40-70   Man Agree   1.689550205 0.009283243  London  40-70   Man Disagree    3.942283812 0.0216609  London  40-70   Man Don't know  0.563183402 0.003094414  
https://stackoverflow.com/questions/65401282/stacked-and-grouped-chart-ggplot2-combined-for-all-entries December 22, 2020 at 07:21AM

没有评论:

发表评论