I am trying to make a plot where I could just specify the min or the max value for the y axis. But I get Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
From the documentation:
You can leave one value as NA to compute from the range of the data.
Thus, I did:
#Getting some data in
plot <- ggplot(mydata,
aes_string(y="yvar", x="xvar", colour="group", group="group", fill="group")
)
#Adding some error bars
plot <- plot + geom_errorbar(aes(ymax=agg+var, ymin=agg-var), size=0.5, colour="black", data=mydata)
plot <- plot + geom_point(size=4)
plot <- plot + geom_line(size=1)
#Here is when I just want to set y max
plot <- plot + coord_cartesian(ylim= c(NA, 100))
If I remove the ylim
or change the NA
to a numeric value, it works well. What am I missing here?
You can use expand limits to extend the axis in only one direction. For example:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
expand_limits(y=c(NA, 50))
For your example, it would be:
plot + expand_limits(y=c(NA, 100))
You can even provide a single value. If that value is greater than the maximum of the data, it will expand the maximum. If lower than the minimum of the data, it will expand the minimum. In your example:
plot + expand_limits(y=100)
And here are two reproducible examples:
p = ggplot(mtcars, aes(wt, mpg)) +
geom_point()
p + expand_limits(y=-20)
p + expand_limits(y=200)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With