i plot following:
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) now want plot region between x=-5000 5000, instead of entire range.
how can that?
basically have 2 options
scale_x_continuous(limits = c(-5000, 5000)) or
coord_cartesian(xlim = c(-5000, 5000)) where first removes data points outside given range , second adjusts visible area. in cases not see difference, if fit data change fitted values.
you can use shorthand function xlim (or ylim), first option removes data points outside of given range:
+ xlim(-5000, 5000) for more information check description of coord_cartesian.
the rstudio cheatsheet ggplot2 makes quite clear visually. here small section of cheatsheet:
distributed under cc by.

Comments
Post a Comment