How to force a y axis to minimum and maximum range in R?
32,202
Solution 1
Just add:
+ coord_cartesian(ylim=c(0,1))
Solution 2
Try this:
scale_y_continuous( limits = c(0,1), expand = c(0,0) )
Author by
Barry
Updated on September 19, 2020Comments
-
Barry over 3 years
If you look at the graph below (y axis), you will notice that the scale is from
0
to0.20
. I have other histograms where the range is from0
to0.4
. I want to make all of them consistent from0
to1
and display they
axis from0
to1
.conne <- file("C:Aisdefined.bin","rb") sd <- readBin(conne, numeric(), size=4, n=1440*720, signed=TRUE) y <- t(matrix((data=sd), ncol=1440, nrow=720)) r <- raster(y) f <- hist(y, breaks=10,main="sm") f$counts <- f$counts/sum(f$counts) dat <- data.frame(counts= f$counts,breaks = f$mids) ggplot(dat, aes(x = breaks, y = counts, fill =counts)) + geom_bar(stat = "identity",alpha = 0.8) + xlab("Pearson correlation")+ ylab("Frequency") + scale_x_continuous(breaks = seq(-1,1,0.250), labels = seq(-1,1,0.250)) + ggtitle("2011") + theme(axis.title.x = element_text(size = 20)) + theme(axis.title.y = element_text(size = 20)) + theme(text = element_text(size=20), axis.text.x = element_text(angle = 90, vjust=1,colour="black"), axis.text.y = element_text(colour="black")) + theme(plot.title = element_text(size = rel(2.5))) + scale_fill_gradientn(colours = "black")
-
SabreWolfy over 10 yearsWhy does
ggplot2
include the space below the y-axis value of 0, when there are no data points below 0?
-
-
James about 11 yearsBeware: Setting limits on a scale may exclude data.
-
Simon O'Hanlon about 11 years@James indeed - I missed that the OP only wanted the y axis. My mistake. It's still valid to use
scale_y_continuous
in this case because its a histogram so values will be bounded between 0 and 1. -
MERose over 9 yearsWhy not simply
ylim()
? -
Mona Jalal over 7 yearsError in +coord_cartesian(ylim = c(0.005, 0.02)) : invalid argument to unary operator
-
James over 7 years@MonaJalal It needs to be added to a ggplot object, not using in isolation. It was intended to the added to the OPs original plot specification.