【问题标题】:How to get geom_smooth() ignore my colour grouping如何让 geom_smooth() 忽略我的颜色分组
【发布时间】:2016-01-22 11:23:18
【问题描述】:

我正在尝试为我的因子的两个级别(按颜色分组)制作一个带有拟合线的图。我使用形状对另一个变体进行分组,但是当我尝试更平滑时,我最终得到 4 条线,而我总共只需要一条两条线(每种颜色 1 条)

这是我使用的数据和代码:

data <- structure(list(K = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2s", "4s"), class = "factor"), 
    q = c(0.12, 0.11, 0.1, 0.09, 0.08, 0.07, 0.06, 0.05, 0.04, 
    0.03, 0.02, 0.01, 0.12, 0.11, 0.1, 0.09, 0.08, 0.07, 0.06, 
    0.05, 0.04, 0.03, 0.02, 0.01, 0.12, 0.11, 0.1, 0.09, 0.08, 
    0.07, 0.06, 0.05, 0.04, 0.03, 0.02, 0.01, 0.12, 0.11, 0.1, 
    0.09, 0.08, 0.07, 0.06, 0.05, 0.04, 0.03, 0.02, 0.01), rarity = c(0.907, 
    0.9206, 0.9359, 0.9321, 0.9405, 0.9344, 0.9449, 0.9106, 0.8844, 
    0.8829, 0.8989, 0.798, 0.7464, 0.8225, 0.877, 0.8521, 0.9127, 
    0.9317, 0.9245, 0.9595, 0.9628, 0.9573, 0.9423, 0.9428, 0.5802, 
    0.6414, 0.5123, 0.57, 0.587, 0.5655, 0.5231, 0.517, 0.4694, 
    0.5459, 0.3745, 0.3274, 0.7936, 0.7821, 0.7297, 0.7227, 0.6814, 
    0.6608, 0.6721, 0.6202, 0.5924, 0.5659, 0.5448, 0.6138), 
    metric = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("APD", "ED"
    ), class = "factor")), .Names = c("K", "q", "rarity", "metric"
), class = "data.frame", row.names = c(NA, -48L))

library(ggplot2) 
ggplot(data=data, aes(x=q, y=rarity, colour=metric, shape=K))+ 
  ggtitle("Relationship")+
  xlab("rate of character change")+
  ylab("Correlation coefficient to average rarity")+
  geom_point()+
  geom_smooth(method=lm,se=FALSE)

对此有什么建议吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    每个组都有两行,因为它被metricK 分开。您真的希望shape 美学仅适用于点层,而不是稍后的平滑。最好将那个属性的aes() 移到那里。

    ggplot(data=data, aes(x=q, y=rarity, colour=metric))+ 
      ggtitle("Relationship")+
      xlab("rate of character change")+
      ylab("Correlation coefficient to average rarity")+
      geom_point(aes(shape=K))+
      geom_smooth(method=lm,se=FALSE)
    

    【讨论】:

    • 关键见解:在 geom_points(aes()) 而不是 ggplot(aes()) 中指定您不希望平滑看到的颜色/形状 感谢 MrFlick,vahab!
    • 在你想忽略美学的几何图形中,也总是可以将任何美学重新指定为 NULL。例如。 geom_smooth(aes(shape = NULL)).
    猜你喜欢
    • 2018-08-23
    • 2019-09-03
    • 2022-11-22
    • 2020-03-09
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多