Search code examples
rggplot2geom-bar

how to optimize the graph ggplot with geom_bar


I am a beginner under R, I created a graph that superimposes the temperatures to the precipitations, under ggplot with geom_bar as an option. However, I use the option position = position_nudge (x = 0.4), so that the graphics are not on top of each other. When I use this option, it completely changes the way of calculating.

For example, as you will see below, I would like to have on the barplots on the right dates, a barplot until 31/30. Do you know how to solve this problem? Thank you in advance for your precious help.

Below are my table and my code.

SOUNAME year_month  pre_type    pre_value   tem_type    tem_value   nb_species
WATERFORD (TYCOR)   2014-04 NONE    14  V_COLD  0   NA
WATERFORD (TYCOR)   2014-04 HEAVY   3   COLD    30  8
WATERFORD (TYCOR)   2014-04 LIGHT   7   HOT 0   NA
WATERFORD (TYCOR)   2014-04 MEDIUM  6   MEDIUM  0   NA
WATERFORD (TYCOR)   2014-05 NONE    15  V_COLD  0   NA
WATERFORD (TYCOR)   2014-05 HEAVY   3   COLD    31  17
WATERFORD (TYCOR)   2014-05 LIGHT   10  HOT 0   NA
WATERFORD (TYCOR)   2014-05 MEDIUM  3   MEDIUM  0   NA
WATERFORD (TYCOR)   2014-06 NONE    17  V_COLD  0   NA
WATERFORD (TYCOR)   2014-06 HEAVY   2   COLD    17  NA
WATERFORD (TYCOR)   2014-06 LIGHT   9   HOT 13  NA
WATERFORD (TYCOR)   2014-06 MEDIUM  2   MEDIUM  0   NA

ggplot(data = complet_w, 
       aes(x = complet_w$year_month, 
           y = complet_w$pre_value, 
           fill = complet_w$pre_type, 
           width=0.5), 
       stat = "identity") + 
  geom_bar(stat = "identity") + 
  xlab("date") + 
  ylab ("Number of days of precipitation") + 
  ggtitle("Precipitation per month") + 
  labs(fill = "Frequency") +
  geom_bar(data=complet_w,
           aes(x=complet_w$year_month, 
               y=complet_w$tem_value, 
               fill=complet_w$tem_type, 
               width=0.1), 
           stat = "identity", 
           position = position_nudge(x=0.4)) + 
  xlab("date") + 
  ylab("Number of days of temperature") + 
  ggtitle("Temperature per month") + 
  labs(fill = "Frequency") 

Below is my result. I would like all bars to be 30-31. Is it possible?

result


Solution

  • I remember your previous question. I think you are looking for something like this:

    library(dplyr)
    library(ggplot2)
    library(reshape2)      #To use melt
    View(TEMP_PREC_BIRR)
    View(TEMP_BIRR)
    write.csv(TEMP_PREC_BIRR,"Data1.csv")
    write.csv(TEMP_BIRR,"Data2.csv")
    Data1=melt(TEMP_PREC_BIRR,id.vars="year_month")
    Data2=melt(TEMP_BIRR,id.vars="year_month")
    Data=rbind(Data1,Data2)
    Data=Data[!(Data$variable=="SOUNAME"),]
    View(Data)
    Data=read.csv("Data1.csv")
    View(Data)
    ggplot(Data,aes(x=year_month,y=Data$precipitation_value_,fill=Data$precipitation_type))+
      geom_col()
    
    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(aes(x = as.numeric(year_month)+0.25, 
                   y = TEMP_PREC_BIRR$precipitation_value, 
                   fill = TEMP_PREC_BIRR$precipitation_type),
               stat = "identity",position = position_stack()) + 
      xlab("date") + ylab ("Number of days of precipitation") + 
      ggtitle("Precipitation per month - BIRR") + labs(fill = "Frequency")+
      geom_bar(data=TEMP_BIRR,aes(x=as.numeric(TEMP_BIRR$year_month)-0.25,
                                     y=TEMP_BIRR$temperature_value,
                                     fill=TEMP_BIRR$temperature_type), stat = "identity",position = position_stack()) +
      xlab("date") + ylab("Number of days of temperature") + 
      ggtitle("Temperature per month - BIRR") + labs(fill = "Frequency")+
      theme(panel.background=element_blank())
    

    Output Plot