Why are these alternatives producing different scaled graphs

26 views
Skip to first unread message

Graham Smith

unread,
Sep 7, 2010, 1:50:13 AM9/7/10
to ggp...@googlegroups.com
I have drawn what I think should be the same density plots

1.

> p45Density<-ggplot(allbats,aes(x=allbats$count[allbats$species == "p45"]))
> print(p45Density + geom_density()+scale_x_continuous("Density of counts"))
Warning message:
Removed 9 rows containing missing values (stat_density).

2

> qplot(allbats$count[allbats$species == "p45"],geom="density", xlab="Density of Counts")
Warning message:
Removed 1 rows containing missing values (stat_density).

The former has a peak value of about 10-04, the latter 8-04

is the the first option removing all the rows with NAs even when there
isn't an NA in the variable I am interested in (p45), and the second
only removing the NA from the p45 variable

If this is the case, I wonder if someone could help with correct
syntax for ggplot2, as the qplot option is correct.

Many thanks,

Graham

Brandon Hurr

unread,
Sep 7, 2010, 3:28:56 AM9/7/10
to Graham Smith, ggp...@googlegroups.com
Graham, 

I can't say I can tell what the problem is off the top of my head, but it seems like it's a problem specific to your dataset and how qplot() and ggplot() handle NAs. 

Something you could try is subsetting your data frame before handing it to qplot()/ggplot() and see if that changes anything. 

allbats.p45<-subset(allbats, species == "p45")

ggplot(allbats.p45 ,aes(x=allbats.p45$count)
+ geom_density()
+scale_x_continuous("Density of counts"))

#quartz() or windows() or x11() to open a new window for comparison's sake

qplot(allbats.p45$count
,geom="density"
,xlab="Density of Counts")

It'd be a lot easier to figure it out if we also had your dataset. If it's not too large use dput() and put it in this email thread or if it's larger dput() it into a text file or something (gmail cuts things off after a thousand or so lines). Change the variables and names if the data is sensitive. 

Brandon


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

Graham Smith

unread,
Sep 7, 2010, 3:45:05 AM9/7/10
to Brandon Hurr, ggp...@googlegroups.com
Brendon,

Thanks for the quick reply, I have attached the data set, without a
context I don't think its that sensitive.

In fact I get an error with your suggestion.

> ggplot(allbats.p45 ,aes(x=allbats.p45$count)
+ + geom_density()
+ +scale_x_continuous("Density of counts"))
Error in aes(x = allbats.p45$count) + geom_density() :
non-numeric argument to binary operator

BUT the qplot version works fine, but it still seems to suggest its
related to NAs.

> qplot(allbats.p45$count
+ ,geom="density"
+ ,xlab="Density of Counts")


Warning message:
Removed 1 rows containing missing values (stat_density).

Graham

jbatsall.txt

Brandon Hurr

unread,
Sep 7, 2010, 4:27:14 AM9/7/10
to Graham Smith, ggp...@googlegroups.com
That's because I needed coffee I think. A slight error with a parentheses and placement of the '+'s.

allbats.p45<-subset(allbats, species == "p45")

ggplot(allbats.p45 , aes(x=allbats.p45$count))+
geom_density()+
scale_x_continuous("Density of counts")

Both of them give me the same error and graph so I'll go back and look at your previous program... 

B

2010/9/7 Graham Smith <myot...@gmail.com>
ggplotoutput.png
qplotoutput.png

Brandon Hurr

unread,
Sep 7, 2010, 4:33:29 AM9/7/10
to Graham Smith, ggp...@googlegroups.com
Graham, 

Not sure what's going on, but I cannot reproduce your original error. 

> p45Density<-ggplot(allbats,aes(x=allbats$count[allbats$species == "p45"]))
> print(p45Density + geom_density()+scale_x_continuous("Density of counts"))
Warning message:
Removed 1 rows containing missing values (stat_density). 

And it gives me the same graph as above. 

Brandon

Graham Smith

unread,
Sep 7, 2010, 4:34:40 AM9/7/10
to ggp...@googlegroups.com
Brandon,

> Both of them give me the same error and graph so I'll go back and look at
> your previous program...

And they both work fine here as well :-)

I look forward to your feedback on my code, as I am obviously doing
something wrong.

Thanks,

Graham

Dennis Murphy

unread,
Sep 7, 2010, 4:39:04 AM9/7/10
to Graham Smith, Brandon Hurr, ggp...@googlegroups.com
Hi:

Thank you for the data; it helps tremendously. There are nine records with missing counts in the entire data set:

> subset(batsall, is.na(count))
    site year habitat area species count
446   14 2009    Wood   SW     p45    NA
470   14 2009    Wood   SW     p55    NA
494   14 2009    Wood   SW    Kuhl    NA
518   14 2009    Wood   SW    Nath    NA
542   14 2009    Wood   SW    uPip    NA
566   14 2009    Wood   SW    natt    NA
590   14 2009    Wood   SW     leb    NA
614   14 2009    Wood   SW    sero    NA
638   14 2009    Wood   SW    leis    NA

The first is in the selected species p45. I did the following:

batsp45 <- subset(batsall, species == 'p45')
> subset(batsp45, is.na(count))
    site year habitat area species count
446   14 2009    Wood   SW     p45    NA
> dim(batsp45)
[1] 72  6

The following selects out the one record in batsp45 with a missing count:

g <- ggplot(subset(batsp45, !is.na(count)), aes(x = count))
g + geom_density(fill = 'green') + scale_x_continuous('Density of counts')

There are no error messages about missing values. The qplot() version is

qplot(count, data = subset(batsp45, !is.na(count)),
       geom = 'density',
       xlab = 'Density of counts')

They look the same to me (except for color :)  A lot like Brandon's, actually...

HTH,
Dennis

2010/9/7 Graham Smith <myot...@gmail.com>
Graham

Graham Smith

unread,
Sep 7, 2010, 4:48:19 AM9/7/10
to ggp...@googlegroups.com
How strange is that,

I have just tried again, but copying and pasting the code in your email

> p45Density<-ggplot(allbats,aes(x=allbats$count[allbats$species=="p45"]))


> print(p45Density + geom_density()+scale_x_continuous("Density of counts"))
Warning message:

Removed 9 rows containing missing values (stat_density).

And I get the graph attached. I am on a Mac with Tiger which means I
am still running R 2.10.0 or am I ? Running R from ESS tells me its,
2.10.0 but running from the terminal tells me its R 2.9.2. Not sure
if this is relevant to this particular problem, but I probably need to
sort this out.

It does seem the problem is local to me, and obviously there is
something odd going on here.

Thanks again,

Graham

Rplot.pdf

Brandon Hurr

unread,
Sep 7, 2010, 4:52:27 AM9/7/10
to Graham Smith, ggp...@googlegroups.com
Strange indeed, but explainable thanks to Dennis. There are 9 NAs total in your dataset, which makes it pretty clear that it's not properly taking the subset of the data when it's plotting. Why... I'm not sure since the code is solid. 

Upgrade R and ggplot2 to the newest versions and see if the error persists. 

Brandon


Graham

Graham Smith

unread,
Sep 7, 2010, 5:01:19 AM9/7/10
to ggp...@googlegroups.com
Denis,

Thanks for this, its really useful, to see a slight twist on the
approach and to confirm my issues seem to be local ones.

Graham

Dennis Murphy

unread,
Sep 7, 2010, 5:03:12 AM9/7/10
to Graham Smith, ggp...@googlegroups.com
Hi Graham:

Try to simplify the subsetting as I showed in my post. It's easier to follow, and it really, really works :) If that doesn't work, try the upgrades as Brandon suggested and try again. If ggplot() is removing 9 rows, it's using the entire data frame, not the p45 subset.

Here's the culprit:


p45Density<-ggplot(allbats, aes(x=allbats$count[allbats$species=="p45"]))
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You're asking ggplot to read in the entire allbats data frame, but then subsetting x. In fact, it's the other way around: subset the data frame and then select the variable.

Hopefully, this solves the mystery :)

Dennis


Graham

Brandon Hurr

unread,
Sep 7, 2010, 5:07:40 AM9/7/10
to Dennis Murphy, Graham Smith, ggp...@googlegroups.com
Scooby Doo would approve. 

Graham Smith

unread,
Sep 7, 2010, 6:46:52 AM9/7/10
to ggp...@googlegroups.com
Denis

> Here's the culprit:
>
> p45Density<-ggplot(allbats, aes(x=allbats$count[allbats$species=="p45"]))
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> You're asking ggplot to read in the entire allbats data frame, but then
> subsetting x. In fact, it's the other way around: subset the data frame and
> then select the variable.

Ah, thanks. I can see that now :-)


Graham

Reply all
Reply to author
Forward
0 new messages