具有三个这样的数据帧:
library(tidyverse)
library(ggplot2)
数据框1:
df1 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
第二:
df2 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
第三:
df3 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
我们可以使用以下方法为每个数据框创建一个密度图:
df_long %>%
ggplot(aes(x = val, color = company, linetype = period)) +
geom_density() +
theme_bw()
如何使用三个不同的数据框将三个图创建为一个图,而另一个将其向下放置,并使用诸如df1图的Gray和df3图的Time这样的标题呢?
答案 0 :(得分:2)
如果数据具有兼容的列(例如示例数据),则可以将它们首先合并为一个大数据框,以标记其来源。
newdf <- rbind(
cbind(df1, category = "Grey"),
cbind(df2, category = "Temperature"),
cbind(df3, category = "Time")
)
ggplot(newdf, aes(val, colour = company, linetype = period)) +
geom_density() +
facet_wrap(~ category, ncol = 1)
或者,如果数据不兼容但仍具有相同的列名,则可以将每个数据帧分别指定到一个层。
ggplot(mapping = aes(val, colour = company, linetype = period)) +
geom_density(data = cbind(df1, facet = "Grey")) +
geom_density(data = cbind(df2, facet = "Temperature")) +
geom_density(data = cbind(df3, facet = "Time")) +
facet_wrap(~ facet, ncol = 1)
如果列名不匹配,则应在每个层中分别指定mapping = aes(...)
参数,而不要在对ggplot()
的调用中指定。