format internal lines of a stacked geom_bar ggplot

僤鯓⒐⒋嵵緔 提交于 2019-12-11 16:31:12

问题


I want to remove the internal borders from my ggplot, leaving a coloured border around the outside of each bar only. Here is a test data frame, with a stacked bar plot. Ideally, I will end up with the groups in the stack still being a shade of grey, with a colourful outline per box.

test <- data.frame(iso=rep(letters[1:5],3),
               num= sample(1:99, 15, replace=T),
               fish=rep(c("pelagic", "reef", "benthic"), each=5),
               colour=rep(rainbow(n=5),3))

ggplot(data=test, aes(x=iso, y=num, fill=fish, colour=colour)) +
  geom_bar(stat="identity") +
  theme_bw() + 
  scale_colour_identity() + scale_fill_grey(start = 0, end = .9)


回答1:


You can accomplish this by moving the fill and colour aes() settings into two separate geom_bar() elements: one which takes the sum for each iso value (the outline), and another which splits things up by fish:

ggplot(data=test, aes(x=iso, y=num)) +
  geom_bar(stat="summary", fun.y="sum", aes(color=colour)) +
  geom_bar(stat="identity", aes(fill=fish)) +
  theme_bw() + 
  scale_colour_identity() + 
  scale_fill_grey(start = 0, end = .9)



来源:https://stackoverflow.com/questions/50712063/format-internal-lines-of-a-stacked-geom-bar-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!