简体   繁体   中英

Error message while plotting density functions in ggplot

I had a data frame with 750 observations and 250 columns, and I would like to plot two density plots on top of each other. In one case, a particular factor is present, in the other it isn't (commercial activities against non-commercial activities).

I created a subset of the data

CommercialActivityData <- subset(MbadSurvey, Q2== 1)
NonCommercialActivityData <- subset(MbadSurvey, Q2== 2)

I then tried to plot this as follows

p1 <- ggplot(CommercialActivityData, aes(x = water_use_PP)) + geom_density()
p1

However, when I do, I get the following error message

Error: Aesthetics must be either length 1 or the same as the data (51): x

I have 51 data values where there is commercial, and 699 where there isn't.

EDIT: new code!!

I don't have access to your data set so I have simulated your data:

# Creating the data frame
MbadSurvey <- data.frame("water_use_PP"=runif(1000,1,100),
                 "Q2"=as.factor(round(runif(1000,1,2),0)))

# Requiring the package
require(ggplot2)

# Creating 3 different density plots based on the Species
p1 <- ggplot(MbadSurvey, aes(x = water_use_PP,colour = Q2)) + geom_density()
p1

在此处输入图片说明

NOTE: The variable Q2 must be a factor!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM