0
votes

In R I'm trying to plot a graph of bars and lines/points using ggplot2 but I have a difficulty with legends and colour settings.

First of all, this is my data frames.

vol_reshape <- data.frame(date = c("2018-01-01", "2018-02-01", "2018-03-01", "2018-04-01"),
                          variable = rep("total", 4),
                          value = as.integer(c("8029", "8164", "9536", "9482")))

qua_reshape <- data.frame(date = rep(c("2018-01-01", "2018-02-01", "2018-03-01", "2018-04-01"), 4),
                          variable = c(rep("quality_1", 4), rep("quality_2", 4), rep("quality_3", 4), rep("pod", 4)),
                          value = as.double(c("0.26", "0.26",   "0.30", "0.32", "0.27", "0.27", "0.30", "0.32", "0.45", "0.42", "0.51", "0.55", "0.05", "0.04", "0.05", "0.05")))

I want to plot a bar graph using vol_reshape, and on this bar graph, I want to plot points and lines using qua_reshape. Here is my output.

enter image description here

Here are the problems I have:

  1. Legends

Apparently now I have redundant and weird legends. What I want to have is one legend showing that the bar is "Total Volume", and another legend showing that each point/line indicate "Détectée automatique", "Détectée automatique ou déclarée par le conducteur", "Détectée automatique, déclarée par le conducteur ou par l'exploitant", "Rémontée POD".

  1. Colours

The colours of this plot is really bad. I set the colour variables cols so that the bar is blue, three lines/points(quality_1 ~ 3) are black, and pod line/point is orange. I set these values in scale_fill_manualbut it doesn't reflect my desire.

It would be great if someone help me solve these problems.

Here is my attempt.

    p <- ggplot(data = vol_reshape[which(vol_reshape$value > 0),], 
                aes(x = date, y = value, label = value, 
                    fill = variable
                    )
                ) +
      geom_bar(stat = "identity", position = "stack", show.legend = T) +   
      theme(legend.title=element_blank()) + 
      geom_text(size = size$label,  position = position_stack(vjust = 0.9), color = "#FFFFFF") +                        
      geom_point(data = qua_reshape, mapping = aes(x = date,
                                                   y = value *max(vol_reshape$value),
                                                   color = quality
                                                   )
                 ) +
      theme(legend.title=element_blank()) + 

      geom_line(data = qua_reshape, mapping = aes(x = date,
                                                  y =value*max(vol_reshape$value),
                                                  color = variable),
                size = size$line) +
      geom_text(data = qua_reshape, mapping = aes(x = date, 
                                                  y =value*max(vol_reshape$value), 
                                                  label =paste0(100*value, '%'),
                                                  color = variable),
                size = size$label, vjust = -0.9, hjust = 1.5
                ) +
      theme_classic() +

      scale_y_continuous(sec.axis = sec_axis(trans = ~.*(1/max(vol_reshape$value)))) +
      scale_fill_manual(name = NULL, values = cols, labels = labs, drop = T) +

      theme(legend.position = "right",
        plot.title = element_text(hjust = 0.5, size = size$title, family="Proxima Nova"),
        plot.subtitle = element_text(size = size$subtitle, family="Proxima Nova"),
        axis.title.x=element_blank(), 
        axis.text.x = element_text(angle = 45, margin=margin(t = 8), 
                                   family="Proxima Nova"))
        )

     cols <- c("total" = "#3D8BDA",  "quality_1" = "#000000", "quality_2" ="#000000",  "quality_3" = "#000000",  "pod" = "#ff8142")
     labs <- c("Total", "Détectée automatique", "Détectée automatique ou déclarée par le conducteur", "Détectée automatique, déclarée par le conducteur ou par l'exploitant", "Rémontée POD")
1

1 Answers

0
votes

You can try

ggplot() + 
    geom_col(data=vol_reshape, aes(x=as.POSIXct(date), y=value, fill=factor("Total"))) + 
    geom_line(data=qua_reshape, aes(x=as.POSIXct(date), y=value2, group=variable, color=variable), size=1.5) + 
    scale_fill_manual(name="", values="skyblue1") + 
    scale_color_manual(values = c("orange", rep(1, 3))) 

enter image description here