使用ggplot2在平行图中的连接方式

Connecting means in parallel plot with ggplot2

我正在尝试连接平行图中的均值,但出现此错误:

Error: Discrete value supplied to continuous scale

这是我的代码:

mdf <- melt(df, id.vars = "sub_i")
class_info <- summarise(group_by(mdf,variable),value = mean(value))

ggplot(data = mdf,
       mapping = aes(x=variable,
                     y=value,
                     color=(sub_i))) +
  geom_line(aes(group = sub_i),size=0.3) + 
  geom_point(shape=1) +
  theme(legend.position="none") +
  labs(y = "Correlation",x="") +
  scale_color_gradientn(colours = rainbow(30)) + 

  # mean point and lines
  geom_point(data = class_info, color="black", size =4,alpha=0.8) +
  geom_line(data = class_info, mapping=aes(color="black"))

这是 30x4“df”头:

  sub_i      msub_r      indiv_r    msub_null
1     1  0.06249845  0.066307886 -0.002599296
2     2 -0.03429027  0.068107218 -0.007419282
3     3  0.04417815  0.052935044  0.014339405
4     4  0.03578681  0.004392912  0.004940727
5     5  0.02851687 -0.075268277 -0.005774686
6     6  0.04049765  0.034980933 -0.002489030

没有最后一行,一切都很好,我明白了,但是没有连接。 Figure

问题是您使用的是连续色标,但在上一个 geom_line 中的 color aes 上映射 "black"(离散值)。而是将颜色设置为参数并使用 group=1 来“连接”这些点。

library(ggplot2)

ggplot(
  data = mdf,
  mapping = aes(
    x = variable,
    y = value,
    color = (sub_i)
  )
) +
  geom_line(aes(group = sub_i), size = 0.3) +
  geom_point(shape = 1) +
  theme(legend.position = "none") +
  labs(y = "Correlation", x = "") +
  scale_color_gradientn(colours = rainbow(30)) +
  geom_point(data = class_info, color = "black", size = 4, alpha = 0.8) +
  geom_line(data = class_info, mapping = aes(group = 1), color = "black")