[R] ggplot questions
Thompson, David (MNR)
David.John.Thompson at ontario.ca
Fri Jun 6 21:11:13 CEST 2008
OK,
The original ggplot() construct (below) on the following two dataframes
(test1, test2) generate different outputs, which I have attached. The
output that I expect is that shown in test2.png. My expectations are
that I have set the plotting limits with 'scale_x_continuous(lim = c(0,
360)) + scale_y_continuous(lim = c(0, 16))' so, both data sets should
produce the same output except for the 'o' at plot center and the 'N' at
the top. The only difference in the two dataframes are inclusion of
first two rows in test2 with rplt column changed to character:
> test2[1:2,]
oplt rplt az dist
1 0 o 0 0
2 0 N 360 16
Ahhh, wait a second! In composing this message I may have found the
problem.
It appears that including the 'scale_x_continuous()' component twice in
my original version was causing (?) the erratic behaviour. And I have
confirmed that the ordering of the layer, scale* and coord* components
does not affect the output. However, I'm still getting more x-breaks
than requested with radial lines corresponding to 45, 135, 225, 315
degrees (NE, SE, SW, NW). Still open to suggestions on that.
# new version working with both dataframes
ggplot() + coord_polar() +
layer( data = test1, mapping = aes(x = az, y = dist, label = rplt), geom
= "text") +
scale_x_continuous(lim = c(0, 360), breaks=c(90, 180, 270, 360),
labels=c('E', 'S', 'W', 'N')) +
scale_y_continuous(lim = c(0, 16), breaks=c(0, 4, 8, 12, 16),
labels=c('centre', '4m', '8m', '12m', '16m'))
######
######
######
# original version NOT WORKING with test1
ggplot() + coord_polar() + scale_x_continuous(lim = c(0, 360)) +
scale_y_continuous(lim = c(0, 16)) +
layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom
= "text") +
scale_x_continuous(breaks=c(90, 180, 270, 360), labels=c('90', '180',
'270', '360'))
# data generating test1.png
test1 <-structure(list(oplt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), rplt = 1:10, az = c(57L, 94L, 96L, 152L, 182L, 185L, 227L,
264L, 332L, 354L), dist = c(4.09, 2.8, 7.08, 7.09, 3.28, 7.85,
6.12, 1.97, 7.68, 7.9)), .Names = c("oplt", "rplt", "az", "dist"
), row.names = c(NA, 10L), class = "data.frame")
# data generating test2.png
test2 <- structure(list(oplt = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
rplt = c("o", "N", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10"), az = c(0, 360, 57, 94, 96, 152, 182, 185, 227,
264, 332, 354), dist = c(0, 16, 4.09, 2.8, 7.08, 7.09, 3.28,
7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c("oplt", "rplt",
"az", "dist"), row.names = c(NA, 12L), class = "data.frame")
Many, many thanks for your patience and perseverance on this one Hadley,
DaveT.
>-----Original Message-----
>From: hadley wickham [mailto:h.wickham at gmail.com]
>Sent: June 6, 2008 02:06 PM
>To: Thompson, David (MNR)
>Cc: r-help at r-project.org
>Subject: Re: [R] ggplot questions
>
>> Does the difference have something to do with ggplot() using ranges
>> derived from the data?
>> When I modify my original 'test' dataframe with two extra rows as
>> defined below, I get expected results in both versions.
>
>Order shouldn't matter - and if it's making a difference, that's a
>bug. But I'm still not completely sure what you're expecting.
>
>> This highlights my next question (warned you ;-) ), I have been
>> unsuccessful in trying to define fixed plotting ranges to generate a
>> 'template' graphic that I may reuse with successive 'overstory plot'
>> data sets. I have used '+ xlim(0, 360) + ylim(0, 16)' but,
>this seems to
>> not have any effect on the final plot layout.
>
>Could you please produce a small reproducible example that
>demonstrates this? It may well be a bug.
>
>Hadley
>
>--
>http://had.co.nz/
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test1.png
Type: image/png
Size: 9710 bytes
Desc: test1.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080606/02dbba07/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test2.png
Type: image/png
Size: 9306 bytes
Desc: test2.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080606/02dbba07/attachment-0001.png>
More information about the R-help
mailing list