0
votes

HI all I'm using Rstudio with the following dataframe:

                      X NSUM MEAN LOWMEAN UPMEAN
2 Nonmonetary Incentive  800 4.86    4.58   5.15
3       $0 (no mention)  822 5.06    4.78   5.35
4                  $25   830 6.35    6.06   6.65
5                  $50   815 6.84    6.54   7.14
6                  $75   864 7.00    6.70   7.29

So I've created this nice plot using the following command:

plot1 <- ggplot(rawdata, aes(x = rawdata$X, y = rawdata$MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = rawdata$LOWMEAN, ymax = rawdata$UPMEAN, width =0), colour = "black") + 
  coord_flip()

Which plots the means and a bar to show the upper and lower bounds. What I want to do is change the y axis so the ticks don't appear as often but no matter what I do, ylim() or scale_y_continuous() I get the error:

Discrete values applied to continuous variable?

2
You shouldn't use $ inside ggplot. In the first argument you specify rawdata as your data, you don't have to keep re-typing it throughout.Gregor Thomas

2 Answers

0
votes

It would help if you could:

  1. provide reproducible data rather than giving us the output and
  2. show us what arguments you passed to ylim() and scale_y_continuous(). Without that, it's hard to know exactly what you did wrong since this is where your problem is. We can't fix it if we don't know that.

Something like scale_y_continuous(breaks = seq(4.5, 7.5, by = 1)) should work fine. This scales the axis (rawdata$MEAN) from 4.5 to 7.5 in increments of 1. You should be able to make the axis work fine this way. Something like ylim(4.5, 7.5) also works. When I added this to your ggplot code it worked fine.

0
votes

So there are a couple of things.

First, aes(...) evaluates it's argument in the context of the default dataset. This means that aes(x=X,...) will look (first) for a column X in whatever data frame you use in the data=... argument (rawdata, in your case). You should never use, e.g. rawdata$X inside aes(...). This can cause unpredictable and often very obscure results.

Second, to control the number of axis ticks, use breaks=... and limits=... in scale_y_continuous(...). The former will set the ticks but might not display all of them because limits are set automatically. The latter will override the default limits. So here are two example with your data:

library(ggplot2)
ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8))

Notice how we've set the breaks to (4,5,6,7,8), but we don't see 4 and 8. This is because ggplot is setting limits automatically, which don't include 4 and 8. You can force this as follows:

ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8), limits=c(4,8))

Finally, if you want toget rid of the faint grid lines between the major ticks, you need to use theme(...)

ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8), limits=c(4,8))+
  theme(panel.grid.minor=element_blank())