My current script and output figure are attached. The problem with the current script is that I cannot manage to plot Cars_Avg_Sunday, Cars_Carfree_sunday, Active_AvgSunday, Active_CarfreeSunday on the Y-AXIS on the right going from 0 to 400 instead of the left-axis from 0 to 40. I tried many alternative approaches but am not coming close to a real solution. The problem with the current script is thus that those 4 lines are plotted on the left instead the right y-axis.
Thanks in advance for your help in solving this issue!
Asthma_For_Excel <- data.frame(
time = c("07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00"),
NO2_Avg_Sunday = c(25, 24, 22, 22, 21, 20, 19, 18, 19, 20, 22, 26, 30, 34),
NO2_Carfree_Sunday = c(28.14285714, 27.28571429, 24.64285714, 18.78571429, 13.14285714, 9.571428571, 6.357142857, 6.071428571, 3.928571429, 3.642857143, 3.571428571, 4, 6.071428571, 18.28571429),
Cars_Avg_Sunday = c(54.7, 75.7, 103.6666667, 158.6666667, 157.6666667, 198.3333333, 205.6666667, 210.6666667, 255.3333333, 221.3333333, 244, 259.3333333, 252.6666667, NA),
CarS_Carfree_Sunday = c(65, 100, 61, 49, 64, 86, 98, 92, 94, 95, 80, 68, 162, NA),
Active_AvgSunday = c(26.66666667, 25.66666667, 39, 47, 87, 141, 139.6666667, 149.3333333, 158.3333333, 168.6666667, 155.3333333, 146, 107.3333333, NA),
Active_CarfreeSunday = c(9, 46, 58, 124, 194, 296, 301, 303, 429, 406, 304, 245, 128, NA)
)
# Remove rows with missing values
Asthma_For_Excel <- na.omit(Asthma_For_Excel)
# Plotting the data
ggplot(Asthma_For_Excel, aes(x = time, group = 1)) +
geom_line(aes(y = NO2_Avg_Sunday, colour = "NO2 Avg Sunday")) +
geom_line(aes(y = NO2_Carfree_Sunday, colour = "NO2 Car-Free Sunday")) +
geom_line(aes(y = Cars_Avg_Sunday, colour = "Cars Avg Sunday")) +
geom_line(aes(y = CarS_Carfree_Sunday, colour = "Cars Car-Free Sunday")) +
geom_line(aes(y = Active_AvgSunday, colour = "Active Avg Sunday")) +
geom_line(aes(y = Active_CarfreeSunday, colour = "Active Car-Free Sunday")) +
scale_y_continuous(
limits = c(0, 40),
expand = c(0, 0),
name = "NO2 Concentrations",
sec.axis = sec_axis(~.*10, name = "Traffic Counts" )
) +
labs(
x = "Time",
colour = "Legend"
)