R Ggplot Boxplot: Change Y-Axis Limit

R ggplot boxplot: change y-axis limit

Use coord_cartesian instead of scale_y_continuous:

ggplot(df, aes(x=Effect2, y=OddsRatioEst)) +
geom_boxplot(outlier.colour=NA) +
coord_cartesian(ylim = c(0, 100))

From the coord_cartesian documentation:

Setting limits on the coordinate system will zoom the plot (like
you're looking at it with a magnifying glass), and will not change the
underlying data like setting limits on a scale will.

The output is as follows. As you can see, not deleting those outliers changes the picture somewhat, so you may want to change the y limit as well.
Sample Image

Limiting the range of the y axis in a boxplot in ggplot without affecting the data on which the boxplot is based

Instead of your scale_y_continuous() code, use coord_cartesian() as follows.

This

scale_y_continuous(breaks =seq(0, .05, .01), limit = c(0, 0.05))

replace with this

coord_cartesian(ylim = c(0,0.05))

Also noticed that you are trying to present mean. Note that boxplot show median not mean. Maybe something that you should keep in mind for your data presentation. Also BoxPlots are usually preferred over other option because it shows data distribution (for example outliers) and other important statistics for comparison. Cropping only to show median, hence may not be a good idea, rather you could just only show median using geom_point().

How to change the limits from scale_y_continuous depending on the plot in R?

Thanks to @teunbrand and @caldwellst I got the solution that I needed it.

There are 3 solutions that work perfectly:

1-

p + scale_y_continuous(limits = function(x){
c(min(x), (max(x)+0.1))
})
p

2-

library(tidyverse)

p + scale_y_continuous(limits = ~ c(min(.x), max(.x) + 0.1))

3-

p + scale_y_continuous(limits = function(x){
c(min(x), ceiling(max(x) * 1.1))
})

How to get geom_boxplot to apply y-limits before calculating boxes?

From joran's comment, this works:

qplot(cyl, mpg, data=mtcars) +
geom_boxplot() +
coord_cartesian(ylim=c(0,20))

From the docs:

There are two ways of zooming the plot display: with scales or with
coordinate systems. They work in two rather different ways.

Setting the limits on a scale will throw away all data that's not
inside these limits. This is equivalent to plotting a subset of the
original data

Setting the limits on the coordinate system performs a visual zoom
the data is unchanged, and we just view a small portion of the
original plot.

Thanks joran!

How to set Boxplot Y axis limits in millions

The easy way

set.seed(42)
x <- rlnorm(100, 12, 2)
boxplot(x/1e6, ylab="Amount in Millions")

Boxplot

If you want more control over the axis labels, create your own axis:

boxplot(x, yaxt="n", ylab="Amount in Millions")
axis(2, c(0, 5e6, 1e7, 1.5e7), c("0", "5", "10", "15"), las=1)

How to change y-axis scale in R boxplot function

library(datasets)
boxplot(cars[c('speed', 'dist')], col = "lightgray", ylim = range(0:120), yaxs = "i")
axis(4, at=seq(0, 120, 10))

Sample Image

The y-axis is on the right-hand side as you wanted I believe.

How to change Y axis without excluding data (outliers not shown but accounted for median)?

Yes, this is not intuitive behavior (at least it gives a warning message).... You need to use coord_cartesian to simply zoom in without changing the data.
Cf.

ggplot(mtcars, aes(x = cyl, y = mpg)) + 
+ geom_boxplot(aes(fill= cyl, group=cyl), colour = "black", outlier.shape = NA, show.legend = FALSE) +
+ coord_cartesian(ylim=c(0,25))

vs.

ggplot(mtcars, aes(x = cyl, y = mpg)) + 
+ geom_boxplot(aes(fill= cyl, group=cyl), colour = "black", outlier.shape = NA, show.legend = FALSE) +
+ scale_y_continuous(limits=c(0,25))

Order Y-axis ggplot boxplot based on dataframe column

Adding scale_x_discrete(limits = rev(c('PAE', 'PAR', 'BER', 'NY', 'AMS'))) would do the trick.


Is this what you are looking for:

# Create ggplot object
bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots

# Create boxplot figure
bp +
geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
coord_flip(ylim = c(0, 25)) +
geom_hline(yintercept = 4) +
ggtitle("Time Difference") +
ylab("Time Difference") +
xlab("City") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(), #remove all border lines
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) + #add y-axis border line
scale_x_discrete(limits = rev(c('PAE', 'PAR', 'BER', 'NY', 'AMS')))

Sample Image

How to set the y range in boxplot graph?

You can simply put an ylim = c(0, 5) in all your boxplot() call. This sets y-axis range (roughly) between 0 and 5.

Perhaps you did not see ylim argument in ?boxplot; the "Arguments" section also does not mention it. But ylim is just a trivial graphical parameter passed via "...". You can also find such example in the "Examples" session of ?boxplot:

 boxplot(len ~ dose, data = ToothGrowth,
boxwex = 0.25, at = 1:3 - 0.2,
subset = supp == "VC", col = "yellow",
main = "Guinea Pigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length",
xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")


Related Topics



Leave a reply



Submit