Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set upper limit in ggplot to include label greater than the maximum value

Tags:

r

ggplot2

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.

enter image description here

like image 472
Tom Avatar asked Jul 11 '12 07:07

Tom


People also ask

Which arguments can be used to add labels in Ggplot?

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 .

How do I stop Ggplot from scientific notation?

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() ).

What are scales in Ggplot?

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.

How do I change the Y axis to log scales ggplot2?

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.


1 Answers

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))

enter image description here

like image 191
Stuples Avatar answered Oct 15 '22 08:10

Stuples