简体   繁体   中英

Legend and graph colors in ggplot2 do not match

I'm experiencing weird behaviour working on the colors of a graph in ggplot2 (I know it's probably just my bad code and not R that's acting werid)

When I change the colors in the graph the legend and plot does not match. For example if I replace lightcoral with blue then legend and plot doesn't match anymore?

Can anyone spot the mistake?

Here's how the graph look like:

在此处输入图像描述

This is the data:

df<- structure(list(compound = c("Acetaldehyde", "Acetaldehyde", "Acetone", 
"Acetone", "Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", 
"Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", 
"Acetaldehyde", "Acetone", "Acetone", "Acetone", "Acetone", "Acetone", 
"Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", 
"Acetone", "Acetaldehyde", "Acetone"), daysincubated4 = c(33, 
95, 33, 95, 33, 95, 33, 95, 4, 10, 17, 24, 66, 4, 10, 17, 24, 
66, 81, 94, 116, 81, 94, 116, 0, 0), emission_mean = c(24.5, 
0.4, 35.8, 2.8, 24.5, 0.4, 35.8, 2.8, 59.1, 45, 11.4, 6.7, 0.1, 
46.7, 44, 56.7, 29.1, 2.7, 0, 0.5, 0.6, 7.7, 0.4, 0.2, 26.1, 
28.5), se = c(11.6, 0.2, 9.4, 2.5, 11.6, 0.2, 9.4, 2.5, 11, 11.4, 
4.3, 3.5, 0, 9, 9.9, 22.8, 13.8, 1.2, 0, 0.2, 0.3, 6.4, 0.1, 
0.1, 5.9, 7.4), sampling_period = structure(c(2L, 3L, 2L, 3L, 
2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 1L, 1L), .Label = c("Thaw", "Anoxic", "Oxic"), class = "factor"), 
    category = c("mean", "mean", "mean", "mean", "mean", "mean", 
    "mean", "mean", "single", "single", "single", "single", "single", 
    "single", "single", "single", "single", "single", "single", 
    "single", "single", "single", "single", "single", "single", 
    "single")), row.names = c(NA, -26L), class = "data.frame")

Code for the graph:

(
df%>%
  ggplot(aes(x=daysincubated4, y=emission_mean))+
  labs(color = "", size= "") +
  labs(x = "Incubation time (days)", y = "Production (nmol g-1 dw soil h-1)") +
  facet_wrap(vars(compound), scales = "free_y") +
  geom_rect(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
            fill="lightblue3",alpha = 1.0)+
  geom_rect(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
            fill = "lightcoral", alpha = 1.0) +
  geom_rect(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
            fill="lightyellow2",alpha = 1.0) +
  geom_rect(aes(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
                fill="lightblue3"),alpha = 1.0)+
  geom_rect(aes(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
                fill = "lightcoral"), alpha = 1.0) +
  geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
                fill="lightyellow2"),alpha = 1.0)+
  geom_pointrange(aes(x = daysincubated4, y = emission_mean, ymin = emission_mean, ymax = emission_mean + se, 
                      color = category, shape = category, group = category, size=category))      +
  scale_color_manual(name = "", labels = c("Period Mean", "Emission"), 
                     values = c("black", "black")) +
  scale_shape_manual(name = "", labels = c("Period Mean", "Emission"), values = c(2, 19))+
  scale_size_manual(name = "", labels = c("Period Mean", "Emission"), values = c(0.7, 0.5))+
  scale_fill_manual(name = "", labels = c("Thaw", "Anoxic","Oxic"), values = c("lightblue3", "lightcoral","lightyellow2"))+
  theme_bw()
)

This is an ordering issue.

In your geom_rect

  1. First is Thaw,
  2. second is Anoxic,
  3. third is Oxic.

However scale_fill_manual orders it alphabetically:

  1. First Anoxic,
  2. second is Thaw and
  3. Oxic.

A < T < O:

You can solve this by setting breaks :

Change lightcoral to blue in geom_rect and scale_fill_manual Change this line

scale_fill_manual(name = "", labels = c("Thaw", "Anoxic","Oxic"), values = c("lightblue3", "lightcoral","lightyellow2"))+

to

 scale_fill_manual(name = "", labels = c("Thaw", "Anoxic","Oxic"), breaks = c("Thaw", "Anoxic", "Oxic"), values = c("lightblue3", "blue", "lightyellow2"))+

Update: See also comment of aosmith: to avoid the order issue, you could also use a named vector outside ggplot values = c(Thaw = "lightblue3", Anoxic = "blue", Oxic = "lightyellow2")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM