bshor
bshor

Reputation: 5059

Discrete value/continuous scale error in ggplot2 even when I use a numeric

I'm trying to plot a cumulative sum line plot like in this Stack Overflow answer. Here is my data:

example = structure(list(date = structure(c(16594, 16611, 16612, 16616, 
16686, 16702, 16723, 16772, 16825, 16827), class = "Date"), endorse = c(13, 
1, 1, 3, 2, 1, 2, 5, 1, 1)), .Names = c("date", "endorse"), row.names = c(8L, 
10L, 12L, 14L, 26L, 34L, 40L, 53L, 68L, 69L), class = "data.frame")

And here is the ggplot2 command I am trying to execute:

ggplot(data = example, aes(x = date, y = cumsum(endorse))) + geom_line() + 
  geom_point() + theme(axis.text.x = element_text(angle=90, hjust = 1)) + 
  scale_x_discrete(labels = example$date) + scale_y_continuous(limits=c(0,30)) + xlab("Date")

I get the "Error: Discrete value supplied to continuous scale" error. But the endorse variable (supposed to be the y variable) is numeric, so I'm not sure what's the problem. The date is obviously discrete.

Upvotes: 0

Views: 579

Answers (1)

lizzie
lizzie

Reputation: 606

One suggestion is to use scale_x_date instead of scale_x_discrete. For example:

ggplot(data = example, aes(x = date, y = cumsum(endorse))) + 
  geom_line() + 
  geom_point() + 
  theme(axis.text.x = element_text(angle=90, hjust = 1)) + 
  scale_x_date() +
  scale_y_discrete(limits=c(0,30)) +
  xlab("Date")

Upvotes: 1

Related Questions