1

I've been looking at this for a while now, does anyone see anything wrong with this ggplot syntax? I am getting this error:

Error: Discrete value supplied to continuous scale

this is z:

     Month Value
1  2011-01-01    11
2  2011-02-01     5
3  2011-03-01     6
4  2011-04-01     6
5  2011-05-01     4
6  2011-06-01     5
7  2011-07-01     3
8  2011-08-01     9
9  2011-09-01    19
10 2011-10-01     3
11 2011-11-01     6
12 2011-12-01     2
13 2012-01-01     1
14 2012-02-01     4
15 2012-04-01     1
16 2012-05-01     2
17 2012-06-01    11
18 2012-07-01     5


ggplot(z, aes(Month, Value)) + 
  geom_bar(fill="orange",size=.3)  + 
  theme_bw() + scale_x_discrete(name="Date") +   
  scale_y_continuous("Number") +
  opts(title="Monthly issues", 
       axis.title.x = theme_text(face="bold", colour="#990000"), 
       axis.text.x  = theme_text(angle=90), 
       axis.title.y = theme_text(face="bold", colour="#990000", angle=90)
   ) + 
  geom_smooth(data=z,aes(Month,Value,group=1), method="lm", size=2, color="darkblue")
2
  • It runs fine for me and produces a plot just fine. Check str(z) and make sure that you don't have Value stored as a factor. Commented Jul 19, 2012 at 14:32
  • this is the output of str(z): str(z) 'data.frame': 18 obs. of 2 variables: $ Month: Date, format: "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" ... $ Value: num 11 5 6 6 4 5 3 9 19 3 ... Commented Jul 19, 2012 at 14:33

1 Answer 1

5

Aha! The problem is your Month column, which as you note in your comment is stored as a Date. R considers this a continuous variable, hence the error with scale_x_discrete. You should probably convert it to a character with as.character if you want to use it with geom_bar.

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

3 Comments

yes when I did this z$Month<-as.Character(z$Month), I can manually run and I see the graph. Problem is that when I try to run this line of code in an R script, my images are not populated: png(paste(m,"-","Issues",".png",sep=""), height=700, width=650) and that ggplot command and dev.off(), png files are created but they are empty.
@MikeDude That's an entirely different problem that is so common it is a FAQ.
thank you so much. That was a great catch (as.character). I was finally able to get around the image generation by grid.arrange, not sure better ways but, this will do it for me.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.