0

I'm trying to draw a simple line plot using ggplot geom_line()

my code is very simple:

df <- as.data.frame(table(data()$Date))
colnames(df) <- c('Date','value')

volPlot <- ggplot(data = df, aes(x = Date,y = value))
volPlot <- volPlot + geom_point(size = 3) + geom_line(size = 3)

return(volPlot)

df looks like this:

         Date value
1  2016-06-01   379
2  2016-06-02   262
3  2016-06-03   264
4  2016-06-04   167
5  2016-06-06   410

The plot shows the points but no line in between them which is what I want

Note: the console returns the following message:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

So I suppose the problem comes from my data structure but I don't know how to prevent this, any help would be nice.

EDIT: Solution was found, you need to add group = 1 in the aes:

volPlot <- ggplot(data = df, aes(x = Date,y = value, group = 1))

1 Answers1

1

With purely numerical Dates as in

data<-cbind(Date=1:5,Value=c(379,262,264,167,410))
ggplot(data = df, aes(x = Date,y = value))+geom_point(size = 3) + geom_line(size = 3)

it works just as expected - lines show up. As laid out by @GGamba, with your factorial presentation of dates, you need to help it a bit as in

df<-as.data.frame(cbind(Date=LETTERS[1:5],value=c(379,262,264,167,410)))
ggplot(data = df, aes(x = Date,y = value))+geom_point(size = 3) + geom_line(aes(group=1),size=3)

I just found more on Plotting lines and the group aesthetic in ggplot2 .

smoe
  • 500
  • 3
  • 13