Freya Slootmans
Freya Slootmans

Reputation: 37

ggplot graph with double y-axis

I'm trying to make a graph with a double y-axis.

My data looks like this:

    year      variable          value
1   2009    Letselongevallen    4455
2   2010    Letselongevallen    4337
3   2011    Letselongevallen    4710
4   2012    Letselongevallen    4666
5   2013    Letselongevallen    4460
6   2014    Letselongevallen    4400
7   2015    Letselongevallen    4284
8   2016    Letselongevallen    4467
9   2017    Letselongevallen    4199
10  2018    Letselongevallen    4433
11  2009    Doden   21
12  2010    Doden   20
13  2011    Doden   22
14  2012    Doden   26
15  2013    Doden   25
16  2014    Doden   30
17  2015    Doden   28
18  2016    Doden   26
19  2017    Doden   17
20  2018    Doden   16

Both "letselongevallen and "doden" need to be on a different axis.

This is the code I wrote

hitrun_graph = ggplot(hitrun_graph1,
                     aes(x = year,
                         y = value, 
                         group = variable,
                         colour = variable)) +
              theme_minimal(base_size = 16) +
              geom_line(size = 2.25) +
              scale_y_continuous(name = "Letselongevallen", limits = c(0,5000),
                                 sec.axis = sec_axis(~./ 142, name = "Doden 30 dagen")) +
              scale_fill_manual(ColorsAll) +
              labs(x = NULL) +
              theme(legend.title = element_blank(),
                    legend.text=element_text(size = 22),
                    axis.text.x = element_text(size = 22),
                    axis.text.y = element_text(size = 22),
                    legend.position = "right")

This gives me a graph with a double y-axis, but how do I force R to put "Doden" on the axis on the right?

enter image description here

Upvotes: 2

Views: 196

Answers (1)

Dij
Dij

Reputation: 1378

You could use facet_grid and set scales = "free". You'll also need to remove the code that manually sets the axes in that case.

ggplot(hitrun_graph1,
  aes(x = year,
      y = value, 
      group = variable,
      colour = variable)) + 
  theme_minimal(base_size = 16) +
  geom_line(size = 2.25) +
  labs(x = NULL) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size = 22),
        axis.text.x = element_text(size = 22),
        axis.text.y = element_text(size = 22),
        legend.position = "right") + 
  facet_grid(rows = vars(variable), scales = "free")

enter image description here

Here is the data for the example:

structure(list(year = c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018), variable = c("Letselongevallen", "Letselongevallen", 
"Letselongevallen", "Letselongevallen", "Letselongevallen", "Letselongevallen", 
"Letselongevallen", "Letselongevallen", "Letselongevallen", "Letselongevallen", 
"Doden", "Doden", "Doden", "Doden", "Doden", "Doden", "Doden", 
"Doden", "Doden", "Doden"), value = c(4455, 4337, 4710, 4666, 
4460, 4400, 4284, 4467, 4199, 4433, 21, 20, 22, 26, 25, 30, 28, 
26, 17, 16)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -20L), spec = structure(list(cols = list(
    X1 = structure(list(), class = c("collector_double", "collector"
    )), year = structure(list(), class = c("collector_double", 
    "collector")), variable = structure(list(), class = c("collector_character", 
    "collector")), value = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

Upvotes: 1

Related Questions