【问题标题】:Assign same custom color to two lines coming from different data-frames but in the same plot为来自不同数据框但在同一个图中的两条线分配相同的自定义颜色
【发布时间】:2015-06-05 18:50:07
【问题描述】:

我有以下情节,绘制来自两个不同数据框的数据:

ggplot() + 
  stat_summary( data = d0_400, aes(x=number, y=(1-value), shape=as.factor(0), size=1 ) , fun.y=mean, geom="line" ) +
  stat_summary( data = d0_400, aes(x=number, y=(1-value), shape=as.factor(0), size=4 ) , fun.y=mean, geom="point" ) +
  stat_summary( data = d1_400, aes(x=number, y=(1-value), shape=as.factor(1), size=1 ) , fun.y=mean, geom="line" ) +
  stat_summary( data = d1_400, aes(x=number, y=(1-value), shape=as.factor(1), size=4 ) , fun.y=mean, geom="point" ) +
  scale_size(range = c(1,5), guide=FALSE) + 
  scale_shape_manual(values=c(0,1) )

使用此代码,我得到以下图:

我想获得以下内容:

  • 线条和形状的自定义十六进制颜色
  • 可选:在形状图例中显示较大的形状,因为在这种尺寸下很难区分它们,并用白色填充它们。

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    对于彩色线条,只需添加 colour 参数

    p = ggplot() + 
      stat_summary( data = d0_400, ..., geom="line", colour = "#hexnum" )
    

    另外,一个可选问题的快速谷歌将我带到here

    提供的解决方案是

    p = p + guides(shape=guide_legend(override.aes=list(size=5)))
    

    【讨论】:

    • 完美!再跟进,是否可以用某种颜色填充形状。我试过fill="#456987",但没用?
    • 有点小技巧,但你可以改变点字符。添加参数pch = 16,它应该是一个实心圆圈。然后colour 就会生效。
    • 我应该将哪个函数作为参数传递pch
    • stat_summary 函数。 stat_summary( data = d0_400, ..., geom="point", colour = "#AFE1EE", pch = 16 )。如果这还不够,如果您提出新问题,您将得到更全面的答案
    • 真是个黑客!将需要找到一种方法来覆盖图例中的形状。 pch 不反映到形状图例:scale_shape_manual(values=c(0,1)
    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多