格式化堆叠的geom_bar ggplot的内部线 [英] format internal lines of a stacked geom_bar ggplot

查看:167
本文介绍了格式化堆叠的geom_bar ggplot的内部线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ggplot中删除内部边框,只在每个条形的外部周围留下一个彩色边框.这是一个测试数据框,带有堆积的条形图.理想情况下,我将使堆栈中的组仍然是灰色阴影,每个框都带有彩色轮廓.

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)

推荐答案

您可以通过移动 fill colour aes()设置分为两个单独的 geom_bar()元素:一个设置为每个 iso 值(轮廓)取和,另一个设置为按 fish :

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)

这篇关于格式化堆叠的geom_bar ggplot的内部线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆