使用示例数据:
#day 1
carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))
#现在,将两个数据帧合并为一个。首先在每个列中创建一个新列。
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
#并合并到您的新数据框vegLengths
vegLengths <- rbind(carrots, cukes)
#now制作你可爱的情节
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'
#并合并到您的新数据框vegLengths
vegLengths <- rbind(carrots, cukes)
#now制作你可爱的情节
ggplot(vegLengths, aes(length, fill = veg)) + geom_histogram(alpha = 0.5), position = 'identity')
我有一个类似的数据集,并决定使用它来更容易理解。我有两个这样的情节。如何使用分面选项并在一个图中一起表示day1和day2以进行比较?
答案 0 :(得分:1)
首先,您必须将所有数据合并到一个数据框中:
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)
您可以用:
制作情节require(ggplot2)
ggplot(vegLengths, aes(x=length, fill = veg)) +
geom_histogram(binwidth=0.5, alpha = 0.5, position = 'identity') +
facet_wrap(~ day)
结果: