Set Only Lower Bound of a Limit for Ggplot

set only lower bound of a limit for ggplot

You can use expand_limits

ggplot(mtcars, aes(wt, mpg)) + geom_point() + expand_limits(y=0)

Here is a comparison of the two:

  • without expand_limits

Sample Image

  • with expand_limits

Sample Image

As of version 1.0.0 of ggplot2, you can specify only one limit and have the other be as it would be normally determined by setting that second limit to NA. This approach will allow for both expansion and truncation of the axis range.

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
scale_y_continuous(limits = c(0, NA))

Sample Image

specifying it via ylim(c(0, NA)) gives an identical figure.

How to set just one limit for axes in ggplot2 with facets?

Set limits one-sided with NA. Works both in coord_ and scale_ functions

I generally prefer coord_ because it does not remove data. For the example below you would additionally need to remove the margin at 0, e.g. with expand.

library(ggplot2)    

carrots <- data.frame(length = rnorm(500000, 10000, 10000))
cukes <- data.frame(length = rnorm(50000, 10000, 20000))
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
vegLengths <- rbind(carrots, cukes)

ggplot(vegLengths, aes(length, fill = veg)) +
geom_density(alpha = 0.2) +
scale_x_continuous(limits = c(0, NA))
#> Warning: Removed 94542 rows containing non-finite values (stat_density).

Sample Image


ggplot(vegLengths, aes(length, fill = veg)) +
geom_density(alpha = 0.2) +
coord_cartesian(xlim = c(0, NA))

Sample Image

Created on 2020-04-30 by the reprex package (v0.3.0)

remove the margin with expand. Also one sided possible. the right margin is set to the default mult expansion of 0.05 for continous axis.

ggplot(vegLengths, aes(length, fill = veg)) +
geom_density(alpha = 0.2) +
scale_x_continuous(expand = expansion(mult = c(0, 0.05))) +
coord_cartesian(xlim = c(0, NA))

Sample Image

Graph bars only appear when lower limit of y axis set to 0 in ggplot

Looks like you're losing data by setting limits and it's screwing up your plot. You can use coord_cartesian() instead of ylim() to 'zoom in' on your data; see https://stackoverflow.com/a/25685952/12957340 and/or page 160 of the ggplot2 book for further info.

full %>%
ggplot(aes(x = order, y = mean)) +
geom_bar(stat = "identity", fill = "003900", width = 0.5, position = position_dodge()) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
geom_text(aes(label = round(mean, digits =1)), position = position_dodge(width=1.0), vjust = -4.0, size = 3) +
#facet_wrap(~names) +
labs(title = "Behavioral intentions in response to each message") +
coord_cartesian(ylim = c(1, 7)) +
theme(axis.text = element_text(size = 7)) +
xlab("Message") +
ylab("Behavioral intentions")

example.png

Set only lower limits in ggplot2 scale_x_datetime(limits)

As aosmith said,

You can't omit the upper limit, but you can use NA for one of the limits that you want automatically calculated. See the help page for ggplot2::ylim

Thank you!

Can I limit ggplot axis range EXACTLY?

As Axeman noted, specifying limits in scale_y_continuous() would limit the y-axis. Another alternative is to add coord_cartesian(ylim = c(20, 73)) to your code.

The difference between the two (irrelevant for geom_point) is that scale_y_continuous(limits = c(...)) would restrict the range of values passed to ggplot for plotting, while coord_cartesian(ylim = c(...)) retricts the visible range after everything has been plotted.

The ggplot2 package cheatsheet over at RStudio summarises this nicely:

illustration

As for your issue with some points being cut off at the edge of the panel, you can get around it by converting the plot to a grob, & turning off the panel's clipping:

# plot with restricted range
p <- ggplot(data = mydata, aes(x = x, y = y)) +
geom_point(shape = 21, size = 5, color = "black", fill = "orange", stroke = 1) +
scale_x_continuous(name = 'Number of Contacts') +
scale_y_continuous(name = "Remembered (%)",
expand = c(0, 0),
sec.axis = sec_axis(trans = ~ (.-min(.))*100/(max(.)-min(.)),
name = "Remembered (Index)",
breaks = break2)) +
coord_cartesian(ylim = c(20, 73)) +
theme(plot.margin = margin(t = 10, unit = "pt")) # increase top margin as some points
# are very close to the top edge

# convert to grob
gp <- ggplotGrob(p)

grid::grid.draw(gp) # verify that the points at the edge are cut off

# turn off clipping for panel
gp$layout$clip[gp$layout$name=="panel"] <- "off"

grid::grid.draw(gp) # points at the edge are now visible

plot

create custom ggplot function and set limit for y axis dynamically

You can't use a string variable to slice a dataframe with $ which is what you are implicitly trying to do with max(data$var_y). Instead of looking for a column with name of the string you pass to var_y it's looking for a column literally named "var_y"

Replace max(data$var_y) with max(data[,var_y]) and it should work.

Edit

The way I would fix this is to make everything explicitly a string and use aes_string to define your ggplot aesthetic with strings. Constructions like the var_x = month, var_y = n in your function call are dangerous because they will change behavior if you ever create a variable called month or n and you want to use them to refer to dataframe columns. Full solution looks like this:

horizontal_bar_chart_2 <- function(data, var_x, var_y){
limit_for_y_axis = max(data[,var_y]) * 1.3
ggplot(data, aes_string(x=var_x, y=var_y)) +
geom_bar(stat = "identity") +
coord_flip() +
geom_text(aes_string(label=var_y), hjust = -.3, color = "gray35") +
scale_y_continuous(expand = c(0, 0),
limits = c(0, limit_for_y_axis)
)

}

# test of horizontal_bar_chart_2
horizontal_bar_chart_2(data = dt, var_x = "month", var_y = "n")


Related Topics



Leave a reply



Submit