代码之家  ›  专栏  ›  技术社区  ›  pali

带双Y轴和误差条的ggplot2条形图

  •  0
  • pali  · 技术社区  · 7 年前

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

    库(ggplot2)
    
    
    #数据生成
    C年(2014、2015、2016)
    响应<-C(1000、1100、1200)
    费率<-C(0.75、0.42、0.80)
    SD1<-C(75、100、180)
    SD2<-C(75、100、180)
    
    df<-数据帧(年、响应、速率、sd1、sd2)
    东风
    
    
    
    #误差线重叠,所以使用位置闪避水平移动它们
    pd<-位置闪避(0.7)将它们左右移动0.05
    
    
    png(“test.png”,units=“in”,family=“Times”,width=2,height=2.5,res=300)pointsize是字体大小增加图像大小以查看键
    GGTRAP(DF)+
    geom_bar(aes(x=年,y=响应),stat=“identity”,fill=“tan1”,colour=“black”)。+
    几何误差(aes(x=年,y=响应,ymin=响应-sd1,ymax=响应+sd1)
    宽度=0.2,误差条宽度
    位置= PD)
    geom_line(aes(x=年,y=率*最大值(df$response)),stat=身份,color=红色,size=2)+
    几何点(aes(x=年,y=率*最大值(df$response)),stat=标识,color=黑色,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”))+
    主题(
    axis.title.y=元素文本(color=“black”),
    axis.title.Y.right=元素文本(color=“blue”))+
    主题(
    axis.text=element_text(size=6,color=“black”,family=“Times”),
    axis.title=element_text(size=7,face=“bold”,color=“black”),
    plot.title=元素文本(color=“black”,size=5,face=“bold.italic”,hjust=0.5,margin=margin(b=5,unit=“pt”)))+
    主题(axis.text.x=element_text(angle=360,hjust=0.5,vjust=1.2,color=“black”))+
    主题(axis.line=element_line(size=0.2,color=“black”),axis.ticks=element_line(colour=“black”,size=0.2))。+
    主题(axis.ticks.length=单位(0.04,“cm”))+
    主题(plot.margin=单位(c(1,0.1,0.1,0.4),“mm”))+
    主题(axis.title.y=element_text(margin=margin(t=0,r=4,b=0,l=0)))+
    主题(axis.title.x=element_text(margin=margin(t=0,r=4,b=2,l=0)))+
    主题(
    panel.grid.major=元素_blank(),
    panel.grid.minor=元素_blank(),
    panel.background=元素_blank())+
    GGTITLE(“SRG3”)。+
    主题(legend.position=“top”)。+
    主题(legend.text=element_text(size=4),
    #legend.justify=c(2.5,1)
    legend.key=元素矩形(大小=1.5),
    legend.key.size=单位(0.3,'行'),
    legend.position=c(0.79,.8),宽度和高度
    legend.direction=“水平”,
    legend.title=元素_blank())
    
    DEFF()
    < /代码> 
    
    

    我的情节如下:

    .

    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()
    

    我的情节如下:

    1 回复  |  直到 7 年前
        1
  •  1
  •   Martin C. Arnold    7 年前

    关于未来问题的建议:您的示例远不是a minimal reproductive example 。所有的视觉效果和注释都与您的问题无关,但使代码过于复杂,这使得其他人难以处理它。

    以下就足够了:

    ggplot(df)+
    土工格栅(aes(x=年,y=响应)
    stat=“identity”,fill=“tan1”,
    colour=“黑色”)。+
    几何误差(aes(x=年,ymin=响应-sd1,ymax=响应+sd1)
    宽度=2,
    位置=pd)+
    几何线(aes(x=年,y=速率*最大值(df$response)),
    颜色=红色,尺寸=2)+
    几何点(aes(x=年,y=速率*最大值(df$响应)),
    颜色=黑色,尺寸=3)
    < /代码> 
    
    

    (请注意,我已经删除了stat=“identity”in allgeom_uux>s,因为这是由默认设置的。此外,yis not a valid aestetic forgeom_errorbar().so i省略that,too.)。

    假设要为其绘制误差线的附加变量是rate*max(df$response))and that the relevant standard deviation issd2,you may just append

    +geom_errorbar(aes(x=year,ymin=rate*max(df$response)-sd2,
    Ymax=比率*最大值(df$response)+sd2)
    colour=“绿色”,
    宽度=2)
    < /代码> 
    
    

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

    . 所有的视觉效果和注释都与您的问题无关,但使代码过于复杂,这使得其他人难以处理它。

    以下内容就足够了:

    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)
    

    (注意我已经搬了stat = "identity"总共geom_因为这是默认设置的。此外,y不是有效的aestetic forgeom_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)
    

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

    enter image description here

    推荐文章