How to get rid of c from the equation in a plot

Hello I am plotting multiple graphs using ggplot. however, there are c() in the equation when they areplotted in the graph. How do I get rid of this c()?

lm_eqn = function(gppricecounty){
  m = lm(yieldgm ~ gpp_sum_8day, gppricecounty);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}
eq <- ddply(gppricecounty,.(Year),lm_eqn)

p <- ggplot(data = gppricecounty, aes(x = gpp_sum_8day, y = yieldgm)) +
  geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
  geom_point()+
  xlab("Mean Cumulative Gross Primary Productivity of the counties (gC/m2/season)")+
  ylab("Mean Yieldof the counties (g/m2/season)")
p1 = p + geom_text(data=eq,aes(x = 1200, y = 600,label=V1), parse = TRUE, inherit.aes=FALSE) + facet_wrap(Year~.)
p1 


Can you share the result of dplyr::glimpse(eq)?

Thanks for looking into it. Here is a screenshot of the result of dplyr::glimpse(eq)

Does adding the code below (prior to plotting) get to your desired result?

library(tidyverse)

eq = eq %>%
  mutate(V1 = str_replace_all(V1, '\\sc\\(|\\)\\s', ''))

image

Thanks for helping. Just got an error while plotting. I think I am getting rid of one of the parenthesis that I don't want.

Yes, I see it's removing all closing parentheses. There's probably a more elegant solution, but see if the following works.

library(tidyverse)

eq = eq %>%
  separate(V1, into = c('V2', 'V3'), sep = '\\+') %>%
  separate(V3, into = c('V4', 'V5'), sep = '%.%') %>%
  mutate_all(~str_squish(.)) %>%
  mutate(V2 = str_replace(V2, '\\sc\\(', ''),
         V2 = substr(V2, 1, nchar(V2) - 1),
         V2 = paste(V2, '+')) %>%
  mutate(V4 = str_replace(V4, 'c\\(', ''),
         V4 = substr(V4, 1, nchar(V4) - 1),
         V4 = paste(V4, '%.%')) %>%
  mutate(V1 = paste0(V2, ' ', V4, ' ', V5)) %>%
  select(Year, V1)

Hi thanks for your time and help. I think we almost got it. But one parenthesis is still there (attached imaged highlighted)

Can you share gppricecounty?

Hey, I was not able to upload the csv file also dont know if this screenshot will help you or not. but i can also send the data through email.
image

Thanks for sharing some data. After reviewing it more, I went back to the drawing board. I looked at the documentation for geom_text() and noticed whenever parse = TRUE, the labels will be parsed into expressions and displayed according to plotmath, which I'm not familiar with. Long story short, the transformations I attempted to make broke this process, and the labels wouldn't display properly.

So, I decided to use the ggtext package for displaying the output. The code below breaks apart the character strings and reassembles them into HTML strings. Admittedly, this is not an elegant solution, but I think it gets to your final goal. Notice for the plot, geom_text is replaced with geom_richtext.

library(tidyverse)
library(plyr)

# sample data provided
gppricecounty = tibble(
  Year = c(rep(2008, 5), rep(2009, 7), rep(2010, 6)),
  yieldgm = c(824, 735, 746, 778, 708, 693, 720, 787, 794, 
              778, 715, 780, 727, 723, 706, 723, 753, 761),
  gpp_sum_8day = c(1179, 1260, 1086, 1274, 1216, 1264, 1446, 1308, 982, 
                   1344, 1358, 1242, 1186, 1122, 1142, 1196, 1194, 1393)
)

lm_eqn = function(gppricecounty){
  m = lm(yieldgm ~ gpp_sum_8day, gppricecounty);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

# original eq data
eq <- plyr::ddply(gppricecounty, .(Year),lm_eqn)

# transformation steps resulting in a table with Year and Label
eq = eq %>%
  # split the string apart into new columns
  separate(V1, into = c('V2', 'V3'), sep = '\\+') %>%
  separate(V3, into = c('V4', 'V5'), sep = '%.%') %>%
  separate(V5, into = c('V6', 'V7'), sep = '\\* "," ~ ~') %>%
  separate(V7, into = c('V8', 'V9'), sep = ' ~ "=" ~') %>%
  mutate_all(~str_squish(.)) %>%
  # clean/transform the columns
  mutate(V2 = paste0('y = ', str_extract(V2, '\".*\"'))) %>%
  mutate(V2 = str_replace_all(V2, '\\"', '')) %>%
  mutate(V2 = str_replace(V2, 'y', '<i>y</i>')) %>%
  mutate(V4 = str_extract(V4, '\".*\"')) %>%
  mutate(V4 = str_replace_all(V4, '\\"', '')) %>%
  mutate(V6 = str_extract(V6, '\\(.*\\)')) %>%
  mutate(V6 = str_replace_all(V6, '\\(|\\)', '')) %>%
  mutate(V8a = paste0(str_extract(V8, '\\(.*\\)'))) %>%
  mutate(V8a = str_replace_all(V8a, '\\(|\\)', '')) %>%
  mutate(V8a = paste0(tolower(V8a), '<sup>', substr(V8, nchar(V8) - 1, nchar(V8)), '</sup>')) %>%
  mutate(V9 = str_replace_all(V9, '\\"', '')) %>%
  # assemble final label
  mutate(Label = paste0(V2, ' + ', V4, '&#8729;', V6, ', ', V8a, ' = ', V9)) %>%
  # turn math symbols plus minus (+ -) into a single minus (-)
  mutate(Label = str_replace(Label, ' \\+ -', ' - ')) %>%
  # keep only two columns
  select(Year, Label)
  
library(ggtext)

p <- ggplot(data = gppricecounty, aes(x = gpp_sum_8day, y = yieldgm)) +
  geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
  geom_point()+
  xlab("Mean Cumulative Gross Primary Productivity of the counties (gC/m2/season)")+
  ylab("Mean Yieldof the counties (g/m2/season)")

p1 = p + 
  geom_richtext(data=eq, 
                aes(x = 1200, y = 600, label = Label), 
                inherit.aes=FALSE, 
                fill = NA, 
                label.color = NA) + 
  facet_wrap(Year ~ .)

p1 

1 Like

Thank you so much for the help. Can I ask you how I can can capitalize the r in the r2?

I added tolower() to match the original image. To keep it capitalized, replace
mutate(V8a = paste0(tolower(V8a), '<sup>', substr(V8, nchar(V8) - 1, nchar(V8)), '</sup>')) %>%

with
mutate(V8a = paste0(V8a, '<sup>', substr(V8, nchar(V8) - 1, nchar(V8)), '</sup>')) %>%