Search code examples
rggplot2geom-bargeom-text

R ggplot2 ordering of labels (geom_text)


I have a plot, which displays a combination of a bar chart and a line chart. Now I want to add labels to the plot, but they are all plotted in a line instead of the right position.

Here is the df I am using:

df1 <- data.frame(cat = c("category1", "category2", "category3", 
                            "category1", "category2", "category3",
                            "category1", "category2", "category3"), 
                  source = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
                  value = c(sample (c(1000L:6000L),9, replace = FALSE)),
                  stringsAsFactors = FALSE)

And the code for the plotting:

library(dplyr)
library(ggplot2)
library(scales)

ggplot(df1) +
  geom_bar(data = filter(df1, source %in% c("a", "b")), aes(cat, value, fill = source), stat="identity", position = position_dodge()) +
  geom_point(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source))+
  geom_line(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source , group = source)) +
  labs(x= "categories", y= "a and b (bars)") +
  scale_y_continuous(sec.axis = sec_axis(~.*2000/5000, name = "c (line)"), 
                     labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
  scale_fill_manual(values= c("darkseagreen4", "darkseagreen3")) +
  geom_text(aes(label=value, x = cat, y = value), vjust = 1.5, size = 3) +
  theme_light() 

Does anybody know how to get the labels in the right position?


Solution

  • Having two different 'plots' in a single plot will force you to also have two different ways to include the labels.

    The problem of having the labels on each bar is solved using position = position_dodge2(), but this won't work with the line. Just include a new geom_text for it.

    df1 <- data.frame(cat = c("category1", "category2", "category3", 
                              "category1", "category2", "category3",
                              "category1", "category2", "category3"), 
                      source = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
                      value = c(sample (c(1000L:6000L),9, replace = FALSE)),
                      stringsAsFactors = FALSE)
    
    library(dplyr)
    library(ggplot2)
    library(scales)
    
    ggplot() +
      geom_col(data = filter(df1, source %in% c("a", "b")), aes(cat, value, fill = source), position = position_dodge()) +
      geom_text(data = filter(df1, source %in% c("a", "b")), aes(cat, value,label=value, group = source), vjust = 1.5, size = 3, position = position_dodge(width = 1)) +
    
      geom_point(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source)) +
      geom_line(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source , group = source)) +
      geom_text(data = filter(df1, source == "c"), aes(cat, value*5000/2000, label=value), vjust = 1.5, size = 3) +
    
      labs(x= "categories", y= "a and b (bars)") +
      scale_y_continuous(sec.axis = sec_axis(~.*2000/5000, name = "c (line)"), 
                         labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
      scale_fill_manual(values= c("darkseagreen4", "darkseagreen3")) +
      theme_light() 
    

    Created on 2018-05-25 by the reprex package (v0.2.0).