Unwanted characters in GGPlot 2 legend entries for stacked area chart

60 views Asked by At

Good day,

I need to prepare a graph showing the proportions of the various biomass components, but it is showing ,1 after each component in the legend in R, Ggplot2. I only want to see the list of components in the legend without the ,1 after each entry. See below the sample data, the code I have used and the graph with the unwanted mistake.

Also, the graph shows that the components(Cone, needle, bark, branch) are increasing with an increase in DBH, while they should be decreasing. Only stem increase with an increase in DBH.

Sample data:

Sample data

Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")

DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)

plotly::ggplotly(
  ggplot(DRProportions[order(DRProportions$Component),], aes(x = DBH, y = Percentage, fill =    Component)) +
    geom_area(alpha=0.6 , linewidth=.5, colour="white") +
    scale_colour_manual("", values = c(Cone="deepskyblue", Needle="darkslategray",    Bark="darkseagreen", Branch="deeppink2", Stem="darksalmon"))+  
    labs(y="Percentage (%)", x="DBH (cm)")+  
    theme(axis.text = element_text(size = 14))+
    theme(axis.title = element_text(size = 15))+
    theme(legend.text = element_text(size = 15)))

Graph with mistake:

Graph with mistake

I have rearranged the order of the legend entries with the associated colors assigned to them. That is where the ,1 error slipped in. Don't know how to modify the code further to get rid of the ,1. Also, all components shows an increasing order, while it should be only stem.

2

There are 2 answers

1
stefan On

The issue is that you map on the fill aes but add a manual color scale. Instead, switch to scale_fill_manual to apply your custom colors and to get your desired legend labels.

Using some fake random example data:

Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")

library(plotly)

set.seed(123)

DRProportions <- expand.grid(
  Component = Component_order,
  DBH = seq(10, 40, 5)
)
DRProportions$Percentage <- runif(nrow(DRProportions))
DRProportions$Percentage <- ave(DRProportions$Percentage, DRProportions$DBH, FUN = \(x) x / sum(x))

names(colours_order) <- Component_order

DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)

gg <- ggplot(
  DRProportions,
  aes(x = DBH, y = Percentage, fill = Component)
) +
  geom_area(alpha = 0.6, linewidth = .5, colour = "white") +
  scale_fill_manual(NULL, values = colours_order) +
  labs(y = "Percentage (%)", x = "DBH (cm)") +
  theme(axis.text = element_text(size = 14)) +
  theme(axis.title = element_text(size = 15)) +
  theme(legend.text = element_text(size = 15))

plotly::ggplotly()

1
Andy Baxter On

To try and recreate your graph with some fake data, there's essentially two things happening:

  • Your scale_colour_manual seems to be messing up grouping. Use scale_fill_manual instead:
library(tidyverse)

df <- tibble(
  DBH = rep(seq(12, 40, 3), times = 5),
  Component = rep(c("Cone", "Needle", "Bark", "Branch", "Stem"), each = 10),
  Percentage = c(
    rep(1, 10),
    10:1,
    rep(15, 20),
    59:68
  )
)

Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")

df$Component <- factor(df$Component, levels = Component_order)

plotly::ggplotly(
ggplot(df[order(df$Component),], aes(x = DBH, y = Percentage, fill =    Component)) +
  geom_area(alpha=0.6 , linewidth=.5, colour="white") +
  scale_fill_manual("", values = c(Cone="deepskyblue", Needle="darkslategray",    Bark="darkseagreen", Branch="deeppink2", Stem="darksalmon"))+  
  labs(y="Percentage (%)", x="DBH (cm)")+  
  theme(axis.text = element_text(size = 14))+
  theme(axis.title = element_text(size = 15))+
  theme(legend.text = element_text(size = 15)))

  • Your approach to doing stacked area is indeed giving three unchanging groups and one reducing group on top of the increasing group. But because the straight lines are stacked on a slope they look like they're increasing. Would geom_line suit your purposes better?
plotly::ggplotly(
ggplot(df[order(df$Component),], aes(x = DBH, y = Percentage, colour = Component)) +
  geom_line() +
  scale_colour_manual("", values = c(Cone="deepskyblue", Needle="darkslategray",    Bark="darkseagreen", Branch="deeppink2", Stem="darksalmon"))+  
  labs(y="Percentage (%)", x="DBH (cm)")+  
  theme(axis.text = element_text(size = 14))+
  theme(axis.title = element_text(size = 15))+
  theme(legend.text = element_text(size = 15)))