我想用自定义颜色将线条图覆盖在箱形图上。
我几乎可以肯定我以前这样做过,但我现在找不到解决方案。有没有人愿意回答这个问题?提前谢谢。
箱形图
这段代码:
ggplot(plotdata) +
geom_boxplot(data=resp_daily_ca_ranked,
aes(x=as.factor(week), y=pct, fill = rankgroup)) +
scale_fill_manual(values=COLS) +
# NOT RUN: geom_line(aes(week, pct, colour=Group)) +
theme_minimal() + xlab("") + ylab(YLAB) +
scale_color_manual(values=COLS) +
scale_y_continuous(labels = scales::percent) +
ggtitle(label = TITLE, subtitle = SUBTITLE)生成所需的箱线图:

线条图
这段代码:
ggplot(plotdata) +
# NOT RUN: geom_boxplot(data=resp_daily_ca_ranked,
# NOT RUN: aes(x=as.factor(week), y=pct, fill = rankgroup)) +
scale_fill_manual(values=COLS) +
geom_line(aes(week, pct, colour=Group)) +
theme_minimal() + xlab("") + ylab(YLAB) +
scale_color_manual(values=COLS) +
scale_y_continuous(labels = scales::percent) +
ggtitle(label = TITLE, subtitle = SUBTITLE)生成所需的线状图:

错误
然而,这段代码包含所需的组件:
ggplot(plotdata) +
geom_boxplot(data=resp_daily_ca_ranked,
aes(x=as.factor(week), y=pct, fill = rankgroup)) +
scale_fill_manual(values=COLS) +
geom_line(aes(week, pct, colour=Group)) +
theme_minimal() + xlab("") + ylab(YLAB) +
scale_color_manual(values=COLS) +
scale_y_continuous(labels = scales::percent) +
ggtitle(label = TITLE, subtitle = SUBTITLE)产生以下错误:
Error: `mapped_discrete` objects can only be created from numeric vectors
Run `rlang::last_error()` to see where the error occurred.天真的方法
这段代码:
ggplot(plotdata) +
geom_boxplot(data=resp_daily_ca_ranked,
aes(x=week, y=pct, fill = rankgroup)) + # <-- removed factor
scale_fill_manual(values=COLS) +
geom_line(aes(week, pct, colour=Group)) +
theme_minimal() + xlab("") + ylab(YLAB) +
scale_color_manual(values=COLS) +
scale_y_continuous(labels = scales::percent) +
ggtitle(label = TITLE, subtitle = SUBTITLE)生成此图(这不是所需的)

创建示例的代码:
require(data.table)
require(ggplot2)
plotdata <- structure(
list(structure(
c(18355L, 18355L, 18355L, 18355L, 18355L, 18355L, 18355L, 18362L, 18362L, 18362L, 18362L,
18362L, 18362L, 18362L, 18369L, 18369L, 18369L, 18369L, 18369L, 18369L, 18369L, 18376L,
18376L, 18376L, 18376L, 18376L, 18376L, 18376L, 18383L, 18383L, 18383L, 18383L, 18383L,
18383L, 18383L, 18390L, 18390L, 18390L, 18390L, 18390L, 18390L, 18390L, 18397L, 18397L,
18397L, 18397L, 18397L, 18397L, 18397L, 18404L, 18404L, 18404L, 18404L, 18404L, 18404L,
18404L, 18411L, 18411L, 18411L, 18411L, 18411L, 18411L, 18411L, 18418L, 18418L, 18418L,
18418L, 18418L, 18418L, 18418L, 18425L, 18425L, 18425L, 18425L, 18425L, 18425L, 18425L,
18432L, 18432L, 18432L, 18432L, 18432L, 18432L, 18432L, 18439L, 18439L, 18439L, 18439L,
18439L, 18439L, 18439L, 18446L, 18446L, 18446L, 18446L, 18446L, 18446L, 18446L, 18453L,
18453L, 18453L, 18453L, 18453L, 18453L, 18453L, 18460L, 18460L, 18460L, 18460L, 18460L,
18460L, 18460L, 18467L, 18467L, 18467L, 18467L, 18467L, 18467L, 18467L),
class = c('IDate', 'Date')),
c(0.1649, 0.1571, 0.1402, 0.1288, 0.1179, 0.1011, 0.0826, 0.0465, 0.0435, 0.0401, 0.0388,
0.0363, 0.0334, 0.0288, 0.0282, 0.0263, 0.0266, 0.0251, 0.0229, 0.0224, 0.0186, 0.0431,
0.0322, 0.0325, 0.0253, 0.025, 0.0214, 0.0155, 0.0453, 0.0301, 0.0322, 0.0233, 0.0238,
0.0189, 0.0149, 0.026, 0.0218, 0.0231, 0.0189, 0.0188, 0.019, 0.0163, 0.0126, 0.0121,
0.013, 0.0119, 0.0109, 0.015, 0.0138, 0.007, 0.0071, 0.0073, 0.0066, 0.0068, 0.0074,
0.0069, 0.0045, 0.0049, 0.0051, 0.0047, 0.0048, 0.0048, 0.0046, 0.0039, 0.0039, 0.0042,
0.0039, 0.0036, 0.0034, 0.0031, 0.0035, 0.0033, 0.0031, 0.0033, 0.0031, 0.0036, 0.0032,
0.0025, 0.003, 0.0028, 0.0031, 0.0031, 0.0029, 0.0027, 0.0023, 0.0025, 0.0023, 0.0026,
0.0024, 0.0028, 0.0025, 0.002, 0.0021, 0.0021, 0.002, 0.0024, 0.0027, 0.003, 0.0017,
0.0019, 0.0023, 0.0022, 0.0022, 0.0022, 0.0023, 0.0025, 0.0026, 0.0026, 0.0025,
0.0024, 0.0023, 0.0024, 0.0027, 0.0032, 0.0029, 0.0028, 0.0024, 0.0034, 0.0033),
c('1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2',
'3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4',
'5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6',
'7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1',
'2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3',
'4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5',
'6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7',
'1', '2', '3', '4', '5', '6', '7')),
.Names = c('week', 'pct', 'Group'),
row.names = c(NA, -119L),
class = c('data.table', 'data.frame'))
resp_daily_ca_ranked <- structure(list(structure(
c(18355L, 18355L, 18355L, 18362L, 18362L, 18362L, 18369L, 18369L, 18369L, 18376L, 18376L, 18376L,
18383L, 18383L, 18383L, 18390L, 18390L, 18390L, 18397L, 18397L, 18397L, 18404L, 18404L, 18404L,
18411L, 18411L, 18411L, 18418L, 18418L, 18418L, 18425L, 18425L, 18425L, 18432L, 18432L, 18432L,
18439L, 18439L, 18439L, 18446L, 18446L, 18446L, 18453L, 18453L, 18453L, 18460L, 18460L, 18460L,
18467L, 18467L, 18467L, 18355L, 18355L, 18355L, 18362L, 18362L, 18362L, 18369L, 18369L, 18369L,
18376L, 18376L, 18376L, 18383L, 18383L, 18383L, 18390L, 18390L, 18390L, 18397L, 18397L, 18397L,
18404L, 18404L, 18404L, 18411L, 18411L, 18411L, 18418L, 18418L, 18418L, 18425L, 18425L, 18425L,
18432L, 18432L, 18432L, 18439L, 18439L, 18439L, 18446L, 18446L, 18446L, 18453L, 18453L, 18453L,
18460L, 18460L, 18460L, 18467L, 18467L, 18467L, 18355L, 18355L, 18355L, 18362L, 18362L, 18362L,
18369L, 18369L, 18369L, 18376L, 18376L, 18376L, 18383L, 18383L, 18383L, 18390L, 18390L, 18390L,
18397L, 18397L, 18397L, 18404L, 18404L, 18404L, 18411L, 18411L, 18411L, 18418L, 18418L, 18418L,
18425L, 18425L, 18425L, 18432L, 18432L, 18432L, 18439L, 18439L, 18439L, 18446L, 18446L, 18446L,
18453L, 18453L, 18453L, 18460L, 18460L, 18460L, 18467L, 18467L, 18467L, 18355L, 18355L, 18355L,
18362L, 18362L, 18362L, 18369L, 18369L, 18369L, 18376L, 18376L, 18376L, 18383L, 18383L, 18383L,
18390L, 18390L, 18390L, 18397L, 18397L, 18397L, 18404L, 18404L, 18404L, 18411L, 18411L, 18411L,
18418L, 18418L, 18418L, 18425L, 18425L, 18425L, 18432L, 18432L, 18432L, 18439L, 18439L, 18439L,
18446L, 18446L, 18446L, 18453L, 18453L, 18453L, 18460L, 18460L, 18460L, 18467L, 18467L, 18467L,
18355L, 18355L, 18355L, 18362L, 18362L, 18362L, 18369L, 18369L, 18369L, 18376L, 18376L, 18376L,
18383L, 18383L, 18383L, 18390L, 18390L, 18390L, 18397L, 18397L, 18397L, 18404L, 18404L, 18404L,
18411L, 18411L, 18411L, 18418L, 18418L, 18418L, 18425L, 18425L, 18425L, 18432L, 18432L, 18432L,
18439L, 18439L, 18439L, 18446L, 18446L, 18446L, 18453L, 18453L, 18453L, 18460L, 18460L, 18460L,
18467L, 18467L, 18467L, 18355L, 18355L, 18355L, 18362L, 18362L, 18362L, 18369L, 18369L, 18369L,
18376L, 18376L, 18376L, 18383L, 18383L, 18383L, 18390L, 18390L, 18390L, 18397L, 18397L, 18397L,
18404L, 18404L, 18404L, 18411L, 18411L, 18411L, 18418L, 18418L, 18418L, 18425L, 18425L, 18425L,
18432L, 18432L, 18432L, 18439L, 18439L, 18439L, 18446L, 18446L, 18446L, 18453L, 18453L, 18453L,
18460L, 18460L, 18460L, 18467L, 18467L, 18467L, 18355L, 18355L, 18355L, 18362L, 18362L, 18362L,
18369L, 18369L, 18369L, 18376L, 18376L, 18376L, 18383L, 18383L, 18383L, 18390L, 18390L, 18390L,
18397L, 18397L, 18397L, 18404L, 18404L, 18404L, 18411L, 18411L, 18411L, 18418L, 18418L, 18418L,
18425L, 18425L, 18425L, 18432L, 18432L, 18432L, 18439L, 18439L, 18439L, 18446L, 18446L, 18446L,
18453L, 18453L, 18453L, 18460L, 18460L, 18460L, 18467L, 18467L, 18467L),
class = c('IDate', 'Date')),
c('4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'5', '5', '5', '5', '5', '5', '5', '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', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7'),
c(0.1277, 0.1051, 0.1096, 0.0341, 0.0329, 0.0382, 0.031, 0.0184, 0.0176, 0.0392, 0.022, 0.0124,
0.0306, 0.0186, 0.0077, 0.0188, 0.0228, 0.0197, 0.0087, 0.0157, 0.0181, 0.0063, 0.0055, 0.0082,
0.0037, 0.0051, 0.0055, 0.004, 0.0025, 0.0043, 0.0023, 0.0024, 0.0046, 0.0022, 0.0029, 0.0038,
0.0027, 0.0024, 0.0028, 0.0024, 0.0028, 0.002, 0.0025, 0.002, 0.0019, 0.0023, 0.0015, 0.0016,
0.0027, 0.0036, 0.0028, 0.1271, 0.1181, 0.1218, 0.04, 0.0398, 0.0353, 0.0384, 0.0244, 0.0168,
0.0249, 0.0251, 0.0333, 0.0185, 0.034, 0.0263, 0.0254, 0.0213, 0.0191, 0.0186, 0.0095, 0.0107,
0.0064, 0.0066, 0.0048, 0.0051, 0.0047, 0.0055, 0.0044, 0.0029, 0.0035, 0.0025, 0.0024, 0.0025,
0.0037, 0.0022, 0.0022, 0.0041, 0.0031, 0.0016, 0.0026, 0.0025, 0.0029, 0.004, 0.002, 0.0016,
0.0037, 0.0037, 0.0015, 0.0031, 0.0024, 0.0029, 0.1671, 0.1859, 0.1612, 0.0471, 0.0549, 0.043,
0.0367, 0.0315, 0.0299, 0.0444, 0.0269, 0.047, 0.0355, 0.0448, 0.0518, 0.0237, 0.0251, 0.0275,
0.0144, 0.0126, 0.0128, 0.0073, 0.0084, 0.0071, 0.005, 0.0065, 0.004, 0.005, 0.0046, 0.0036,
0.005, 0.0057, 0.003, 0.003, 0.0022, 0.002, 0.0022, 0.0028, 0.002, 0.0025, 0.0026, 0.0014,
0.0021, 0.0029, 0.0011, 0.0031, 0.0037, 0.0016, 0.0038, 0.0035, 0.0023, 0.1047, 0.1079, 0.09,
0.0332, 0.0354, 0.0328, 0.0195, 0.0345, 0.0282, 0.016, 0.0333, 0.0343, 0.012, 0.0362, 0.0275,
0.0189, 0.0205, 0.0195, 0.016, 0.0124, 0.0116, 0.0073, 0.0064, 0.0063, 0.0044, 0.0052, 0.0046,
0.0037, 0.0037, 0.0032, 0.0039, 0.0039, 0.0032, 0.0021, 0.0033, 0.0033, 0.0029, 0.0033, 0.0032,
0.0026, 0.0023, 0.0028, 0.002, 0.0023, 0.0023, 0.0021, 0.0031, 0.0028, 0.0031, 0.0039, 0.0034,
0.1311, 0.1591, 0.1527, 0.0482, 0.0451, 0.0413, 0.0219, 0.0258, 0.0286, 0.014, 0.0223, 0.0631,
0.0132, 0.0178, 0.0395, 0.0198, 0.0209, 0.0202, 0.0186, 0.0197, 0.0099, 0.0093, 0.0103, 0.0052,
0.0061, 0.0065, 0.0038, 0.005, 0.0048, 0.0026, 0.005, 0.0059, 0.0028, 0.0054, 0.0031, 0.0026,
0.003, 0.0034, 0.0015, 0.0034, 0.0024, 0.0028, 0.0027, 0.0026, 0.0016, 0.0036, 0.0021, 0.0026,
0.0031, 0.0028, 0.0029, 0.1301, 0.1439, 0.1352, 0.0382, 0.0416, 0.0413, 0.0406, 0.0197, 0.028,
0.0303, 0.0241, 0.0339, 0.0219, 0.0281, 0.0365, 0.024, 0.0207, 0.0223, 0.02, 0.024, 0.0121,
0.0092, 0.0094, 0.0064, 0.0072, 0.0051, 0.0054, 0.0034, 0.0026, 0.0035, 0.0023, 0.002, 0.0024,
0.0032, 0.0024, 0.0032, 0.0028, 0, 0.0013, 0.0038, 0.001, 0.0025, 0.0016, 0.002, 0.0024, 0.0024,
0.001, 0.0022, 0.003, 0.004, 0.0037, 0.0668, 0.0612, 0.0969, 0.0247, 0.032, 0.034, 0.0135,
0.0109, 0.0208, 0.0059, 0.007, 0.0173, 0.0062, 0.0046, 0.0185, 0.0119, 0.0196, 0.0182, 0.0125,
0.021, 0.0151, 0.0062, 0.0066, 0.0072, 0.0043, 0.007, 0.0053, 0.0018, 0.005, 0.0028, 0.003,
0.0031, 0.0032, 0.0023, 0.0014, 0.0032, 0.002, 0.001, 0.0027, 0.0031, 0.0014, 0.0028, 0.0019,
0.0013, 0.0021, 0.0012, 0, 0.0033, 0.0024, 0.003, 0.0034)),
row.names = c(NA, -357L),
class = c('data.table', 'data.frame'),
.Names = c('week', 'rankgroup', 'pct'))
TITLE <- "Incremental Response Rate by Community Area (grouped)"
SUBTITLE <- sprintf("Weeks ending %s to %s",
format(min(plotdata$week), "%m/%d/%y"),
format(max(plotdata$week), "%m/%d/%y"))
YLAB <- "Incremental Percent Responding"
COLS <- structure(c('#F3771AFF', '#D84C3EFF', '#AE305CFF', '#7F1E6CFF', '#500E6CFF',
'#1E0C44FF', '#000004FF'),
.Names = c('1', '2', '3', '4', '5', '6', '7'))发布于 2020-07-31 07:18:16
下面的代码实际上与问题的代码相同。可能主要的区别是在层boxplot和line中显式地指定了data参数。
ggplot(plotdata) +
geom_boxplot(data = resp_daily_ca_ranked,
mapping = aes(week, pct, fill = rankgroup)) +
geom_line(data = plotdata,
mapping = aes(week, pct, color = Group), show.legend = FALSE) +
scale_fill_manual(values = COLS) +
scale_color_manual(values = COLS) +
scale_y_continuous(labels = scales::percent) +
xlab("") + ylab(YLAB) +
ggtitle(label = TITLE, subtitle = SUBTITLE) +
theme_minimal()

发布于 2020-08-01 00:21:37
我今天找到了答案。
首先,我需要记住aes需要一个“组”参数(我的意思是记住,因为只有x和y是aes的文档化输入)。
其次,有必要在boxplot中使用interaction来捕获时间和组。
第三,我还组合了分组和未分组的数据,然后在geom调用中对数据进行子集。source
combodata <- rbind(plotdata[,list(week, pct, Group, subset="line")],
resp_daily_ca_ranked[ , list(week, pct, Group=rankgroup,
subset="boxplot")])
ggplot(combodata, aes(x=week, y=pct, fill=Group)) +
geom_boxplot(aes(group=interaction(week, Group)),
data = function(x){x[subset=="boxplot"]})+
geom_line(aes(color=Group), data = function(x){x[subset=="line"]}) +
theme_minimal() + xlab("") + ylab(YLAB) +
scale_color_manual(values=COLS) +
scale_fill_manual(values=COLS) +
scale_y_continuous(labels = scales::percent) +
ggtitle(label = TITLE, subtitle = SUBTITLE)我不确定是否有必要合并数据,但无论如何这都很好,因为我将处理数据的方式。

顺便说一句,这样做的原因是为了找出聚合数据中是否存在有意义的偏差。
例如,看看这一段时间:

很有趣..。为什么表现最差的人会有一个尖峰?这和其他事情有关系吗?这让我有了在哪里查找数据的感觉。
https://stackoverflow.com/questions/63182571
复制相似问题