Error: Discrete value supplied to continuous scale

8,048 views
Skip to first unread message

abc123s

unread,
May 16, 2012, 2:54:55 PM5/16/12
to ggplot2
I get the error
Error: Discrete value supplied to continuous scale
when I run the following code:
require(reshape)
require(ggplot2)
a <- read.csv("test.csv")
a$Date <- as.POSIXct(strptime( as.character( a$Date) , '%m/%d/%Y %H:
%M'))
a <- a[c(-7,-8)]
a$sum <- rowSums(a[2:6])
a <- melt (a, id = 'Date' , variable_name = 'Machines')
p = ggplot(a, aes(Date,value)) + geom_line(aes(colour = Machines)) +
scale_y_continuous('Load') + scale_x_continuous()

The test.csv file begins as a csv file where the first column
specifies the date and time of the observation, and then each of the
next columns specifies a machine that the observation was made from.
I'm trying to graph these observations in chronological order, all on
one graph (as well as the sum of all the observations on each date).

I want to change the scale of the markings on the x-axis, but
regardless of whether or not I use scale_x_continuous() or
scale_x_discrete(), I get the same error. Does anyone know why this is
happening, or if there is a better way to do what I want?

Brandon Hurr

unread,
May 17, 2012, 3:42:11 AM5/17/12
to abc123s, ggplot2
This looks like it's your data that is the problem. If you could take a subset of it, verify that it gives you the problem, and copy/paste the output of dput(subset of your data here) into your response we can help you much easier. Otherwise, it's very hard to say and we're all just guessing. 


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
May 17, 2012, 4:47:33 AM5/17/12
to abc123s, ggplot2
Hi:

In addition to Brandon's comments, there are several things missing
that could be important, including the version of R you are running
and the version of ggplot2. You can supply this with the output from
sessionInfo().

On Wed, May 16, 2012 at 11:54 AM, abc123s <peter...@gmail.com> wrote:
> I get the error
> Error: Discrete value supplied to continuous scale
> when I run the following code:
> require(reshape)
> require(ggplot2)
> a <- read.csv("test.csv")
> a$Date <- as.POSIXct(strptime( as.character( a$Date) , '%m/%d/%Y %H:
> %M'))
> a <- a[c(-7,-8)]
> a$sum <- rowSums(a[2:6])
> a <- melt (a, id = 'Date' , variable_name = 'Machines')
> p = ggplot(a, aes(Date,value)) + geom_line(aes(colour = Machines)) +
> scale_y_continuous('Load') + scale_x_continuous()

What is the class of Date in the melted data frame a? If it is a
POSIXct or Date object, you should be able to use scale_x_date() or
scale_x_datetime(), but the arguments differ in versions 0.8.9 and
0.9.x. That's why the sessionInfo() matters. For version 0.9.x, the
ggplot2 transition guide may be helpful: see
http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf,
particularly section 7.1.

HTH,
Dennis
>
> The test.csv file begins as a csv file where the first column
> specifies the date and time of the observation, and then each of the
> next columns specifies a machine that the observation was made from.
> I'm trying to graph these observations in chronological order, all on
> one graph (as well as the sum of all the observations on each date).
>
> I want to change the scale of the markings on the x-axis, but
> regardless of whether or not I use scale_x_continuous() or
> scale_x_discrete(), I get the same error. Does anyone know why this is
> happening, or if there is a better way to do what I want?
>
Reply all
Reply to author
Forward
0 new messages