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?
So there are a couple of things.
First,
aes(...)
evaluates it's argument in the context of the default dataset. This means thataes(x=X,...)
will look (first) for a columnX
in whatever data frame you use in thedata=...
argument (rawdata
, in your case). You should never use, e.g.rawdata$X
insideaes(...)
. This can cause unpredictable and often very obscure results.Second, to control the number of axis ticks, use
breaks=...
andlimits=...
inscale_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: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:Finally, if you want toget rid of the faint grid lines between the major ticks, you need to use
theme(...)
It would help if you could:
ylim()
andscale_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 likeylim(4.5, 7.5)
also works. When I added this to your ggplot code it worked fine.