My data range always seems to be greater than the top label in the y-axis. Is there a way I can automatically include it without manually setting limits?
e.g. in
ggplot(data.frame(x=1:11, y=c(11:17,5:2)), aes(x,y)) + geom_point()
I would like to have labels 0 and 20 appear on the y-axis and o and 12 appear on the x-axis (and the plot expand accordingly). This should be done automatically without specifying these numbers in scale options for example.
To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .
In case you want to omit the use of exponential formatting on one of the axes in a ggplot: Add scale_*_continuous(labels = scales::comma) with * being replaced by the axis you want to change (e.g. scale_x_continuous() ).
Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape. They also provide the tools that let you interpret the plot: the axes and legends.
This can be done easily using the ggplot2 functions scale_x_continuous() and scale_y_continuous(), which make it possible to set log2 or log10 axis scale. An other possibility is the function scale_x_log10() and scale_y_log10(), which transform, respectively, the x and y axis scales into a log scale: base 10.
What about the following solution:
library(ggplot2)
d <- data.frame(x=1:11, y=c(11:17,5:2))
px <- pretty(d$x)
py <- pretty(d$y)
ggplot(d, aes(x,y)) + geom_point() +
scale_x_continuous(breaks=px, limits=range(px)) +
scale_y_continuous(breaks=py, limits=range(px))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With