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

时间:2015-10-23 01:49:01

标签: 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)

对此有何建议?

1 个答案:

答案 0 :(得分:8)

由于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)

enter image description here