1

I want to plot a stacked bar where the upper part is colored in the same way but with alpha. The images below give an idea, what I want is to have the magma colors, but the upper part with alpha. I wonder if it helps more to have two plots and "stack" them together. The upper part (alpha) is the tam_loc variable in the dataset. That is rural areas.

If I put alpha in aes it produces the output, but I cannot manipulate the alpha degree. Further, if I do this the legend now has a bar (mean value) for non alpha values. See image Not useful legend

The data is here.

My code looks like this:

  ggplot(locPCP, aes(reorder(x=NOMGEO, -mean),y=mean, fill=bin, alpha=tam_loc)) + 
  geom_bar(stat="identity") +
  scale_fill_manual(labels = c("> 8", "3.5 - 4", "3 - 3.5", "2.5 - 3", "2 - 2.5", "< 2"),
                    values = c(state_colors[2:7])) +
  scale_x_discrete(name="State") + 
  scale_y_continuous(name="Emissions per capita (t CO2eq/year)", limits=c(0, 15)) +
  geom_hline(yintercept=pcp2015, linetype="dashed", 
             color = "black", size=1) +
  geom_hline(yintercept=pcp2030, linetype="dashed", 
             color = "red", size=1) +
  geom_hline(yintercept=pcp2018, linetype="dashed", 
             color = "blue", size=1) +
  theme_classic() +
  theme(legend.position="top",
        legend.text = element_text(size=15, color="black"),
        legend.title = element_text(size=15, color="black"),
        axis.text.x = element_text(size=15, color="black", angle = 90, vjust = 0.2, hjust=1),
        axis.text.y = element_text(size=15, color="black"),
        axis.title.x = element_text(size=15, color="black", vjust=-1),
        axis.title.y = element_text(size=15, color="black")) +
  geom_errorbar(aes(ymax=meanS, ymin=meanS), colour="white", width=0.5, size=1) # http://www.cookbook-r.com/Graphs/Lines_(ggplot2)/ 

Classified by locality

Leading color by region

Daniel
  • 218
  • 1
  • 2
  • 9
  • 2
    Sure is that possible. But it will probably require some reshaping of your data. For more help I would suggest to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Sep 15 '22 at 09:56
  • I have provided all the data. – Daniel Sep 15 '22 at 10:00
  • You haven't told us what it is that you want to stack. What would the darker areas on the top represent? What variable do you wish to display with the different alpha? – Allan Cameron Sep 15 '22 at 10:13
  • I have added more text. Basically, the darker areas would be at the bottom. – Daniel Sep 15 '22 at 10:17
  • It's not clear what you need to solve - you already have a stacked plot distinguished by alpha. What do you mean by `alpha degree`? Do you just want to fine-tune the amount of transparency? In that case add `scale_alpha_manual` and specify your desired alpha levels (between 0 and 1) in the `values` argument. – Ottie Sep 15 '22 at 10:29

1 Answers1

3

You would simply map alpha to tam_loc and specify a scale_alpha_manual scale. Specify guide = "none" inside the alpha scale if you don't want it to appear in the legend. If you want a guide without the horizontal bar, use key_glyph = draw_key_blank inside geom_errorbar, which is where this line is coming from.

Remember, every fill color has an alpha value. A completely solid color that you cannot see through has an alpha value of 1, and a completely invisible color has an alpha of 0. It becomes confusing when you start to talk about the upper parts of the bars "having alpha" - it's more accurate to say that the upper bars have a lower alpha value.

ggplot(locPCP, aes(reorder(x=NOMGEO, -mean), y = mean, fill = bin, 
                   alpha = tam_loc)) + 
  geom_col() +
  geom_hline(yintercept = pcp2015, linetype = "dashed", 
             color = "black", size = 1) +
  geom_hline(yintercept = pcp2030, linetype = "dashed", 
             color = "red", size = 1) +
  geom_hline(yintercept=pcp2018, linetype="dashed", 
             color = "blue", size = 1) +
  geom_errorbar(aes(ymax = meanS, ymin = meanS), colour = "white",
                width = 0.5, size = 1, key_glyph = draw_key_blank) +
  scale_fill_manual(labels = c("> 8", "3.5 - 4", "3 - 3.5", 
                               "2.5 - 3", "2 - 2.5", "< 2"),
                    values = c(state_colors[2:7])) +
  scale_x_discrete(name = "State") + 
  scale_y_continuous(name = "Emissions per capita (t CO2eq/year)", 
                     limits = c(0, 15)) +
  scale_alpha_manual(values = c(0.5, 1), name = 'Location') +
  theme_classic(base_size = 15) +
  theme(legend.position = c(0.5, 1), 
        legend.box = 'vertical', 
        legend.direction = 'horizontal',
        legend.justification = c(0.5, 1),
        axis.text.x = element_text(angle = 90, vjust = 0.2, hjust = 1),
        axis.text.y = element_text(size = 15, color="black"),
        axis.title.x = element_text(vjust = -1))

enter image description here

Incidentally, you have some variables defined in your code that are not present in your question. I have guessed at them here.

state_colors <- viridisLite::magma(8)
pcp2015 <- 5
pcp2018 <- 3
pcp2030 <- 2.5
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • If I remove the `none` in `scale_alpha_manual(values = c(0.5, 1), guide = 'none')` to specify that `alpha=1` is equal to urban and `alpha=0.5` is equal to rural, is there any way to remove the bar that appears in the middle? Further, how could I add move the alpha to be below the box colors? – Daniel Sep 15 '22 at 11:58
  • @Daniel see my update - is that what you're looking for? – Allan Cameron Sep 15 '22 at 12:11
  • @Daniel. Very nice looking plot by the way. – Allan Cameron Sep 15 '22 at 12:22