Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a y axis to minimum and maximum range in R?

Tags:

If you look at the graph below (y axis), you will notice that the scale is from 0 to 0.20. I have other histograms where the range is from 0 to 0.4. I want to make all of them consistent from 0 to 1 and display the y axis from 0 to 1.

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") 
like image 211
Barry Avatar asked Feb 19 '13 15:02

Barry


People also ask

How do you set the Y axis range in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

What is YLIM R?

ylim: Convenience function to set the limits of the y axis.


2 Answers

Just add:

+ coord_cartesian(ylim=c(0,1)) 
like image 85
James Avatar answered Oct 19 '22 10:10

James


Try this:

scale_y_continuous( limits = c(0,1), expand = c(0,0) ) 
like image 24
Simon O'Hanlon Avatar answered Oct 19 '22 12:10

Simon O'Hanlon