3

I'm trying to create a single graph with 2 superimposed smooth density plots of my numeric_var stratified for my factor_var, using Rstudio R version 3.5.1 on Mac. I always get the same error:

"Aesthetics must be either length 1 or the same as the data (): x, fill" 

I've tried multiple approaches (one example below). I've also followed this guide - STHDA, removed NA and checked other questions regarding this error but I can't manage to get this working. This is what I'm trying to get:

image

Help please? (I'm a newbie, first question, please be kind :) )

data

mydata <- fulldata %>%
    select(numeric_var,factor_var) %>%
    filter(factor_var== 0 | factor_var== 1) 
head(mydata)

   numeric_var factor_var
1          0.6          0
2          0.7          0
3          0.7          1
4          0.9          0
5          0.6          1
6          0.7          0

plot code

ggplot(mydata, aes(x = numeric_var, fill = factor_var)) +
    geom_density(alpha = 0.5)

error:

Aesthetics must be either length 1 or the same as the data (598): x, fill
divibisan
  • 11,659
  • 11
  • 40
  • 58
Mia
  • 95
  • 1
  • 6
  • 2
    Please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 14 '18 at 14:20

2 Answers2

2

You have also tell ggplot you want your data seperated by a variable, you can easily to this with the group argument:

x<-c(0.6,0.7,0.7,0.9,0.6,0.7)
y<-c(0,0,1,0,1,0)

df <- as.data.frame(cbind(x,y))
ggplot(df, aes(x=x,fill=y, group = y))+
  geom_density(alpha=0.5)

gives the ouputfirst plot

now the legend is different, this outcome is because ggplot realizes there is a variable and not a character. Actually it just needs factorisation, but it doesn't get it in your example. A workaround would be as follows (i'm sure there are more elegant solutions):

x<-c(0.6,0.7,0.7,0.9,0.6,0.7)
y<-as.character(c(0,0,1,0,1,0))

df <- as.data.frame(cbind(x,y))
ggplot(df, aes(x=x, fill=y, group=y))+
  geom_density(alpha=0.5)

which should result in the desired graph

graph2

mischva11
  • 2,811
  • 3
  • 18
  • 34
  • 1
    Thanks! I think there was also another issue because I've solve this without group but with the following: mydata <- data.frame(num_var = c(0.6, 0.7, 0.7, 0.9, 0.6, 0.7, 0.6, 0.7, 1.1, 0.75), int_var = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L)) head(mydata) mydata[,'fact_var']<-factor(mydata[,'int_var']) ggplot(mydata, aes(x = num_var, fill = fact_var)) + geom_density(alpha = 0.5) – Mia Aug 14 '18 at 15:37
0

I've solved the issue by looking at the structure and realising that the above code would not return a factor variable (not sure why?) so changed it to the below.

Thanks to everyone who replied! Now I can finally have lunch

sample data

mydata <- data.frame(num_var = c(0.6, 0.7, 0.7, 0.9, 0.6, 0.7, 0.6, 0.7, 1.1, 0.75),
int_var = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L))

code

head(mydata)
mydata[,'fact_var']<-factor(mydata[,'int_var'])
ggplot(mydata, aes(x = num_var, fill = fact_var)) + geom_density(alpha = 0.5)

enter image description here

Mia
  • 95
  • 1
  • 6