Search code examples
rggplot2axis-labels

adding a label in geom_line in R


I have two very similar plots, which have two y-axis - a bar plot and a line plot:

enter image description here

code:

sec_plot <- ggplot(data, aes_string (x = year, group = 1)) +
    geom_col(aes_string(y = frequency), fill = "orange", alpha = 0.5) +
    geom_line(aes(y = severity))  

However, there are no labels. I want to get a label for the barplot as well as a label for the line plot, something like:

enter image description here

How can I add the labels to the plot, if there is only pone single group? is there a way to specify this manually? Until know I have only found option where the labels can be added by specifying them in the aes

EXTENSION (added a posterior):

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar){

  if ("agegroup" %in% xvar) xvar <- get("agegroup")
  # data <- data[, startYear:= as.numeric(startYear)]

  data <- data[!claims == 0][, ':=' (scaled = get(yvarsec) * max(get(yvar))/max(get(yvarsec)),
                                     param  = max(get(yvar))/max(get(yvarsec)))]

  param <- data[1, param] # important, otherwise not found in ggplot

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
    geom_col(aes_string(y = yvar, fill = groupvar, alpha = 0.5), position = "dodge") +
    geom_line(aes(y = scaled,  color = gender)) +
    scale_y_continuous(sec.axis = sec_axis(~./(param), name = paste0("average ", yvarsec),labels = function(x) format(x, big.mark = " ", scientific = FALSE))) +
    labs(y = paste0("total ", yvar)) +
    scale_alpha(guide = 'none') + 
    theme_pubclean() +
    theme(legend.title=element_blank(), legend.background = element_rect(fill = "white"))
}

plot.ExposureYearly <- getSecPlot(freqSevDataAge, xvar = "agegroup", yvar = "exposure", yvarsec = "frequency", groupvar = "gender")
plot.ExposureYearly

How can the same be done on a plot where both the line plot as well as the bar plot are separated by gender?


Solution

  • Here is a possible solution. The method I used was to move the color and fill inside the aes and then use scale_*_identity to create and format the legends.
    Also, I needed to add a scaling factor for severity axis since ggplot does not handle the secondary axis well.

    data<-data.frame(year= 2000:2005, frequency=3:8, severity=as.integer(runif(6, 4000, 8000)))
    
    library(ggplot2)
    library(scales)
    
    sec_plot <- ggplot(data, aes(x = year)) +
      geom_col(aes(y = frequency, fill = "orange"), alpha = 0.6) +
      geom_line(aes(y = severity/1000, color = "black")) +
      scale_fill_identity(guide = "legend", label="Claim frequency (Number of paid claims per 100 Insured exposure)", name=NULL) +
      scale_color_identity(guide = "legend", label="Claim Severity (Average insurance payment per claim)", name=NULL) +
      theme(legend.position = "bottom") +
      scale_y_continuous(sec.axis =sec_axis( ~ . *1, labels = label_dollar(scale=1000), name="Severity") ) +  #formats the 2nd axis
      guides(fill = guide_legend(order = 1),  color = guide_legend(order = 2))                                #control which scale plots first
    
    sec_plot
    

    enter image description here