0

I want to add the results of multiple linear models to my plot as a geom_text , they are stored in the same tibble as the scatterplot data. Since i want to use the approx symbol (%~~%) and the r squared as superscript (r^2) is set parse=T inside the geom_text command. However, it gives me:

Error in parse(text = text[[i]]) : <text>:1:14: unexpected symbol
1: r^2 %~~%0.25 RMSE

with an arrow pointing to the M in RMSE. If i omit the parse=T, it will plot, but without the formatting. Further if i run it in Rstudio by simply clicking in the plot command structure and press Ctrl+Enter it gives me:

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

If i i mark the code part and hit Ctrl+Enter i gives throws the unexpected symbold error. Does anybody know why and how to circumvent this? I already tried leaving out the RMSE string part, then it simply throws the error on another location of the string. It seems to be connected to the round command but i do not now how to circumvent this. Here is the whole plot command:

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))

This is how it look with an formated string, but i want the equal signs to be approx signs and the r2 to be in superscript. Unformatted Text Plot

data:

    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")
AEM
  • 1,354
  • 8
  • 20
  • 30
m4D_guY
  • 166
  • 1
  • 11

1 Answers1

1

First, you should create a data.frame that just has the vlaues for the label, otherwise you are drawing the label over and over again on top of itself for each point in your data set. So you can create

labeldata <- df %>% 
   distinct(date_rpm, RMSE_height, R2_height, intercept_height, slope_height)

And then when you make the label, you need to create text that's a valid R plotmath expression. You can't just freely mix text and math symbols. You can see what it's currently producing with

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)

So you can see how many times it's trying to print that same text. And those aren't valud expressions. You can't just have multiple statements in an R expresion with just a space inbetween them, here's an alternate version using the reduced 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)"

Now we only get one label and it's syntactically correct. You can use that in the geom_text like

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)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295