Home > Mobile >  Connecting means in parallel plot with ggplot2
Connecting means in parallel plot with ggplot2

Time:05-06

I'm trying to connect means in this parallel plot, but I get this error:

Error: Discrete value supplied to continuous scale

here is my code:

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"))

And this is 30x4 "df" head:

  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

Without the last line every thing is okay, and I get this, but means are not connected. Figure

CodePudding user response:

The issue is that you are using a continuous color scale but map "black" (a discrete value) on the color aes in your last geom_line. Instead set the color as a parameter and use group=1 to "connect" the points.

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")

  • Related