代码之家  ›  专栏  ›  技术社区  ›  Jesper.Lindberg

将几何图形线添加到图例中

  •  1
  • Jesper.Lindberg  · 技术社区  · 7 年前

    在搜索了昨天和今天的网络之后,我得到一个传奇工作的唯一方法就是在本文中遵循“brian diggs”的解决方案: Add legend to ggplot2 line plot

    它给出了以下代码:

    library(ggplot2)
    ggplot()+
      geom_line(data=myDf, aes(x=count, y=mean, color="TrueMean"))+
      geom_hline(yintercept = myTrueMean, color="SampleMean")+
      scale_colour_manual("",breaks=c("SampleMean", "TrueMean"),values=c("red","blue"))+
      labs(title = "Plot showing convergens of Mean", x="Index", y="Mean")+
      theme_minimal()
    

    如果我去掉 hline 但是如果我在 哈林 那不是真正的颜色(比如 "SampleMean" )我得到一个错误,它不是一个颜色(只为 哈林 ) 怎么能加上一个像传奇这样的普通事情这么大的问题呢?有更简单的方法吗?

    要创建原始数据:

    #Initial variables
    myAlpha=2
    myBeta=2
    successes=14
    n=20
    fails=n-successes
    
    #Posterior values
    postAlpha=myAlpha+successes
    postBeta=myBeta+fails
    
    #Calculating the mean and SD
    myTrueMean=(myAlpha+successes)/(myAlpha+successes+myBeta+fails)
    myTrueSD=sqrt(((myAlpha+successes)*(myBeta+fails))/((myAlpha+successes+myBeta+fails)^2*(myAlpha+successes+myBeta+fails+1)))
    
    #Simulate the data
    simulateBeta=function(n,tmpAlpha,tmpBeta){
      tmpValues=rbeta(n, tmpAlpha, tmpBeta)
      tmpMean=mean(tmpValues)
      tmpSD=sd(tmpValues)
      returnVector=c(count=n, mean=tmpMean, sd=tmpSD)
      return(returnVector)
    }
    
    #Make a df for the data
    myDf=data.frame(t(sapply(2:10000, simulateBeta, postAlpha, postBeta)))
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   pogibas    7 年前

    给定的解决方案在大多数情况下都有效,但不适用于 geom-hline ( vline )。对于它们,您通常不必使用 aes ,但当您需要生成一个图例时,必须将它们包装在 aes :。

    库(ggplot2)
    GGPROTH()+
    地理线(aes(count,mean,color=“truemean”),mydf)+
    geom_h hline(aes(yintercept=mytruemean,color=“samplemean”))+
    比例颜色手册(值=C(“红色”、“蓝色”))+
    labs(title=“显示平均值收敛的绘图”,
    x=“索引”,
    Y=“平均”,
    颜色=空)+
    最小极小()
    < /代码> 
    
    


    查看原始数据,您可以使用geom_point以获得更好的可视化效果(还添加了一些主题更改):。

    ggplot()。+
    几何点(aes(count,mean,color=“observated”),mydf,
    α=0.3,尺寸=0.7)+
    geom_h hline(aes(yintercept=mytruemean,color=“expected”),
    线型=2,尺寸=0.5)+
    比例颜色手册(值=C(“蓝色”、“红色”))+
    labs(title=“显示平均值收敛的绘图”,
    x=“索引”,
    Y=“平均”,
    color=“mean type”)。+
    主题_minimal()。+
    参考线(颜色=参考线图例(override.aes=列表(
    线型=0,大小=4,形状=15,alpha=1)
    )
    < /代码> 
    
    

    ,但当需要生成一个图例时,必须将它们包装在俄歇电子能谱:

    library(ggplot2)
    ggplot() +
      geom_line(aes(count, mean, color = "TrueMean"), myDf) +
      geom_hline(aes(yintercept = myTrueMean, color = "SampleMean")) +
      scale_colour_manual(values = c("red", "blue")) +
      labs(title = "Plot showing convergens of Mean",
           x = "Index",
           y = "Mean",
           color = NULL) +
      theme_minimal()
    

    enter image description here


    查看可以使用的原始数据geom_point为了更好地可视化(也增加了一些主题变化):

    ggplot() +
      geom_point(aes(count, mean, color = "Observed"), myDf,
                 alpha = 0.3, size = 0.7) +
      geom_hline(aes(yintercept = myTrueMean, color = "Expected"),
                 linetype = 2, size = 0.5) +
      scale_colour_manual(values = c("blue", "red")) +
      labs(title = "Plot showing convergens of Mean",
           x = "Index",
           y = "Mean",
           color = "Mean type") +
      theme_minimal() +
      guides(color = guide_legend(override.aes = list(
        linetype = 0, size = 4, shape = 15, alpha = 1))
      )
    

    enter image description here

    推荐文章