Eliminating negative y-axis values ​​in ggplot2

I would like to change the y-axis scale to ggplot2 for the case when I draw a value that, as I know, is always positive.

To illustrate this problem, consider the graph that is created when plotting the graph:

df = data.frame(x=seq(1,10),y=rep(0,10))
ggplot(df,aes(x,y)) + geom_point()

enter image description here

I would like to set the lower limit for the y axis in this case to 0 without specifying the maximum value for the y axis. If possible, I would like to do this in one command (since I should usually call facet_gridusing scales = freeafter creating this graph)

+4
source share
2 answers

Edit

OP , y, expand 0. :

:

set.seed(1)

df <- data.frame(x=1:10,
                 y=rep(0, 10))
df1 <- data.frame(x=1:10,
                  y=sample(1:10 , 10, replace=T))
df
df1

, :

plot.zeros <- function(data) {
  upper.lim <- ifelse(any(data$y != 0), NA, 1)
  print(ggplot(data, aes(x, y)) + geom_point() +
    scale_y_continuous(limits = c(0, upper.lim), expand = c(0.01, 0)))
}

plot.zeros(df)
plot.zeros(df1)

, y 1, y- 0. y- , y ​​ 0, ggplot. y-max , "else" ifelse - , 1. , expand ylim(0, upper.lim) scale_y_continuous.


, min , expand_limits. , :

df <- data.frame(type=sample(c("a","b"), 20, replace=T),
                  x=rep(1:10, 2),
                  y=c(rep(0, 10), sample(1:10 , 10, replace=T)))
df

ggplot(df,aes(x,y)) + geom_point() +
    facet_wrap(facets='type', scales="free") +
    expand_limits(y=0)

enter image description here

, facet_wrap grid_wrap, :

scale_y_continuous(limits=c(0, max(df$y) * 1.1))
+2

, , , , .

( ) , 1.0.0 NA , ( @Cotton.Rockwood ).

+2

All Articles