【问题标题】:Color by manual scale for geom errorbar几何误差条的手动刻度颜色
【发布时间】:2015-12-04 06:23:36
【问题描述】:

我在尝试将错误栏设置为手动颜色以遵循我为我的点设置的配色方案时遇到了麻烦。本质上,我希望每个误差条的颜色与其关联点的填充颜色相匹配。

创建数据框

mean<-c(4,5,6,7)
CI<-c(0.5,0.4,0.3,0.2)
stress<-c(1,2,3,4)
a<-c(10,10,20,20)
b<-c(7.5,7.5,8,8)
data<-data.frame(mean,CI,stress,a,b)

原图

library(ggplot2)
p<- ggplot(data, aes(a, mean))
p+geom_point()+
  geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
  geom_point(aes(fill=factor(stress)),size=8, shape=21)+
  scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
  scale_x_continuous("Level A",breaks=c(10,20))+
  ylab(expression("Level B"))+
  theme_bw(17)

尝试手动创建彩色误差线,但没有成功

p<- ggplot(data, aes(a, mean))
    p+geom_point()+
      geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
scale_color_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
      geom_point(aes(fill=factor(stress)),size=8, shape=21)+
      scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
      scale_x_continuous("Level A",breaks=c(10,20))+
      ylab(expression("Level B"))+
      theme_bw(17)

p<- ggplot(data, aes(a, mean))
        p+geom_point()+
          geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
    scale_fill_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
          geom_point(aes(fill=factor(stress)),size=8, shape=21)+
          scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
          scale_x_continuous("Level A",breaks=c(10,20))+
          ylab(expression("Level B"))+
          theme_bw(17)

【问题讨论】:

  • 我在您第一次尝试手动将颜色设置为geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI, color=factor(stress)), width=0.3) 时修改了geom_errorbar。手柄的颜色与填充相同。这就是你所追求的吗?
  • 我试图让手柄的颜色与填充颜色相同,是的。但是,我仍然没有在手柄上获得正确的颜色。它们是彩色的,但与点的填充不匹配。

标签: r colors ggplot2 errorbar


【解决方案1】:

为我工作。

ggplot(data, aes(a, mean)) +
    geom_point()+
    geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI, color=factor(stress)), width=0.3)+
    scale_color_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
    geom_point(aes(fill=factor(stress)),size=8, shape=21)+
    scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
    scale_x_continuous("Level A",breaks=c(10,20))+
    ylab(expression("Level B"))+
    theme_bw(17)

【讨论】:

  • 有什么办法可以去掉点周围的轮廓,或者用同样的方法给它上色吗?我正在尝试使用此解决方案,但我有更多数据,黑色边框挡住了点的颜色。
  • @LinaBird 使用不同的shape。在互联网上搜索代码页。例如,21 是一个带有黑色边框的透明圆圈。
猜你喜欢
  • 2018-06-05
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
相关资源
最近更新 更多