【发布时间】:2021-08-03 13:56:59
【问题描述】:
我有以下数据框,我用它来创建一个 ggplot,它是我使用 facet_wrap 安排的一组密度图。
structure(list(date = c("2021-07-25", "2021-07-25", "2021-07-25",
"2021-07-25", "2021-07-25", "2021-07-25", "2021-07-25", "2021-07-25",
"2021-07-25", "2021-07-25", "2021-07-26", "2021-07-26", "2021-07-26",
"2021-07-26", "2021-07-26", "2021-07-26", "2021-07-26", "2021-07-26",
"2021-07-26", "2021-07-26", "2021-07-27", "2021-07-27", "2021-07-27",
"2021-07-27", "2021-07-27", "2021-07-27", "2021-07-27", "2021-07-27",
"2021-07-27", "2021-07-27", "2021-07-28", "2021-07-28", "2021-07-28",
"2021-07-28", "2021-07-28", "2021-07-28", "2021-07-28", "2021-07-28",
"2021-07-28", "2021-07-28", "2021-07-29", "2021-07-29", "2021-07-29",
"2021-07-29", "2021-07-29", "2021-07-29", "2021-07-29", "2021-07-29",
"2021-07-29", "2021-07-29", "2021-07-30", "2021-07-30", "2021-07-30",
"2021-07-30", "2021-07-30", "2021-07-30", "2021-07-30", "2021-07-30",
"2021-07-30", "2021-07-30", "2021-07-31", "2021-07-31", "2021-07-31",
"2021-07-31", "2021-07-31", "2021-07-31", "2021-07-31", "2021-07-31",
"2021-07-31", "2021-07-31"), Order_Type = structure(c(1L, 1L,
2L, 2L, 3L, 3L, 8L, 8L, 9L, 9L, 1L, 1L, 2L, 2L, 3L, 3L, 8L, 8L,
9L, 9L, 1L, 1L, 2L, 2L, 3L, 3L, 8L, 8L, 9L, 9L, 1L, 1L, 2L, 2L,
3L, 3L, 8L, 8L, 9L, 9L, 1L, 1L, 2L, 2L, 3L, 3L, 8L, 8L, 9L, 9L,
1L, 1L, 2L, 2L, 3L, 3L, 8L, 8L, 9L, 9L, 1L, 1L, 2L, 2L, 3L, 3L,
8L, 8L, 9L, 9L), .Label = c("group1", "group2", "group3",
"group4", "group5", "group6",
"group7", "group8", "group9"), class = "factor"),
ntf_normalized = c(1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.8372252453,
0.8372252453, 0.3275361961, 0.3275361961, 1, 1, 1, 1, 0.3275361961,
0.3275361961, 0.1840258965, 0.1840258965, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0.1603545867, 0.1603545867, 0.4950785714, 0.4950785714,
0.7142857145, 0.7142857145, 1, 1, 1, 1, 1, 1, 1, 1, 0.5526912352,
0.5526912352, 1, 1, 1, 1, 0, 0, 1, 1), ntf_first_touch = c(1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1), spend = c(25.99294208,
25.99294208, 308.2773075, 308.2773075, 206.28925744, 206.28925744,
0.00398, 0.00398, 304.77034136, 304.77034136, 410.2332538,
410.2332538, 185.819275, 185.819275, 213.72726668, 213.72726668,
414.61074958, 414.61074958, 323.66596883, 323.66596883, 570.88491388,
570.88491388, 115.2426355, 115.2426355, 234.28878223, 234.28878223,
846.78931866, 846.78931866, 336.60236658, 336.60236658, 803.26558903,
803.26558903, 258.157645, 258.157645, 214.85833223, 214.85833223,
1157.49991513, 1157.49991513, 321.93666812, 321.93666812,
523.59701611, 523.59701611, 122.06332, 122.06332, 137.31790493,
137.31790493, 724.54289394, 724.54289394, 213.9509169, 213.9509169,
393.06862193, 393.06862193, 161.8394165, 161.8394165, 88.23073883,
88.23073883, 342.05106384, 342.05106384, 117.82164784, 117.82164784,
388.23691524, 388.23691524, 97.247412, 97.247412, 99.12069722,
99.12069722, 0, 0, 91.27114294, 91.27114294), names = c("cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch", "cac_normalized", "cac_first_touch", "cac_normalized",
"cac_first_touch"), values = c(25.99294208, 25.99294208,
308.2773075, 308.2773075, 206.28925744, 206.28925744, 0.00796,
0.00398, 364.024308954985, 152.38517068, 1252.4821948984,
410.2332538, 185.819275, 185.819275, 213.72726668, 213.72726668,
1265.84711710279, 414.61074958, 1758.80664072733, 323.66596883,
570.88491388, 570.88491388, 115.2426355, 115.2426355, 234.28878223,
234.28878223, 846.78931866, 846.78931866, 336.60236658, 336.60236658,
803.26558903, 803.26558903, 258.157645, 258.157645, 214.85833223,
214.85833223, 1157.49991513, 1157.49991513, 321.93666812,
321.93666812, 523.59701611, 523.59701611, 122.06332, 122.06332,
137.31790493, 137.31790493, 4518.37960391812, 724.54289394,
432.155478462706, 213.9509169, 550.296070536911, 393.06862193,
161.8394165, 161.8394165, 88.23073883, 88.23073883, 342.05106384,
342.05106384, 117.82164784, 117.82164784, 702.448113003837,
388.23691524, 97.247412, 97.247412, 99.12069722, 99.12069722,
0, 0, 91.27114294, 91.27114294)), row.names = c(NA, -70L), groups = structure(list(
date = c("2021-07-25", "2021-07-26", "2021-07-27", "2021-07-28",
"2021-07-29", "2021-07-30", "2021-07-31"), .rows = structure(list(
1:10, 11:20, 21:30, 31:40, 41:50, 51:60, 61:70), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ggplot(df1, aes(x = values, fill = names)) +
geom_density(alpha = 0.3) +
labs(title = "Advertising", subtitle = "CAC Distribution") +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
xlab("CAC") +
ylab("density") +
facet_wrap(~Order_Type, scales = 'free')
问题是,我发现 group2 和 group3 中发生的事情有点难以解释。这有点像一个错误。事实证明,group2 和 group3 的数字完全相同,因此其中一条线被删除了。看起来只有一个名称的数据(cac_normalized 但没有 cac_first_touch)。有没有办法稍微改变线路以便看到这两个事件?我知道 geom_point 可以因为这个确切的原因变成 geom_jitter 。如果我将 geom_jitter 放入我的 ggplot 中,我会收到错误消息。如果我更改 geom_density 中的 alpha,它不会做任何事情。有人知道吗?
【问题讨论】:
标签: r ggplot2 density-plot