Daniel
Daniel

Reputation: 263

Plot stacked barplot with upper part with alpha

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

Upvotes: 1

Views: 250

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174278

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

Upvotes: 3

Related Questions