-1

Lets take the following data.frame as an example:

data <- data.frame(windowSize1=rep(c("1 week","2 weeks"),each=6),
                   windowSize2 = rep(c("0 weeks","1 week"),6),
                   Modell = rep(c("SVM","random Forest","NN"), 4),
                   MSE=c(rnorm(12))
                   )

ggplot()+
  geom_point(data=data, aes(x=windowSize1, y=MSE, color=Modell, shape=windowSize2),size=2)

I want to add to this plot a line connecting the MSE value of row 1 in the data and of row 7 in the same color of the reelated SVM - datapoints. But when I try:

  ggplot()+
  geom_point(data=data, aes(x=windowSize1, y=MSE, color=Modell, shape=windowSize2),size=2)+
  geom_line(data=data[c(1,7),],aes(x=windowSize1, y=MSE, color=Modell) )

I get the error method: "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?"

Does anyone understand this message? (I don't understand it, because both rows of the data.frame are from the Group "SVM")

Miri
  • 75
  • 1
  • 6
  • 1
    you should move the `data` statement and the `aes` statement into the `ggplot` function. Try that – akash87 May 21 '19 at 13:28

1 Answers1

1

I think the problem is that windowSize1 is a factor, and therefore ggplot does not know how to draw a line between the values. If you change windowSize1 to integer, then it works:

library(ggplot2)
data <- data.frame(windowSize1=rep(c(1,2),each=6),
                   windowSize2 = rep(c("0 weeks","1 week"),6),
                   Modell = rep(c("SVM","random Forest","NN"), 4),
                   MSE=c(rnorm(12))
)

ggplot()+
  geom_point(data=data, aes(x=windowSize1, y=MSE, color=Modell, shape=windowSize2),size=2)+
geom_line(data=data[c(1,7),],aes(x=windowSize1, y=MSE, color=Modell) )

enter image description here

EDIT: But there is actually a better solution that leaves the factors as they are. The trick is to make ggplot understand between which points it is supposed to draw lines. You can do this with the group argument. In this case the Modells build the groups, so:

data <- data.frame(windowSize1=rep(c("0 weeks","1 week"),each=6),
                   windowSize2 = rep(c("0 weeks","1 week"),6),
                   Modell = rep(c("SVM","random Forest","NN"), 4),
                   MSE=c(rnorm(12)))
ggplot()+
  geom_point(data=data, aes(x=windowSize1, y=MSE, color=Modell, shape=windowSize2,size=2))+
  geom_line(data=data[c(1,7),],aes(x=windowSize1, y=MSE, color=Modell, group=Modell,size=1) )

enter image description here

otwtm
  • 1,779
  • 1
  • 16
  • 27