Home > Blockchain >  change alternations in geom_col dodge between fill and alpha
change alternations in geom_col dodge between fill and alpha

Time:01-27

I got a bar graph with both alpha and fill. I would like the plot to alternate with alpha and mix colors. Currently, I only get it to mix alpha and order by colors (see MWE below). Any help towards changing the ordering would be very appreciated.

library(tibble)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
set.seed(1)
tibble(
  y = runif(20),
  x = letters[rep(1:2, times = 10)],
  f = factor(LETTERS[rep(1:2, each = 10)], levels = LETTERS[1:2], ordered = TRUE),
  a = factor(rep(1:5, times = 4), levels = 1:5, ordered = TRUE)
) %>% 
  ggplot(aes(x, y, fill = f, alpha = a))  
  geom_col(position = position_dodge2(1))  
  scale_alpha_discrete(range = c(.5, 1))
#> Warning: Using alpha for a discrete variable is not advised.

Created on 2023-01-26 with reprex v2.0.2

I.e., I am trying to get dark yellow mixed with dark purple and light/transparent yellow with light/transparent purple.

CodePudding user response:

Perhaps like this ..., i.e. simply switch the order of fill and alpha to change the grouping:

library(tibble)
library(ggplot2)

set.seed(1)

tibble(
  y = runif(20),
  x = letters[rep(1:2, times = 10)],
  f = factor(LETTERS[rep(1:2, each = 10)], levels = LETTERS[1:2], ordered = TRUE),
  a = factor(rep(1:5, times = 4), levels = 1:5, ordered = TRUE)
) %>% 
  ggplot(aes(x, y, alpha = a, fill = f))  
  geom_col(position = position_dodge2(1))  
  scale_alpha_discrete(range = c(.5, 1))
#> Warning: Using alpha for a discrete variable is not advised.

  • Related