Superposer deux barplot empilés avec 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

Superposer deux barplot empilés avec ggplot

Messagepar Elodie Perron » 18 Mai 2018, 07:49

Je suis debutante en R, donc peut-être que c'est une question simple, mais j'aimerais associer deux barplots empiles sur un même et seul graphique.

Par exemple, pour la ville BIRR, j'ai deux graphiques: un avec les precipitations et l'autre avec les temperatures.
Pour chaque mois, j'aimerai avoir un barplot des temperatures, à coté de celui des precipitations.


Ci dessous mon jeu de données:

Code : Tout sélectionner

TEMP_BIR <- structure(list(SOUNAME = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("BALLYSHANNON (CATHLEENS FALL)",
"BIRR", "MALIN HEAD", "VALENTIA OBSERVATORY", "WATERFORD (TYCOR)"
), class = "factor"), year_month = structure(c(1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("2004-04",
"2004-05", "2004-06", "2004-07"), class = "factor"), temperature_type = structure(c(4L,
4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("COLD",
"HOT", "MEDIUM", "VERY_COLD"), class = "factor"), temperature_value = c(0,
0, 0, 0, 28, 29, 19, 25, 0, 2, 11, 6, 2, 0, 0, 0)), .Names = c("SOUNAME",
"year_month", "temperature_type", "temperature_value"), row.names = c(NA,
16L), class = "data.frame")



Code : Tout sélectionner

Temp_prec_birr <- structure(list(SOUNAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "BIRR", class = "factor"),
    year_month = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("2004-04", "2004-05",
    "2004-06", "2004-07"), class = "factor"), precipitation_type = structure(c(4L,
    4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("HEAVY", "LIGHT", "MEDIUM", "NONE"), class = "factor"),
    precipitation_value = c(11L, 20L, 11L, 10L, 2L, 1L, 1L, 1L,
    15L, 7L, 16L, 18L, 2L, 3L, 2L, 2L)), .Names = c("SOUNAME",
"year_month", "precipitation_type", "precipitation_value"), class = "data.frame", row.names = c(NA,
-16L))




#Ci-dessous mon code pour les graphiques:

Code : Tout sélectionner

ggplot(data = TEMP_PREC_BIRR, aes(x = TEMP_PREC_BIRR$year_month,
                                  y = TEMP_PREC_BIRR$precipitation_value,
                                  fill = TEMP_PREC_BIRR$precipitation_type)) +
  geom_bar(aes (width = .2), stat = "identity") +
  xlab("date") + ylab ("Number of days of precipitation") +
  ggtitle("Precipitation per month - BIRR") + labs(fill = "Frequency")

ggplot(data = TEMP_BIRR, aes(x = TEMP_BIRR$year_month,
                             y = TEMP_BIRR$temperature_value,
                             fill = TEMP_BIRR$temperature_type)) +
  geom_bar(aes(width = .2), stat = "identity") +
    xlab("date") + ylab("Number of days of temperature") +
  ggtitle("Temperature per month - BIRR") + labs(fill = "Frequency")

François Bonnot
Messages : 537
Enregistré le : 10 Nov 2004, 15:19
Contact :

Re: Superposer deux barplot empilés avec ggplot

Messagepar François Bonnot » 18 Mai 2018, 08:36

Bonjour,
Pour insérer votre jeu de données, utiliser dput() :
http://forums.cirad.fr/logiciel-R/viewtopic.php?f=1&t=3302
François


Retourner vers « Questions en cours »

Qui est en ligne

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

cron