1

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))
6
  • Date is interpreted as a factor - you probably want to do lubridate:parse_ymt()first. Commented May 31, 2017 at 14:03
  • It should be table(data$Date) in your first line Commented May 31, 2017 at 14:03
  • 1
    ..AND, the reason of the error: You need a group aesthetic. group = 1 should suffice in this case Commented May 31, 2017 at 14:05
  • ...And add group=value inside your aes(). And print(volPlot) rather than return. Commented May 31, 2017 at 14:05
  • @AndrewGustar the data() and return() may indicate this is part of a Shiny app, where both would be ok Commented May 31, 2017 at 14:07

1 Answer 1

2

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 .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.