Problem while computing aesthetics.

Hi there I want to plot some graphs with the following code:

measurement_columns <- c("total conc.", "green algae", "bluegreen", "diatoms", "cryptophyta", "yellow subst.", "av. Activity")
plot_list <- list()

for (factor in measurement_columns) {
  # Create a ggplot for the current factor
  p <- ggplot(Algae_analysis, aes(x = Date, y = .data[[factor]])) +
    geom_line() +
    geom_smooth(method = "lm", se = FALSE, color = "red", linetype = "dashed") +
    labs(x = "Date", y = factor) +
    theme_minimal()+
    scale_x_continuous(breaks=Algae_analysis$Date)+
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

plot_list[[factor]] <- p

combined_plots <- plot_list[[1]]
for (i in 2:length(plot_list)) {
  combined_plots <- combined_plots + plot_list[[i]]
}

Unfortunately, the column names contain some space characters, / and % which is why i get the Error in `geom_line()`:
! Problem while computing aesthetics.
i Error occurred in the 1st layer.

This is the table im using: 
structure(list(Date = structure(c(1681948800, 1683072000, 1683590400, 
1684195200, 1684886400, 1685404800, 1686009600, 1686614400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), `total conc.  [µg/L]` = c(2.17, 2.315, 
7.58, 5.525, 7.655, 7.145, 2.75, 4.47), `green algae [µg/L]` = c(0.75, 
0.885, 2.71, 1.61, 2.16, 2.465, 1.3, 2.23), `bluegreen [µg/L]` = c(0.1, 
0.005, 0.08, 0.13, 0.14, 0.165, 0.075, 0.42), `diatoms [µg/L]` = c(1.005, 
1.33, 4.75, 3.56, 5.205, 4.235, 0.97, 1.455), `cryptophyta [µg/L]` = c(0.315, 
0.095, 0.04, 0.23, 0.15, 0.275, 0.4, 0.365), `yellow subst. [µg/L]` = c(0.33, 
0.2, 0.235, 0.38, 0.395, 0.35, 0.245, 0.305), `av. Activity [%]` = c(69.18, 
65.74, 63.125, 68.505, 68.31, 61.85, 69.115, 72.345)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -8L))

Usually you would use the backticks (``) but i don't know how to include these in my code? Can someone please help?

It looks like you have these properly backticked, but I have a better suggestion.

  1. Defer using the descriptive variable names until it's time for making a presentation table. The {gt} package, for example, will allow you to use expansive names with tons of options for stylization. And {ggplot2} theming allows more relabeling facilities than anyone reasonably needed.

  2. In the meantime, replace the existing variable names with short, pithy names. You know the data and won't have any trouble keeping them straight. For example: dt, tot,green,bluegreen,diatoms,crypts,yellow,avg.

Thank you very much, I didn't think of changing the labels afterwards ^^

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.