Voulant étudier l'impact de la météo sur le trafic routier, j'aimerai réalisé un graphique avec comme axe x, les mois, comme axe Y à droite, les températures et précipitations et comme axe Y à gauche, le nombre de voiture.
Cependant, lorsque je génère ce graphique, les précipitations et températures disparaissent, puisque l'échelle de l'axe du nombre de voiture est beaucoup trop importante (de 0e+00 à 3e+06). Est-il possible d'avoir pour l'axe Y droite, une graduation allant de 0 à 30 tout en ayant une graduation différente pour l'axe Y à gauche?
Merci d'avance pour vos réponses, afin de mieux visualiser, ci-dessous le code.
Code : Tout sélectionner
structure(list(SOUNAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "WATERFORD (TYCOR)", class = "factor"), year_month = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("2013-03", "2013-05",
"2013-08", "2013-09", "2013-10", "2013-12"), class = "factor"),
pre_type = structure(c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L
), .Label = c("HEAVY", "LIGHT", "MEDIUM", "NONE"), class = "factor"),
pre_value = c(17L, 1L, 7L, 3L, 16L, 1L, 10L, 4L, 17L, 1L,
8L, 5L, 12L, 1L, 11L, 6L, 7L, 3L, 16L, 5L, 6L, 2L, 20L, 3L
), tem_type = structure(c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"), class = "factor"),
tem_value = c(0L, 15L, 0L, 16L, 0L, 28L, 3L, 0L, 0L, 0L,
30L, 0L, 0L, 12L, 16L, 0L, 0L, 30L, 1L, 0L, 0L, 26L, 0L,
5L), cnt_vehicle = c(NA, 2822180, NA, NA, NA, 3142510, NA,
NA, NA, 3372690, NA, NA, NA, 3321950, NA, NA, NA, 3352332,
NA, NA, NA, 3051846, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L)), .Names = c("SOUNAME", "year_month", "pre_type",
"pre_value", "tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA,
-24L), class = "data.frame")
Code : Tout sélectionner
ggplot(data = w_complet_2013, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
scale_x_continuous(breaks=1:6, labels=unique(w_complet_2013$year_month)) +
geom_bar(stat = "identity", width=0.3) +
xlab("date") + ylab ("Number of days of precipitation") +
ggtitle("Precipitation per month") + labs(fill = "Frequency") +
geom_bar(data=w_complet_2013,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
xlab("date") + ylab("Number of days of temperature") +
ggtitle("Temperatures and precipitations- WATERFORD") + labs(fill = "Frequency") + theme( plot.title = element_text(size = 12, face ="bold.italic")) +
geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), size = 1), show.legend=FALSE, stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(~.,name="Number of vehicle")) + theme( plot.title = element_text(size = 11, face ="bold.italic")) + theme(axis.title.x = element_text(size = 15)) + theme(axis.title.y = element_text(size = 15))