margins of axis limits

1,273 views
Skip to first unread message

tiannan

unread,
Feb 24, 2011, 10:02:44 PM2/24/11
to ggplot2
hi group,

When we set the limits of an axis, for example,
scale_y_continuous(limits=c(1,100)). The axis is annotated with ticks
1, ...100. However, in practice, there are margins below 1, and above
100. The margins look weird when I set the plot background as white
and grids as grey. Is there any command to remove the margins? I want
to show the axis exactly from 1~100. Thanks!

Best regards,
Tiannan

Dennis Murphy

unread,
Feb 25, 2011, 3:31:49 AM2/25/11
to tiannan, ggplot2
Hi:

Use expand = c(0, 0) in scale_y_continuous()...but you need to be careful, as the following example shows.

# Code that yielded the data below
# d <- data.frame(x = 1:50, y = 1 + 2 * (1:50) + rnorm(50))

d <- structure(list(x = 1:50, y = c(2.22632311995742, 4.20930208812107,
7.69257133173181, 11.4678820426463, 11.3889228872281, 12.964789666021,
14.9892838863793, 16.2579057517061, 20.3697428126379, 19.7722455882214,
23.2962197550043, 25.2820819179160, 24.8895517763587, 29.0665792968226,
30.8196379204549, 33.8473887803161, 35.5028627005978, 39.4445523401192,
39.690849402195, 43.2145274867637, 42.8410118799909, 44.5864427729729,
47.1313483767006, 48.1310295474914, 50.0732779511282, 51.0482220944363,
55.1249583302253, 57.0684756428575, 60.184254246046, 59.8336192477496,
62.6972156781126, 64.6973171194804, 67.7917822920305, 69.5302184880986,
70.6796916043978, 73.548634673699, 74.715812785803, 76.7094404183777,
77.3055555089025, 79.8326412990929, 82.2005648765955, 83.455876938435,
87.266264854227, 88.123819928451, 89.1637923282434, 92.1738365476067,
94.7135682848657, 97.1203279278892, 97.7393565673663, 101.610960107591
)), .Names = c("x", "y"), row.names = c(NA, -50L), class = "data.frame")

ggplot(d, aes(x, y)) + geom_point() +
     scale_x_continuous(expand = c(0, 0)) +
     scale_y_continuous(expand = c(0, 0))

ggplot(d, aes(x, y)) + geom_point() +
     scale_x_continuous(limits = c(0, 50), expand = c(0, 0)) +
     scale_y_continuous(limits = c(0, 100), expand = c(0, 0))

Look at the points in the lower left and upper right corners of the first plot. As long as you have data that are contained with the plot boundaries you set, there should be no problem, but points close to or on the boundaries will get clipped when you remove the axis padding. Notice that the observation at x = 50 is slightly above 100; the first plot removes the padding but the point is still present, although clipped. In the second plot, since the y-limits are fixed, the point at x = 50 is removed and you get a warning.

HTH,
Dennis



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

tiannan guo

unread,
Feb 25, 2011, 8:44:38 AM2/25/11
to Dennis Murphy, ggplot2
Thanks Dennis, it works!

cheers,
Tiannan
Reply all
Reply to author
Forward
0 new messages