0

I want to create overlaped density plot. I decided to use ggplot2.

My data are in data frame formate. Here How they are look:

Ge<-data.frame(Ge)
dim(Ge)
#[1] 100   1
Ge[1:4,]
#[1]   6.005409  38.681342 102.079283 185.672611
dim(Tr)
#[1] 100   1
Tr[1:4,]
#[1] 12.8678547  1.3034715  1.1372413  0.7973491

Here is my code to create plot:

library(ggplot2)

ggplot() + geom_density(aes(x=x), colour="red", data=Tr) + 
  geom_density(aes(x=x), colour="blue", data=Ge)

But this is the error I get it:

    Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error: stat_density requires the following missing aesthetics: x

Would someone help me to solve this ?

1
  • Is x the name of the column in each data frame? Try names names(Ge) and names(Tr). In my example I just directly referred to the column that I wanted, Ge[,1] and that worked.
    – scs217
    Jun 12, 2014 at 23:32

1 Answer 1

0

You should be using a single data frame where ever possible with ggplot. That is the logic behind the syntax, but is unintuitive at first. Considering your sample code, Tr and Ge are factors and there is one set of values which you're representing on a common x-axis.

The reshape2 package has a handy tool for combining separate data into a format suitable for ggplot plotting, melt. Check out the package documentation, but see below for working code and a sample output.

require(ggplot2)
require(reshape2)
Ge=runif(n=100)
Tr=runif(n=100)
data=data.frame(Ge,Tr)
names(data)=c('Ge','Tr')
data=melt(data,id.vars=NULL)
ggplot(data,aes(x=value,fill=variable))+geom_density(alpha=.4)

There is a book by Hadley Wickham which covers all of this information in excellent detail. Amazon link

Update I have more closely replicated the OP's code (straying away from best practices) and still get a functional plot, though with a warning.

Ge=data.frame(runif(n=100))
Tr=data.frame(runif(n=120))

ggplot()+geom_density(aes(data=Ge,x=Ge[,1]),color='red')+
  geom_density(aes(data=Tr,x=Tr[,1]),color='blue')

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous

5
  • But how is the Ge and Tr have different length ? Jun 12, 2014 at 13:52
  • Can you update your question with a reproducible example of what Ge and Tr are? Have you tried the plot with only Tr and then only Ge? How do those tests work out?
    – scs217
    Jun 12, 2014 at 17:01
  • 1
    I updated my answer testing your code and it works for me. Perhaps there are some issues with your data frames. Are any of the values NA or of a different type than numeric? Try summary(Ge) and summary(Tr).
    – scs217
    Jun 12, 2014 at 18:29
  • I checked, there is no NA, but the values are in the format of : 3.848143e-01 , ..... Jun 12, 2014 at 18:44
  • Test out plotting only Tr and then only Ge. Let me know how those work. Also, does my original post fix your problem?
    – scs217
    Jun 12, 2014 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.