Search code examples
rggplot2

Show all data labels in combined graph and modify legend


I created a graph using the code

name <- rep(c("ba", "EW", "RW", "Se", "St", "US", "VN"), 2)
value <-  c(0, 6323, 7397, 13945, 11801, 55255, 22519, 4124, 13540, 9616, 57724, 6646, 22021, 8841)
type <- c(rep("g", 7), rep("o", 7))
test <- data.frame(name, value, type)

name2 <- c("ba", "EW", "RW", "Se", "St", "US", "VN")
share <- c(1, .61, .47, .8, .34, .28, .28) 
test2 <- data.frame(name2, share)

scaling_factor <- 8e4

ggplot(test, aes(x=name, y=value, fill=type)) + 
  geom_bar(position = position_stack(reverse = TRUE), stat='identity') + 
  scale_fill_manual(values = c("#0099f9", "#69c6ff")) + 
  geom_text(aes(label = value), position = position_stack(vjust = 0.5, reverse = TRUE), colour = "black", size = 3) + 
  guides(fill = guide_legend(title = "", reverse = TRUE)) + 
  geom_line(data = test2, 
            aes(x = name2, y = share * scaling_factor, fill = NULL, group = 1), color = "orange", size = 1) + 
  scale_y_continuous(name = "axis 1", sec.axis = dup_axis(~./80000, name = "axis 2", labels = scales::percent)) + 
  geom_point(data = test2, aes(x = name2, y = share * scaling_factor, fill = NULL, group = 1), color = "orange", size = 3)

the graph looks like this graph here

I want to add data label for the line and show the value from share as percentage, but the existing labels in geom_text for the stacked bars are always overwritten whenever I want to add it. How can I add it correctly and avoid they overlap?

And I tried to show the line as line with marker (similar as in Excel) by combining geom_line and geom_point, but in the legend the yellow dot and blue bars are also combined. I expected the yellow line and two blue bars are shown separately, how can I correct this?


Solution

  • Are you looking for something like this? Notice how I set the color in geom_line and geom_point and then used scale_color_manual.

    library(ggplot2)
    name <- rep(c("ba", "EW", "RW", "Se", "St", "US", "VN"), 2)
    value <-  c(0, 6323, 7397, 13945, 11801, 55255, 22519, 4124, 13540, 9616, 57724, 6646, 22021, 8841)
    type <- c(rep("g", 7), rep("o", 7))
    test <- data.frame(name, value, type)
    
    name2 <- c("ba", "EW", "RW", "Se", "St", "US", "VN")
    share <- c(1, .61, .47, .8, .34, .28, .28) 
    test2 <- data.frame(name2, share)
    
    scaling_factor <- 8e4
    
    ggplot(test, aes(x=name, y=value)) + 
      geom_bar(aes(fill=type), position = position_stack(reverse = TRUE), stat='identity') + 
      scale_fill_manual(values = c("#0099f9", "#69c6ff")) + 
      geom_text(aes(label = value), position = position_stack(vjust = 0.5, reverse = TRUE), colour = "black", size = 3) + 
      guides(fill = guide_legend(title = "", reverse = TRUE)) + 
      geom_line(data = test2, 
                aes(x = name2, y = share * scaling_factor, group = 1, color = "percent"), size = 1) + 
      scale_y_continuous(name = "axis 1", sec.axis = dup_axis(~./80000, name = "axis 2", labels = scales::percent)) + 
      geom_point(data = test2, aes(x = name2, y = share * scaling_factor, color = "percent"), size = 3) +
      geom_text(aes(x=name2, y = share * scaling_factor, label = paste0(share*100, "%")), 
                data = test2, vjust = 1, colour = "black", size = 3) +
      scale_color_manual(values = c(percent = "orange")) + labs(color = "")
    #> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
    #> ℹ Please use `linewidth` instead.
    #> This warning is displayed once every 8 hours.
    #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    #> generated.
    

    Plot with bars, line and points

    Created on 2024-11-06 with reprex v2.1.1