如何让geom_smooth()忽略我的颜色分组

vah*_*hab 12 r ggplot2

我正在尝试使用适合我的因素的两个级别(按颜色分组)的线条绘制.我使用形状来组合另一个变体但是当我尝试更顺畅时,我最终得到了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)
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

MrF*_*ick 14

你得到两行各组becase的它被分成两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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 关键见解:指定您不希望在`geom_points(aes())中看到的颜色/形状,而不是在`ggplot(aes())`谢谢MrFlick,vahab! (4认同)
  • 在您要忽略该美学的几何图形中,也始终可以将任何美学重新指定为NULL。例如`geom_smooth(aes(shape = NULL))`。 (3认同)