0

I have the following tibble format and i want to create a chart with two y-axis.

sample <- climate <- tibble(
  Month = c("1/1/2019","2/1/2019","3/1/2019","4/1/2019","5/1/2019","6/1/2019","7/1/2019","8/1/2019","9/1/2019","10/1/2019","11/1/2019","12/1/2019","1/1/2020","2/1/2020","3/1/2020"),
  Reactions = c(52111,37324,212695,152331,24973,10878,7413,8077,13066,50486,8087,12600,31625,25578,20069),
  Ratio = c(1371,1866,6445,4914,925,363,218,245,335,1530,352,525,1506,1112,873)
)

Here's what i tried so far.

ggplot() + 
  geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
  geom_line(mapping = aes(x = sample$Month , y = sample$Ratio), size = 2, color = "red") + 
  scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))

Any help will be appreciated

RaV
  • 617
  • 1
  • 5
  • 11

1 Answers1

2

you have to recode Month column as date, and multiply Ratio times 20 (since you devided second axis by 20):

library(lubridate)

sample$Month <- mdy(sample$Month)
ggplot() + 
  geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
  geom_line(mapping = aes(x = sample$Month , y = sample$Ratio*20), size = 2, color = "red") + 
  scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))

you can also improve your code with use of data variable inside ggplot()

ggplot(sample, aes(x = Month)) + 
  geom_bar(aes(y = Reactions), stat = 'identity') +
  geom_line(aes(y = Ratio*20), size = 2, color = "red") + 
  scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))

Plot: Result plot

RaV
  • 617
  • 1
  • 5
  • 11
  • Hi RaV, thank you for your answer.Thing is, the line is now attributed to the same y-axis as the bars, while i want it to be to the right-hand sight axis. As you can see the line never reaches the point of say, 6445 – Xristos Solwmou Mar 31 '20 at 11:08
  • @XristosSolwmou check it now - I've noticed this issue after I'd already posted answer and addressed it by multiplying _Ratio_. – RaV Mar 31 '20 at 11:11
  • It works fine thank you very much. Can you please let me know what does the group = 1 does? – Xristos Solwmou Mar 31 '20 at 11:13
  • 1
    @XristosSolwmou I updated my solution. The `group = 1` solves the problem with factor variable, you can read about it under [this question](https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation). However, I've noticed that the right solution in your case is to cast _Month_ column as date type. Sorry for the confusion. – RaV Mar 31 '20 at 11:27
  • Hi RaV thank you for all of your help. Can you please let me know if possible how i can add a legend to the chart as well? – Xristos Solwmou Mar 31 '20 at 12:55
  • @XristosSolwmou of course. Can you describe what type of legend you want to include on this plot? – RaV Mar 31 '20 at 13:02
  • I would like a legend on the right giving the line its name. For example Likes / Reactions. Thanks – Xristos Solwmou Mar 31 '20 at 13:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210664/discussion-between-rav-and-xristos-solwmou). – RaV Mar 31 '20 at 13:07