Identical continuous color palettes in ggplot

30 Views Asked by At

I am trying to plot two graphs with the same continuous color palette. In one plot the range for the color palette to represent is 0-0.48 with the vast majority of points between 0-0.18. The other plot's range is 0-0.08. I want to use the same color palette on both plots and have the same colors associated with the same value.

Below is a reproducible example (with the real data for color):

color_range_df1 <- c("0.005","0.000","0.000","0.007","0.007","0.006","0.005",
                     "0.002","0.063","0.001","0.101","0.151","0.006","0.178",
                     "0.140","0.150","0.130","0.155","0.175","0.175","0.152",
                     "0.006","0.058","0.167","0.151","0.000","0.050","0.015",
                     "0.079","0.003","0.043","0.105","0.078","0.091","0.006",
                     "0.159","0.089","0.151","0.002","0.004","0.174","0.087",
                     "0.050","0.172","0.099","0.018","0.060","0.115","0.014",
                     "0.000","0.004","0.000","0.000","0.000","0.000","0.000",
                     "0.135","0.003","0.003","0.006","0.000","0.000","0.000",
                     "0.488","0.000","0.003","0.000","0.048","0.000","0.000",
                     "0.133","0.161","0.001","0.079","0.000","0.139","0.000",
                     "0.041","0.001","0.175","0.101","0.058","0.180","0.140")

color_range_df2 <- c("0.057","0.022","0","0.009","0.049","0.048","0.055","0.048",
                     "0.031","0.049","0.043","0.047","0.06","0.059","0.049","0.045",
                     "0.042","0.049","0","0","0","0","0","0","0","0.034","0.056",
                     "0.05","0.046","0.053","0.033","0.047","0.047","0.031",
                     "0.046","0.084","0.079")


df1_bigrange_and_outlier<- data.frame(X = 1:84, y = 1:84, color = color_range_df1)
df2_smallrange <- data.frame(X = 1:37, y = 1:37, color = color_range_df2)


ggplot(data = df1_bigrange_and_outlier, aes(x = X, y = y, color = color)) +
  geom_point()

In this example, I want the 0.488 point to be a very distinct color, and there to be a visual difference between 0-0.05 and 0-0.18.

I've tried

pal <- c(colorRampPalette(c("#c9e2f6", "#95cbee", "#0099dc",
                             "#0D98BA","#4ab04a", "#C0FF02", "#ffd73e"))(10),
          colorRampPalette(c("#e29421", "#e29421", "#f05336", "#ce472e", "#8B0000"))(30))

which works well for plot 1, but I'm not sure how to translate that to the second plot, where the range only goes from 0-0.08 instead of 0-0.48.

1

There are 1 best solutions below

0
Limey On BEST ANSWER

It's buried deep in the online doc, but you can use limits to specify the range of your colour scale. However, aesthetics are going to be an issue: with disparate ranges like this, discrimination will be a problem even after you've addressed @stefan's point.

Here's something that may get close to what you want, with a small modification of your original set up:

df1_bigrange_and_outlier<- data.frame(X = 1:84, y = 1:84, color = as.numeric(color_range_df1))
df2_smallrange <- data.frame(X = 1:37, y = 1:37, color = as.numeric(color_range_df2))


ggplot(
  data = df1_bigrange_and_outlier, 
  aes(x = X, y = y, color = color)
) +
  geom_point() +
  scale_color_steps(
    limits = c(0, 0.5), 
    low = scales::muted("blue"), 
    high = scales::muted("red")
)

enter image description here

ggplot(
  data = df2_smallrange, 
  aes(x = X, y = y, color = color)
) +
  geom_point() +
  scale_color_steps(
    limits = c(0, 0.5), 
    low = scales::muted("blue"), 
     high = scales::muted("red")
)

enter image description here