Home > database >  How to do expand_limits to a factor and keep the existing color scheme and display the na.value box
How to do expand_limits to a factor and keep the existing color scheme and display the na.value box

Time:08-01

I wish to make 2 plots.

The first plot is :

library(ggplot2)

set.seed(123)

df  <- data.frame(x = sample(c("A", "B","C", "D"), 100, replace = TRUE),
                  y = as.factor(sample(c("North", "South", "West", NA), 100, replace = TRUE)))

ggplot(df)  
  aes(x, fill = y)  
  geom_bar()  
  scale_fill_discrete(type  = c("red",
                                "green",
                                "blue"),
                      na.value = "black")

First_Image

The second plot should be the same as the first plot but it should have an extra key in the legend.

ggplot(df)  
  aes(x, fill = y)  
  geom_bar()  
  scale_fill_discrete(type  = c("red",
                                "green",
                                "blue"),
                      na.value = "black")  
    expand_limits(fill=("East"))

Second_image When I try to do this the legend in the second plot does not have the key corresponding to NA and the colors used change. How can I fix these 2 issues ?

Please Note: I wish to make "comparable plots" by doing this. One plot should have a subset of colors of the other plot but they should have the same color scheme AND the legend should be the same in both.

The plot with the superset of colors is easy. It is the plot with the subset of colors which needs the above fixing of expand_limits.

CodePudding user response:

The issue with the colors in your second plot is that by expanding the limits you add an additional category while providing only three colors. According to the docs ?scale_fill_discrete the type argument could be

A character vector of color codes. The codes are used for a 'manual' color scale as long as the number of codes exceeds the number of data levels (if there are more levels than codes, scale_colour_hue()/scale_fill_hue() are used to construct the default scale)

Hence, for your second plot you get the default ggplot2 colors.

Concerning the issue with the removed legend key for the NA I can only guess what's happening under the hood when you expand the limits.

But a fix would be to be more explicit about what you want to do. First, I would suggest to explicitly use scale_fill_manual if you want to set a manual palette. Second, if you want to add another category not present in your data then add this category to the levels of your factor, add an additional color and use drop=FALSE to prevent the catgeory from being dropped.

library(ggplot2)

set.seed(123)

df  <- data.frame(x = sample(c("A", "B","C", "D"), 100, replace = TRUE),
                  y = as.factor(sample(c("North", "South", "West", NA), 100, replace = TRUE)))

ggplot(df)  
  aes(x, fill = y)  
  geom_bar()  
  scale_fill_manual(values  = c("red",
                                "green",
                                "blue"),
                    na.value = "black")


ggplot(df)  
  aes(x, fill = factor(y, levels = c(levels(y), "East")))  
  geom_bar()  
  scale_fill_manual(values  = c("red",
                                "green",
                                "blue",
                                "yellow"),
                    na.value = "black", drop = FALSE)  
  labs(fill = "y")

  • Related