Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

ggplot2 - Even height of bars in facet wrapped stacked barchart

I want to make the bar heights of a facet wrapped stacked barchart all equal height with the percentages of each category projected in the middle of them. I just can't figure it out.

With the following code I got this figure:

    ggplot(na.omit(plot_data), aes(x = cycle, y = percent, fill = response)) + 
  geom_col() + 
  facet_grid(~mut)+
  geom_text(aes(label=percent(percent, accuracy = 1)),
            position = position_stack(vjust = .5), size = 4, color = "white") +
  labs(x="", y="Percentage")+
  scale_y_continuous(labels = percent)+
  theme_bw()+
  #scale_fill_brewer(palette = "Accent")+
  scale_fill_jco()+
  ggtitle("Response by mutation status (%)")+
  theme(strip.text = element_text(size=18))+
  theme(axis.line = element_line(size=1, colour = "black"),
        panel.grid.major = element_line(colour = "#d3d3d3"), panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank()) +
  theme(plot.title = element_text(size = 18, family = "serif", face = "bold"),
        text=element_text(family="serif"),
        axis.text.x=element_text(colour="black", size = 12),
        axis.text.y=element_text(colour="black", size = 12))

enter image description here

And I want it to be just like this figure but then obviously retaining the facet wrap:

enter image description here

This is my data:

tibble [60 x 6] (S3: tbl_df/tbl/data.frame)
 $ PID: num [1:60] 1 2 3 4 5 6 7 8 9 10 ...
 $ mut: chr [1:60] "PP" "PN" "PP" "n/a" ...
 $ C2 : chr [1:60] "n/a" "n/a" "n/a" "PR" ...
 $ C4 : chr [1:60] "MR" "SD" "n/a" "n/a" ...
 $ C8 : chr [1:60] "MR" "MR" "n/a" "n/a" ...
 $ BE : chr [1:60] "MR" "MR" "n/a" "PR" ...

Your help is much appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...