Ggplot set ylim min OR max

I am trying to make a graph where I can simply specify the minimum or maximum value for the y axis. But I getError in if (zero_range(range)) { : missing value where TRUE/FALSE needed

From the documentation :

You can leave one value as NA for calculation from a data range.

So 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 delete ylimor change the value NAto a numeric value, it will work well. What am I missing here?

+4
source share
1 answer

You can use expansion constraints to expand the axis in only one direction. For instance:

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  expand_limits(y=c(NA, 50))

For your example, this would be:

plot + expand_limits(y=c(NA, 100))

. , . , . :

plot + expand_limits(y=100)

:

p = ggplot(mtcars, aes(wt, mpg)) +
  geom_point()

p + expand_limits(y=-20)
p + expand_limits(y=200)

enter image description here

+6

Source: https://habr.com/ru/post/1691261/


All Articles