rggplot2bar-chart

Remove lines from a barplot legend in ggplot2


I have a barplot with smoothing lines from ggplot. But for some reason, blue lines appear in the legend on top of the barplot colors and I don't understand where they come from and how to remove them. Especially because it is a shade of blue I did not use in the code.

enter image description here

This is my code. I have tried various ggplot functions like show_guide = F or override.aes but they either don't work or remove the legend completely.

# Combine both lists into a data frame
anomaly_mean <- data.frame(
  Year = rep(1980:2023, each = 2),  # Years for x-axis, repeat years twice for DWD and ERA5
  Anomaly = c(D.anomaly_mean, E.anomaly_mean),  # Combined anomalies
  Dataset = rep(c("DWD", "ERA5"), times = length(D.anomaly_mean))  # Dataset names
)

# Create a new column combining Dataset and Anomaly sign
anomaly_mean$ColorGroup <- with(anomaly_mean, paste(Dataset, ifelse(Anomaly >= 0, "+", "-"), sep = ""))

# Plot barplot for Mean Anomalies

ggplot(anomaly_mean, aes(x = factor(Year), y = Anomaly, fill = ColorGroup, group = Dataset)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_smooth(data = subset(anomaly_mean, Dataset == "DWD"),# Smoothing line for DWD
              method = "loess", aes(x = factor(Year), y = Anomaly, color = "DWD"),  
              size = 1, se = FALSE, span = 0.4) +  
  geom_smooth(data = subset(anomaly_mean, Dataset == "ERA5"), # ERA5 line in blue
              method = "loess", aes(x = factor(Year), y = Anomaly, color = "ERA5"), 
              size = 1, se = FALSE, span = 0.4) +  
  labs(title = "Anomalies for yearly mean temperatures",
       subtitle = "Reference Period from 1981 to 2010; computed from DWD and ERA5 Data",
       x = "Year", 
       y = "Temperature Anomaly [°C]",
       color = "LOESS") +  # Title for the line legend
  scale_fill_manual(values = c("DWD+" = "brown2", 
                               "DWD-" = "cornflowerblue", 
                               "ERA5+" = "darkred", 
                               "ERA5-" = "blue")) +
  scale_color_manual(values = c("DWD" = "gray", "ERA5" = "black")) +  # colors for the smoothing lines
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, size = 10),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 10),
        legend.key.size = unit(0.5, "cm")) +  
  scale_x_discrete(labels = function(x) ifelse(as.integer(x) %% 5 == 0, x, ""))  # Show only every 5 years


Solution

  • The comment of Allan Cameron answers your question nicely. The correct code snippet is here:

    ggplot(anomaly_mean, aes(x = factor(Year), y = Anomaly, group = Dataset)) +
      geom_bar(aes(fill = ColorGroup), stat = "identity", position = "dodge") + # here is the change
      geom_smooth(data = subset(anomaly_mean, Dataset == "DWD"),
                  method = "loess", aes(x = factor(Year), y = Anomaly, color = "DWD"),
                  size = 1, se = FALSE, span = 0.4) + 
      geom_smooth(data = subset(anomaly_mean, Dataset == "ERA5"),
                  method = "loess", aes(x = factor(Year), y = Anomaly, color = "ERA5"),
                  size = 1, se = FALSE, span = 0.4)