Reputation: 1117
Using the example data:
# day 1
carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))
#Now, combine your two dataframes into one. First make a new column in each.
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
#and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)
#now make your lovely plot
ggplot(vegLengths, aes(length, fill = veg)) + geom_histogram(alpha = 0.5), position = 'identity')
# day 2
carrots <- data.frame(length = rnorm(600000, 6, 2))
cukes <- data.frame(length = rnorm(70000, 7, 2.5))
#Now, combine your two dataframes into one. First make a new column in each.
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
#and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)
#now make your lovely plot
ggplot(vegLengths, aes(length, fill = veg)) + geom_histogram(alpha = 0.5), position = 'identity')
I have a similar dataset and decided to use this for easier understanding. I have two such plots. How to use the faceting option and represent day1 and day2 together in a single plot in order to compare them ?
Upvotes: 1
Views: 243
Reputation: 83215
First you have to combine all the data into one dataframe:
carrots1 <- data.frame(length = rnorm(100000, 6, 2))
cukes1 <- data.frame(length = rnorm(50000, 7, 2.5))
carrots1$veg <- 'carrot'
cukes1$veg <- 'cuke'
vegLengths1 <- rbind(carrots1, cukes1)
vegLengths1$day <- '1'
carrots2 <- data.frame(length = rnorm(600000, 6, 2))
cukes2 <- data.frame(length = rnorm(70000, 7, 2.5))
carrots2$veg <- 'carrot'
cukes2$veg <- 'cuke'
vegLengths2 <- rbind(carrots2, cukes2)
vegLengths2$day <- '2'
vegLengths <- rbind(vegLengths1, vegLengths2)
After you can make a plot with:
require(ggplot2)
ggplot(vegLengths, aes(x=length, fill = veg)) +
geom_histogram(binwidth=0.5, alpha = 0.5, position = 'identity') +
facet_wrap(~ day)
The result:
Upvotes: 1