I'm trying to create one plot with two different scaled y-axes in ggplot2. I tried several different suggestions that I already found online, but it still does not work for my data .I've got count data as well as temperature data related to one specific month in a year:
$ Month : chr "11-2016" "12-2016" "01-2017" "10-2017" ...
$ Amount : int 67 118 2 62 60 60 53 89 189 223 ...
$ GroundTemp: num 3.8 0.5 -2.9 11.5 4.5 3.6 0.3 4.7 5.1 1.8 ...
$ AirTemp : num 3.9 0.68 -3.62 10.38 4.25 ...
I would like to create a plot, representing the month on the x-axis and the "Amount" as well as "AirTemp" on two different y-axes. Whereby the data refering to "Amount" should be represented as barplot and the "AirTemp" as a line.
That's the data set that I'm working with:
structure(list(Month = c("11-2016", "12-2016", "01-2017", "10-2017", "11-2017", "01-2018", "02-2018", "11-2018", "11-2019", "12-2019", "01-2020", "11-2020", "12-2020"), Amount = c(67L, 118L, 2L, 62L, 60L, 60L, 53L, 89L, 189L, 223L, 31L, 138L, 10L), GroundTemp = c(3.8, 0.5, -2.9, 11.5, 4.5, 3.6, 0.3, 4.7, 5.1, 1.8, 1.4, 10.3, 1.6 ), AirTemp = c(3.9, 0.68, -3.62, 10.38, 4.25, 4.3, -2.09, 4.89, 4.64, 3.01, 2.51, 9.38, 2.41)), class = "data.frame", row.names = c(NA, -13L))
I tried many different ways of getting there and I think that's the closes one:
geom_line(aes(y=AirTemp,color="2"),linetype=1, size=2)+
geom_point(aes(y=AirTemp, color="2"),size=4)+
geom_line(aes(y=Amount,color="1"),linetype=1, size=2)+
geom_point(aes(y=Amount, color="1"),size=4)
That's the resulting plot, colour 1 represents the "Amount" and colour 2 the "AirTemp"
I don't get any further than this. Could you may help me out here?
Thanks