【问题标题】:ggplot2 barplot with dual Y-axis and error bars具有双 Y 轴和误差线的 ggplot2 条形图
【发布时间】:2018-09-10 18:59:26
【问题描述】:

我正在尝试生成带有双 Y 轴和误差线的条形图。我已经成功地为一个变量生成了一个带有误差线的图,但我不知道如何为另一个变量添加误差线。我的代码看起来像这样。谢谢。

library(ggplot2)


#Data generation
Year <- c(2014, 2015, 2016)
Response <- c(1000, 1100, 1200)
Rate <- c(0.75, 0.42, 0.80)
sd1<- c(75, 100, 180)
sd2<- c(75, 100, 180)

df <- data.frame(Year, Response, Rate,sd1,sd2)
df



# The errorbars overlapped, so use position_dodge to move them horizontally
pd <- position_dodge(0.7) # move them .05 to the left and right


png("test.png", units="in", family="Times",  width=2, height=2.5, res=300) #pointsize is font size| increase image size to see the key
ggplot(df)  + 
  geom_bar(aes(x=Year, y=Response),stat="identity", fill="tan1", colour="black")+
  geom_errorbar(aes(x=Year, y=Response, ymin=Response-sd1, ymax=Response+sd1),
                width=.2,  # Width of the error bars
                position=pd)+
 geom_line(aes(x=Year, y=Rate*max(df$Response)),stat="identity",color = 'red', size = 2)+
 geom_point(aes(x=Year, y=Rate*max(df$Response)),stat="identity",color = 'black',size = 3)+
 scale_y_continuous(name = "Left Y axis", expand=c(0,0),limits = c(0, 1500),breaks = seq(0, 1500, by=500),sec.axis = sec_axis(~./max(df$Response),name = "Right Y axis"))+
  theme(
    axis.title.y = element_text(color = "black"),
    axis.title.y.right = element_text(color = "blue"))+
  theme(
    axis.text=element_text(size=6, color = "black",family="Times"),
    axis.title=element_text(size=7,face="bold", color = "black"),
    plot.title = element_text(color="black", size=5, face="bold.italic",hjust = 0.5,margin=margin(b = 5, unit = "pt")))+
  theme(axis.text.x = element_text(angle = 360, hjust = 0.5, vjust = 1.2,color = "black" ))+
  theme(axis.line = element_line(size = 0.2, color = "black"),axis.ticks = element_line(colour = "black", size = 0.2))+
  theme(axis.ticks.length = unit(0.04, "cm"))+
  theme(plot.margin=unit(c(1,0.1,0.1,0.4),"mm"))+
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 4, b = 0, l = 0)))+
  theme(axis.title.x = element_text(margin = margin(t = 0, r = 4, b = 2, l = 0)))+
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank())+
  ggtitle("SRG3")+
  theme(legend.position="top")+
  theme(  legend.text=element_text(size=4),
          #legend.justification=c(2.5,1),
          legend.key = element_rect(size = 1.5),
          legend.key.size = unit(0.3, 'lines'),
          legend.position=c(0.79, .8),  #width and height
          legend.direction = "horizontal",
          legend.title=element_blank())

dev.off()

我的情节如下:

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    对未来问题的建议:您的示例远非minimal reproducible example。所有视觉效果和注释都与您的问题无关,但会使代码过于复杂,这使得其他人更难使用它。

    以下内容就足够了:

    ggplot(df) + 
    geom_bar(aes(x = Year, y = Response), 
                 stat = "identity", fill = "tan1", 
                 colour = "black") +
    geom_errorbar(aes(x = Year, ymin = Response - sd1, ymax = Response + sd1),
                  width = .2,
                  position = pd) +
    geom_line(aes(x = Year, y = Rate * max(df$Response)), 
              color = 'red', size = 2) +
    geom_point(aes(x = Year, y = Rate * max(df$Response)), 
               color = 'black', size = 3)
    

    (请注意,我已经删除了所有geom_s 中的stat = "identity",因为这是默认设置的。此外,y 不是geom_errorbar() 的有效美学,所以我也省略了。)

    假设您要为其绘制误差线的附加变量是Rate * max(df$Response)),并且相关的标准差是sd2,您可以简单地追加

     + geom_errorbar(aes(x = Year, ymin = Rate * max(df$Response) - sd2, 
                         ymax = Rate * max(df$Response) + sd2), 
                     colour = "green",
                     width = .2)
    

    到上面的代码块。这会产生下面的输出。

    【讨论】:

    • 感谢您的回答。您的绘图中缺少右 y 轴
    • 不客气。抱歉,我忘记了:不可能在此处添加第二个 Y 轴,因为 Rate * max(df$Response) 不是 Response 的单调变换,请参阅 ggplot2.tidyverse.org/reference/sec_axis.html
    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多