我如何在 geom_text 粘贴命令中使用 `parse = T` 和类似 round 的函数?
How can i use `parse = T` and functions like round in a geom_text paste command?
我想将多个线性模型的结果作为 geom_text
添加到我的图中,它们与散点图数据存储在相同的 tibble
中。因为我想使用近似符号 (%~~%
) 和 r 平方作为上标 (r^2
) 在 geom_text 命令中设置 parse=T
。但是,它给了我:
Error in parse(text = text[[i]]) : <text>:1:14: unexpected symbol
1: r^2 %~~%0.25 RMSE
带有指向 RMSE
中的 M
的箭头。如果我省略 parse=T
,它会绘制,但没有格式。此外,如果我 运行 它在 Rstudio
中通过简单地单击绘图命令结构并按 Ctrl+Enter 它给出我:
Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
如果我标记代码部分并按 Ctrl+Enter 我会抛出意外的符号错误。
有谁知道为什么以及如何避免这种情况?我已经尝试省略 RMSE
字符串部分,然后它只是在字符串的另一个位置抛出错误。它似乎与 round
命令有关,但我现在不知道如何规避它。
这是整个绘图命令:
df %>%
ggplot(aes(x = value_height, y = rpm, col = variant )) +
geom_smooth(method = 'lm', col = 'black', se = F) +
# geom_smooth(method = 'lm', se = F) +
geom_point(shape = 1) +
geom_text(
aes(label = paste0(
'r^2 %~~%', round(R2_height, 2), ' RMSE %~~% ', round(RMSE_height, 2),
'\n y =', round(intercept_height, 2),'+', round(slope_height, 2 ), 'x')),size = 4, x = 0.3, y = 10, col = 'black', parse = T) +
facet_wrap(~date_rpm) +
theme_bw() +
theme(legend.position = c(.8,.2), legend.direction = 'horizontal') +
scale_color_discrete(name='') +
ylab('Rising Plate Meter (RPM)') +
xlab('Crop Surface Height (CSH) [cm]') +
guides(colour = guide_legend(nrow = 3))
这是格式化字符串的样子,但我希望等号为近似号,r2 为上标。
数据:
df <- structure(list(date_rpm = structure(c(18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416), class = "Date"), rpm = c(5, 3.5,
3, 6, 9, 5, 3, 3, 4, 13, 21, 10, 7.5, 12, 16, 12, 13, 9, 18,
10, 7, 11, 12, 7, 11, 15, 11, 14, 14, 16, 13, 10, 10, 10, 16,
5, 8, 8, 10, 7, 9, 8, 4.5, 6, 5, 4, 3, 5, 4, 4.5, 20, 5.5, 5,
7, 9, 5, 8.5, 10, 17, 4, 8, 15, 7, 14, 12, 15, 9, 22, 13, 9,
16, 6, 9, 13, 12, 19.5, 16, 7, 13, 9.5, 5, 12, 19, 11, 7, 6,
19, 12, 15), plotname = c("A1", "A1", "A1", "A1", "A1", "A1",
"A1", "A1", "A1", "A1", "A2", "A2", "A2", "A2", "A2", "A2", "A2",
"A2", "A2", "A2", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3",
"A3", "A3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3",
"B3", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1",
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C3",
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C2", "C2", "C2",
"C2", "C2", "C2", "C2", "C2", "C2", "B2", "B2", "B2", "B2", "B2",
"B2", "B2", "B2", "B2", "B2", "C2"), variant = c("Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient"), diff = structure(c(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, 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), class = "difftime", units = "days"), value_height = c(0.0747471996861645,
0.0519722114175053, 0.0444986718332666, 0.0338748996540651, 0.130905933869191,
0.0627344897669605, 0.0461518377320379, 0.0496649014747749, 0.0346351036658654,
0.197689089000735, 0.228481029642039, 0.0869697505592281, 0.247327755659054,
0.110378037151108, 0.190863379116716, 0.153554587528623, 0.214562251650054,
0.173082661424947, 0.309699486041891, 0.117518367930355, 0.0411031690694518,
0.167414698107489, 0.367566883054554, 0.0802824884398371, 0.157873638605667,
0.189377089678231, 0.125737899389022, 0.158878721039871, 0.241583593960466,
0.238216268605199, 0.352655443094544, 0.121164650752627, 0.119100065312834,
0.087414577089507, 0.279764767350822, 0.116740391172212, 0.146888537284655,
0.0964947561932425, 0.116610769498146, 0.0829571222854873, 0.0387162030753443,
0.0489122439653445, 0.0316104231209591, 0.0265153787903867, 0.0384905540336997,
0.0679986415765224, 0.0321631997318591, 0.0634453928368723, 0.0192758592508607,
0.029608364762931, 0.0513664771770609, 0.0391104934561966, 0.040441841914736,
0.0412192509092134, 0.0682212633964343, 0.0464335384531918, 0.0814433919972387,
0.0871005588107639, 0.192856747879941, 0.060900984139278, 0.161748380742521,
0.200371047197762, 0.150143034561821, 0.176932236243939, 0.126676901792869,
0.270053961338141, 0.161895491119124, 0.125705210367839, 0.114015497713007,
0.128272423377404, 0.120223209775727, 0.0805775270623676, 0.0944333850827992,
0.196956766062769, 0.339835594440329, 0.203907762837206, 0.2025146484375,
0.0979132816709321, 0.095847887870593, 0.073602084455819, 0.0895857851729434,
0.0644812542459239, 0.320930093021716, 0.127427725956358, 0.235913659772302,
0.108491541975636, 0.138192386950477, 0.199337716829979, 0.275268289317255
), R2_height = c(0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941), slope_height = c(value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276
), intercept_height = c(5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852), RMSE_height = c(3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771
)), row.names = c(NA, -89L), class = "data.frame")
首先,您应该创建一个只有标签值的 data.frame,否则您将在数据集中的每个点上一遍又一遍地绘制标签。所以你可以创造
labeldata <- df %>%
distinct(date_rpm, RMSE_height, R2_height, intercept_height, slope_height)
然后当你制作标签时,你需要创建一个有效的 R plotmath 表达式的文本。您不能随意混合文本和数学符号。您可以使用
查看当前正在生成的内容
with(df, paste0(
'r^2 %~~%', round(R2_height, 2), ' RMSE %~~% ', round(RMSE_height, 2),
'\n y =', round(intercept_height, 2),'+', round(slope_height, 2 ), 'x'))
# [1] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# [2] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# [3] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# .... (many more lines repeated)
所以您可以看到它尝试打印相同文本的次数。这些都不是有价值的表达。你不能只在 R 表达式中有多个语句,它们之间只有一个 space,这是使用简化的 data.frame
的替代版本
with(labeldata, paste0(
'atop(r^2 %~~%', round(R2_height, 2), '~RMSE %~~% ', round(RMSE_height, 2),
', y =', round(intercept_height, 2),'+', round(slope_height, 2 ), '*x)'))
# [1] "atop(r^2 %~~%0.65~RMSE %~~% 3.59, y =5.28+36.36*x)"
现在我们只得到一个标签,而且它在语法上是正确的。您可以在 geom_text
中使用它,例如
geom_text(
aes(label = paste0(
'atop(r^2 %~~%', round(R2_height, 2), '~RMSE %~~% ', round(RMSE_height, 2),
', y =', round(intercept_height, 2),'+', round(slope_height, 2 ), '*x)')),size = 4, x = 0.3, y = 10, col = 'black',
data=labeldata, parse = T)
我想将多个线性模型的结果作为 geom_text
添加到我的图中,它们与散点图数据存储在相同的 tibble
中。因为我想使用近似符号 (%~~%
) 和 r 平方作为上标 (r^2
) 在 geom_text 命令中设置 parse=T
。但是,它给了我:
Error in parse(text = text[[i]]) : <text>:1:14: unexpected symbol
1: r^2 %~~%0.25 RMSE
带有指向 RMSE
中的 M
的箭头。如果我省略 parse=T
,它会绘制,但没有格式。此外,如果我 运行 它在 Rstudio
中通过简单地单击绘图命令结构并按 Ctrl+Enter 它给出我:
Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
如果我标记代码部分并按 Ctrl+Enter 我会抛出意外的符号错误。
有谁知道为什么以及如何避免这种情况?我已经尝试省略 RMSE
字符串部分,然后它只是在字符串的另一个位置抛出错误。它似乎与 round
命令有关,但我现在不知道如何规避它。
这是整个绘图命令:
df %>%
ggplot(aes(x = value_height, y = rpm, col = variant )) +
geom_smooth(method = 'lm', col = 'black', se = F) +
# geom_smooth(method = 'lm', se = F) +
geom_point(shape = 1) +
geom_text(
aes(label = paste0(
'r^2 %~~%', round(R2_height, 2), ' RMSE %~~% ', round(RMSE_height, 2),
'\n y =', round(intercept_height, 2),'+', round(slope_height, 2 ), 'x')),size = 4, x = 0.3, y = 10, col = 'black', parse = T) +
facet_wrap(~date_rpm) +
theme_bw() +
theme(legend.position = c(.8,.2), legend.direction = 'horizontal') +
scale_color_discrete(name='') +
ylab('Rising Plate Meter (RPM)') +
xlab('Crop Surface Height (CSH) [cm]') +
guides(colour = guide_legend(nrow = 3))
这是格式化字符串的样子,但我希望等号为近似号,r2 为上标。
数据:
df <- structure(list(date_rpm = structure(c(18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416, 18416,
18416, 18416, 18416, 18416), class = "Date"), rpm = c(5, 3.5,
3, 6, 9, 5, 3, 3, 4, 13, 21, 10, 7.5, 12, 16, 12, 13, 9, 18,
10, 7, 11, 12, 7, 11, 15, 11, 14, 14, 16, 13, 10, 10, 10, 16,
5, 8, 8, 10, 7, 9, 8, 4.5, 6, 5, 4, 3, 5, 4, 4.5, 20, 5.5, 5,
7, 9, 5, 8.5, 10, 17, 4, 8, 15, 7, 14, 12, 15, 9, 22, 13, 9,
16, 6, 9, 13, 12, 19.5, 16, 7, 13, 9.5, 5, 12, 19, 11, 7, 6,
19, 12, 15), plotname = c("A1", "A1", "A1", "A1", "A1", "A1",
"A1", "A1", "A1", "A1", "A2", "A2", "A2", "A2", "A2", "A2", "A2",
"A2", "A2", "A2", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3",
"A3", "A3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3",
"B3", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1",
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C3",
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C2", "C2", "C2",
"C2", "C2", "C2", "C2", "C2", "C2", "B2", "B2", "B2", "B2", "B2",
"B2", "B2", "B2", "B2", "B2", "C2"), variant = c("Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Moderate", "Moderate", "Moderate", "Moderate",
"Moderate", "Moderate", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Very Lenient", "Very Lenient",
"Very Lenient", "Very Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient", "Lenient",
"Lenient", "Lenient", "Lenient", "Lenient", "Lenient"), diff = structure(c(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, 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), class = "difftime", units = "days"), value_height = c(0.0747471996861645,
0.0519722114175053, 0.0444986718332666, 0.0338748996540651, 0.130905933869191,
0.0627344897669605, 0.0461518377320379, 0.0496649014747749, 0.0346351036658654,
0.197689089000735, 0.228481029642039, 0.0869697505592281, 0.247327755659054,
0.110378037151108, 0.190863379116716, 0.153554587528623, 0.214562251650054,
0.173082661424947, 0.309699486041891, 0.117518367930355, 0.0411031690694518,
0.167414698107489, 0.367566883054554, 0.0802824884398371, 0.157873638605667,
0.189377089678231, 0.125737899389022, 0.158878721039871, 0.241583593960466,
0.238216268605199, 0.352655443094544, 0.121164650752627, 0.119100065312834,
0.087414577089507, 0.279764767350822, 0.116740391172212, 0.146888537284655,
0.0964947561932425, 0.116610769498146, 0.0829571222854873, 0.0387162030753443,
0.0489122439653445, 0.0316104231209591, 0.0265153787903867, 0.0384905540336997,
0.0679986415765224, 0.0321631997318591, 0.0634453928368723, 0.0192758592508607,
0.029608364762931, 0.0513664771770609, 0.0391104934561966, 0.040441841914736,
0.0412192509092134, 0.0682212633964343, 0.0464335384531918, 0.0814433919972387,
0.0871005588107639, 0.192856747879941, 0.060900984139278, 0.161748380742521,
0.200371047197762, 0.150143034561821, 0.176932236243939, 0.126676901792869,
0.270053961338141, 0.161895491119124, 0.125705210367839, 0.114015497713007,
0.128272423377404, 0.120223209775727, 0.0805775270623676, 0.0944333850827992,
0.196956766062769, 0.339835594440329, 0.203907762837206, 0.2025146484375,
0.0979132816709321, 0.095847887870593, 0.073602084455819, 0.0895857851729434,
0.0644812542459239, 0.320930093021716, 0.127427725956358, 0.235913659772302,
0.108491541975636, 0.138192386950477, 0.199337716829979, 0.275268289317255
), R2_height = c(0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941, 0.645737397976941, 0.645737397976941,
0.645737397976941, 0.645737397976941), slope_height = c(value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276,
value_height = 36.3572830718276, value_height = 36.3572830718276
), intercept_height = c(5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852, 5.28102549943852, 5.28102549943852,
5.28102549943852, 5.28102549943852), RMSE_height = c(3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771,
3.58862854318771, 3.58862854318771, 3.58862854318771, 3.58862854318771
)), row.names = c(NA, -89L), class = "data.frame")
首先,您应该创建一个只有标签值的 data.frame,否则您将在数据集中的每个点上一遍又一遍地绘制标签。所以你可以创造
labeldata <- df %>%
distinct(date_rpm, RMSE_height, R2_height, intercept_height, slope_height)
然后当你制作标签时,你需要创建一个有效的 R plotmath 表达式的文本。您不能随意混合文本和数学符号。您可以使用
查看当前正在生成的内容with(df, paste0(
'r^2 %~~%', round(R2_height, 2), ' RMSE %~~% ', round(RMSE_height, 2),
'\n y =', round(intercept_height, 2),'+', round(slope_height, 2 ), 'x'))
# [1] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# [2] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# [3] "r^2 %~~%0.65 RMSE %~~% 3.59\n y =5.28+36.36x"
# .... (many more lines repeated)
所以您可以看到它尝试打印相同文本的次数。这些都不是有价值的表达。你不能只在 R 表达式中有多个语句,它们之间只有一个 space,这是使用简化的 data.frame
的替代版本with(labeldata, paste0(
'atop(r^2 %~~%', round(R2_height, 2), '~RMSE %~~% ', round(RMSE_height, 2),
', y =', round(intercept_height, 2),'+', round(slope_height, 2 ), '*x)'))
# [1] "atop(r^2 %~~%0.65~RMSE %~~% 3.59, y =5.28+36.36*x)"
现在我们只得到一个标签,而且它在语法上是正确的。您可以在 geom_text
中使用它,例如
geom_text(
aes(label = paste0(
'atop(r^2 %~~%', round(R2_height, 2), '~RMSE %~~% ', round(RMSE_height, 2),
', y =', round(intercept_height, 2),'+', round(slope_height, 2 ), '*x)')),size = 4, x = 0.3, y = 10, col = 'black',
data=labeldata, parse = T)