Changer l'ordre de la légende - GGPLOT

Postez ici vos questions, réponses, commentaires ou suggestions - Les sujets seront ultérieurement répartis dans les archives par les modérateurs

Modérateur : Groupe des modérateurs

Elodie Perron
Messages : 19
Enregistré le : 18 Mai 2018, 06:36

Changer l'ordre de la légende - GGPLOT

Messagepar Elodie Perron » 14 Juin 2018, 10:26

Bonjour à tous,
J'aimerai changer l'ordre de ma légende (elle est automatiquement classée par ordre alphabétique).
J'aimerai obtenir : "NONE", "LIGHT","MEDIUM","HEAVY", "V_COLD","COLD,"MEDIUM","HOT"
J'ai d'abord définis les levels mais sans succès, je fais donc appel à votre aide !
Ci-dessous ma table :

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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "BALLYSHANNON (CATHLEENS FALL)", 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, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L), .Label = c("2013-03",
    "2013-04", "2013-05", "2013-06", "2013-07", "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, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
    3L, 4L, 1L, 2L, 3L), .Label = c("HEAVY", "LIGHT", "MEDIUM",
    "NONE"), class = "factor"), pre_value = c(13L, 2L, 11L, 5L,
    9L, 3L, 10L, 7L, 2L, 6L, 13L, 10L, 10L, 1L, 15L, 4L, 16L,
    2L, 7L, 5L, 2L, 2L, 17L, 9L, 7L, 3L, 13L, 6L, 5L, 2L, 10L,
    14L, 1L, 5L, 19L, 6L), tem_type = structure(c(4L, 3L, 2L,
    1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L,
    2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L,
    3L, 2L, 1L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"
    ), class = "factor"), tem_value = c(0L, 7L, 0L, 23L, 0L,
    29L, 0L, 1L, 0L, 29L, 2L, 0L, 0L, 21L, 9L, 0L, 0L, 5L, 25L,
    0L, 0L, 18L, 13L, 0L, 0L, 21L, 9L, 0L, 0L, 26L, 5L, 0L, 0L,
    24L, 0L, 7L), cnt_vehicle = c(NA, 2754406, NA, NA, NA, 2846039,
    NA, NA, NA, 3149377, NA, NA, NA, 3058810, NA, NA, NA, 3362614,
    NA, NA, NA, 3415716, NA, NA, NA, 3020812, NA, NA, NA, 3076665,
    NA, NA, NA, 2775306, 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, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L
    )), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value",
"tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA,
-36L), class = "data.frame")


Ci-dessous le code de mon graphique :



Code : Tout sélectionner

b_complet_2013[,"pre_type"] <- factor(b_complet_2013[,"pre_type"], levels = c( "NONE","LIGHT","MEDIUM","HEAVY"), labels = c( "NONE","LIGHT","MEDIUM","HEAVY"))
b_complet_2013[,"tem_type"] <- factor(b_complet_2013[,"tem_type"], levels = c( "V_COLD","COLD","MEDIUM","HOT"), labels = c( "V_COLD","COLD","MEDIUM","HOT"))



ggplot(data = b_complet_2013, aes(x = x, y = pre_value*100000, fill = pre_type), stat = "identity") +
  scale_x_continuous(breaks=(1:9)+0.2, labels=unique(b_complet_2013$year_month)) +
  geom_bar(stat = "identity", width=0.3) +
  xlab("date") + ylab ("Number of days of précipitations(left) and temperatures (ritght)") +
  ggtitle("Precipitation per month") + labs(fill = "Frequency") +
  geom_bar(data=b_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
  xlab("date") + ylab("Number of days of precipitations(left) and temperatures (ritght)") +
  ggtitle("Impact of weather on road traffics") + labs(fill = "Frequency") + theme( axis.title.y = element_text(color = "blue", face = "bold")) +
  theme(axis.text.y = element_text(color = "blue", face = "bold", size=9)) + theme( axis.title.y.right = element_text(color = "black", face = "bold")) +
  theme(axis.text.y.right = element_text(color = "black", size = 9, face = "bold"))  +
  geom_line(mapping = aes(x= x+0.2, y = as.numeric(cnt_vehicle)), colour = I("blue"), size = 0.8) +
  geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), colour = I("blue")), show.legend=FALSE, stat = "identity") +
  scale_y_continuous(sec.axis = sec_axis(~./100000,name="Number of days of precipitations(left) and temperatures (ritght)")) +
  theme( plot.title = element_text(size = 17)) + theme(axis.title.x = element_text(size = 12)) + theme(axis.title.y = element_text(size = 12)) +
  labs(y = "Number of vehicles", color ="black") +
  theme(panel.background = element_rect(linetype = "dashed", fill="white"), plot.background = element_rect(linetype = "dashed",fill="grey90" ))

Logez Maxime
Messages : 3138
Enregistré le : 26 Sep 2006, 11:35

Re: Changer l'ordre de la légende - GGPLOT

Messagepar Logez Maxime » 14 Juin 2018, 13:13

Bonjour,

tu peux rajouter la fonction scale_fill_manual en définissant au préalable un vecteur de couleur pour toutes les catégories :

Code : Tout sélectionner

# par exemple :
cols <- structure(c("#C49A00", "#00B6EB", "#00C094", "#A58AFF", "#53B400",
"#00B6EB", "#F8766D", "#FB61D7"), .Names = c("NONE", "LIGHT",
"MEDIUM", "HEAVY", "V_COLD", "COLD", "MEDIUM", "HOT"))

ggplot(data = b_complet_2013, aes(x = x, y = pre_value*100000, fill = pre_type), stat = "identity") +
  scale_x_continuous(breaks=(1:9)+0.2, labels=unique(b_complet_2013$year_month)) +
  geom_bar(stat = "identity", width=0.3) +
  xlab("date") + ylab ("Number of days of précipitations(left) and temperatures (ritght)") +
  ggtitle("Precipitation per month") + labs(fill = "Frequency") +
  geom_bar(data=b_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
  xlab("date") + ylab("Number of days of precipitations(left) and temperatures (ritght)") +
  scale_fill_manual(breaks = names(cols ), values = cols ) + # la ligne a rajouter
  ggtitle("Impact of weather on road traffics") + labs(fill = "Frequency") + theme( axis.title.y = element_text(color = "blue", face = "bold")) +
  theme(axis.text.y = element_text(color = "blue", face = "bold", size=9)) + theme( axis.title.y.right = element_text(color = "black", face = "bold")) +
  theme(axis.text.y.right = element_text(color = "black", size = 9, face = "bold"))  +
  geom_line(mapping = aes(x= x+0.2, y = as.numeric(cnt_vehicle)), colour = I("blue"), size = 0.8) +
  geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), colour = I("blue")), show.legend=FALSE, stat = "identity") +
  scale_y_continuous(sec.axis = sec_axis(~./100000,name="Number of days of precipitations(left) and temperatures (ritght)")) +
  theme( plot.title = element_text(size = 17)) + theme(axis.title.x = element_text(size = 12)) + theme(axis.title.y = element_text(size = 12)) +
  labs(y = "Number of vehicles", color ="black") +
  theme(panel.background = element_rect(linetype = "dashed", fill="white"), plot.background = element_rect(linetype = "dashed",fill="grey90" ))
je te laisse le soin de remplacer les valeurs de cols par les couleurs de ton choix.

Cordialement,
Maxime


Retourner vers « Questions en cours »

Qui est en ligne

Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité