1

I'm a beginner in R, so maybe it sounds simple but I'm looking to stack two bar plot.
For example, for the city BIRR, I have two graphs: one with the precipitations, the other with the temperatures.
What I am trying to do is that for each month 04/2004, I would like to have the barplot stacks temperatures and the barplot stacks precipitation.

I put you my dataset below:

#TEMP_BIR 
"SOUNAME" "year_month" "temperature_type" "temperature_value"    
"1" "BIRR" "2004-04" "VERY_COLD" 0    
"2" "BIRR" "2004-05" "VERY_COLD" 0    
"3" "BIRR" "2004-06" "VERY_COLD" 0    
"4" "BIRR" "2004-07" "VERY_COLD" 0    
"5" "BIRR" "2004-04" "MEDIUM" 28    
"6" "BIRR" "2004-05" "MEDIUM" 29    
"7" "BIRR" "2004-06" "MEDIUM" 19    
"8" "BIRR" "2004-07" "MEDIUM" 25    
"9" "BIRR" "2004-04" "HOT" 0    
"10" "BIRR" "2004-05" "HOT" 2    
"11" "BIRR" "2004-06" "HOT" 11    
"12" "BIRR" "2004-07" "HOT" 6    
"13" "BIRR" "2004-04" "COLD" 2    
"14" "BIRR" "2004-05" "COLD" 0    
"15" "BIRR" "2004-06" "COLD" 0    
"16" "BIRR" "2004-07" "COLD" 0  

# Temp_prec_birr 
"SOUNAME" "year_month" "precipitation_type" "precipitation_value"    
"BIRR" "2004-04" "NONE" "11"    
"BIRR" "2004-05" "NONE" "20"    
"BIRR" "2004-06" "NONE" "11"    
"BIRR" "2004-07" "NONE" "10"    
"BIRR" "2004-04" "HEAVY" "2"    
"BIRR" "2004-05" "HEAVY" "1"    
"BIRR" "2004-06" "HEAVY" "1"    
"BIRR" "2004-07" "HEAVY" "1"    
"BIRR" "2004-04" "LIGHT" "15"    
"BIRR" "2004-05" "LIGHT" "7"    
"BIRR" "2004-06" "LIGHT" "16"    
"BIRR" "2004-07" "LIGHT" "18"    
"BIRR" "2004-04" "MEDIUM" "2"    
"BIRR" "2004-05" "MEDIUM" "3"    
"BIRR" "2004-06" "MEDIUM" "2"    
"BIRR" "2004-07" "MEDIUM" "2"


#I put you my code below:

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")
GGamba
  • 11,599
  • 2
  • 35
  • 38
Elodie P.
  • 73
  • 4
  • Possible duplicate of [Plot two graphs in same plot in R](https://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – symbolrush Jun 18 '19 at 09:32

3 Answers3

1

You can use a library called gridExtra to do that. It has a function grid.arrange() that does just that. Further, you can specify the arrangements by specifying the number of rows or number of column of the plot by passing it to nrow or ncol.

example: If you want to arrange 6 plots into 3X2 then use

grid.arrange(plot1,plot2,plot3,plot4,plot5,plot6,nrow=2)

To answer your problem look at the code and output below:

library(ggplot2)
library(gridExtra)
Data=iris
a=ggplot(Data,aes(x=Data$Sepal.Length,Data$Petal.Length,color=Data$Species))+
  geom_point()+theme(panel.background = element_blank())+
  labs(x="Sepal Length",y="Petal Length",color="Species")
b=ggplot(Data,aes(x=Data$Sepal.Width,Data$Petal.Width,color=Data$Species))+
  geom_point()+theme(panel.background = element_blank())+
  labs(x="Sepal Width",y="Petal Width",color="Species")
grid.arrange(a,b)

The output of the Image is Here

Vedha Viyash
  • 618
  • 1
  • 6
  • 16
  • Thanks for your answer. Yes I had already done that, but the graphics are one below the other, I would like to have one over the other. – Elodie P. May 18 '18 at 09:53
  • @ElodieP., I think you just need to switch the order in your function, so is `grid.arrange(b, a)` instead of `grid.arrange(a,b)` – Ben G Jun 14 '18 at 14:27
0

Use dplyr::bindrows() to combine the two sets, and then use tidyr::gather() to put your temperature and precipitation values in one column where key = precipitation_or_temp and value = value. You can then use one ggplot object with a facet_wrap on the precipitation_or_temp variable to get your two plots.

Check out this link for info on gather: http://r4ds.had.co.nz/tidy-data.html
And check out this link for more info on facets: http://r4ds.had.co.nz/data-visualisation.html

Really do check out the R for Data Science book. It's really easy to follow and will actually help with these answers. Here's how you make the graph with some sample data. Actually arranging the data into the appropriate format is a task I'll leave for you.

library(tidyverse)  
data <- 
  tibble(
    month = as_factor(c("January", "January", "February", "February")),
    value_type = c("temperature", "precipitation", "temperature", "precipitation"),
    value = c(30, 25, 40, 25))

ggplot(data) +
  geom_col(aes(x = month, y = value) +
  facet_wrap(~value-type)
Ben G
  • 2,903
  • 1
  • 13
  • 33
  • Hi Benjamin, Thanks for your answer. Unfortunately I did not understand very well ... Is it possible to show me an example with the data set please? Thank you in advance, Elodie – Elodie P. May 18 '18 at 09:50
  • @ElodieP. I added some sample code for how to use `facet_wrap()` in ggplot, but you'll have to arrange the data in the format I used. Really do read the info in the links I posted, they are invaluable. – Ben G May 23 '18 at 14:11
0

I get what you want to plot, But you need to arrange your data so much using dplyr and/or melt it to get desired output. It's a tedious process.

Although I found the problem in your code. If you are trying to fix your code you have to use only one ggplot() function to plot in the same region. If you create two ggplot() functions your output will be plotted in another plane.

Here's the correct way to use your version of the code with the output:

ggplot(data = TEMP_PREC_BIRR, aes(x = TEMP_PREC_BIRR$year_month, 
                                  y = TEMP_PREC_BIRR$precipitation_value, 
                                  fill = TEMP_PREC_BIRR$precipitation_type,width=0.2)) + 
  geom_bar(stat = "identity",position = position_nudge(x=-0.2)) + 
  xlab("date") + ylab ("Number of days of precipitation") + 
  ggtitle("Precipitation per month - BIRR") + labs(fill = "Frequency")+
  geom_bar(data=TEMP_BIRR,aes(x=TEMP_BIRR$year_month, 
                                 y=TEMP_BIRR$temperature_value, 
                                 fill=TEMP_BIRR$temperature_type), stat = "identity",position = position_nudge(x=0.2)) + 
  xlab("date") + ylab("Number of days of temperature") + 
  ggtitle("Temperature per month - BIRR") + labs(fill = "Frequency")

Output Plot

Hope this helps.

Vedha Viyash
  • 618
  • 1
  • 6
  • 16
  • Thank you very much for your answer !! I'm really happy, thank you again for your help. However, I have a question. How come bars do not go up to 30 - 31? Because it's the number of days per month, so normally the sum per month is 30 or 31. Is it understandable? – Elodie P. May 18 '18 at 16:23