Superimposed density plot, ggplot2, R 3.5.1. , error: Aesthetics must be either length 1 or the same as the data

462 Views Asked by At

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
2

There are 2 best solutions below

1
mischva11 On

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

0
Mia On

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