Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: ggplot set ylim with custom break

Tags:

r

ggplot2

I want to customize the gap (breaks) in the y axis of the plot. I tried two options.

option1: ylim(0.0,0.6) 
option2: scale_y_continuous(breaks=seq(0.0, 0.6, 0.1))

Problem with the option1 is that it breaks in every 0.2 up to limit of y axis which is 0.6. Example of problem1 Problem with the option2 is that it gives this illusion of drastic difference between plots since it enlarge the 0.1 segment of the plot. Example of problem2

What I want is y-axis to be break at every 0.1 or customize it to any break while showing the max limit of the y-axix (in this case 0.0 to 0.6 but break every 0.1).

xVal = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
a = c(0.18340368127959822, 0.17496531617798133, 0.16772886654445848, 0.15934821062512169, 0.15390913489444036, 0.14578798884106348, 0.14524174121702108, 0.13958093302847951, 0.1365009715515553, 0.13337340345559975, 0.12995175856952607, 0.12583603207983862, 0.12180656145228715, 0.11824179486798418, 0.11524630600365712)
b = c(0.13544353787855531, 0.11345498050033079, 0.11449834060237293, 0.10479213576778054, 0.09677430524414686, 0.091990671548439179, 0.089965934807318487, 0.088711600335474206, 0.088923403079789909, 0.087989321310275717, 0.085424600757017272, 0.08251334730889931, 0.080178280060313953, 0.077717041621392688, 0.076638743116633837)
c = c(0.087351324973658093, 0.12113308515702567, 0.11422800742900453, 0.11264309199970789, 0.11390287790920843, 0.10774426268894192, 0.10587704437111881, 0.10474954948318291, 0.10568277685778472, 0.10201545270338952, 0.09939827283775747, 0.098062403381144761, 0.094110034623398231, 0.091211408116407641, 0.089369778116029489)

library(ggplot2)
library(reshape2)

df = data.frame(xVal, a, b, c)
df.melt = melt(df, id="xVal")

problem1:

ggplot(data=df.melt, aes(x=xVal, y=value, colour=variable)) +
            geom_point() + 
            geom_line() + 
            xlab("xVal") + ylab("YValues") + xlim(1,16) +  
            ylim(0.0,0.6)

problem2:

ggplot(data=df.melt, aes(x=xVal, y=value, colour=variable)) + 
            geom_point() + 
            geom_line() + 
            xlab("xVal") + ylab("YValues") + xlim(1,16) + 
            scale_y_continuous(breaks=seq(0.0, 0.6, 0.1))

How do I customize the y-axis such that break according to a value that I specify.

like image 219
add-semi-colons Avatar asked May 26 '15 14:05

add-semi-colons


People also ask

How do I add a break in ggplot2?

To add axis breaks in ggplot2 plots in R, we use scale_x_break() and scale_y_break() functions. These functions take a vector as a parameter that has breakpoints. If we need multiple breakpoints we can add those too.

How do I change the Y axis scale in ggplot2?

Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.

What is YLIM R?

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


1 Answers

See the below. You need to set both the breaks AND the limits. Otherwise you may not like the breaks chosen or it may zoom in on the data and not show some of your breaks.

library(ggplot2)
library(reshape2)

xVal = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
a = c(0.18340368127959822, 0.17496531617798133, 0.16772886654445848, 0.15934821062512169, 0.15390913489444036, 0.14578798884106348, 0.14524174121702108, 0.13958093302847951, 0.1365009715515553, 0.13337340345559975, 0.12995175856952607, 0.12583603207983862, 0.12180656145228715, 0.11824179486798418, 0.11524630600365712)
b = c(0.13544353787855531, 0.11345498050033079, 0.11449834060237293, 0.10479213576778054, 0.09677430524414686, 0.091990671548439179, 0.089965934807318487, 0.088711600335474206, 0.088923403079789909, 0.087989321310275717, 0.085424600757017272, 0.08251334730889931, 0.080178280060313953, 0.077717041621392688, 0.076638743116633837)
c = c(0.087351324973658093, 0.12113308515702567, 0.11422800742900453, 0.11264309199970789, 0.11390287790920843, 0.10774426268894192, 0.10587704437111881, 0.10474954948318291, 0.10568277685778472, 0.10201545270338952, 0.09939827283775747, 0.098062403381144761, 0.094110034623398231, 0.091211408116407641, 0.089369778116029489)

df = data.frame(xVal, a, b, c)
df.melt = melt(df, id="xVal")

ggplot(data=df.melt,
  aes(x=xVal, y=value, colour=variable)) + geom_point() +
  geom_line() + xlab("xVal") + ylab("YValues") + xlim(1,16) + scale_y_continuous(breaks=seq(0.0, 0.6, 0.1), limits=c(0, 0.6))
like image 97
jrdnmdhl Avatar answered Nov 05 '22 09:11

jrdnmdhl