Search code examples
rggplot2geom-col

How to draw a column graph after adding all positive and negative values for a specific 'year' in geom_col?


I have a data frame contain some information for a period of (1948-2013). Every year also contains data in terms of 4 season. Now sometimes, the year contains positive and negative data. Sample of my data

   YEAR Region               temp_avg_max temp_avg_min temp_dif_avg_max whole_avg_min_temp
     <int> <chr>                       <dbl>        <dbl>            <dbl>              <dbl>
     1  1948 Central egion                33.1         20.6          -0.400              -0.516
     2  1948 Eastern Region               32.7         19.1          -0.775              -2.08 
     3  1948 Northern Region              33.5         20.2           0.0124             -0.970
     4  1948 Northsouthern Region         33.5         21.3           0.0443              0.154
     5  1948 Northwestern Region          31.6         20.9          -1.88               -0.232
     6  1948 Southeastern Region          32.5         21.8          -0.958               0.676
     7  1949 Central egion                33.1         20.6          -0.400              -0.516
     8  1949 Eastern Region               32.7         19.1          -0.775              -2.08 
     9  1949 Northern Region              33.5         20.2           0.0124             -0.970
     10 1949 Northsouthern Region         33.5         21.3           0.0443              0.154
     11 1949 Northwestern Region          31.6         20.9          -1.88               -0.232
     12 1949 Southeastern Region          32.5         21.8          -0.958               0.676

Now I want to draw a column graph and I also did it. My code is given below

plot_diff_max_temp_vs_year_region_col <- ggplot(data=diff_max_temp[diff_max_temp$YEAR %in% c(1948, 1960, 1972, 1984, 1996, 2008, 2013),], 
                                       aes(x=Region, y=temp_dif_avg_max, fill=factor(YEAR)))+
 geom_col(position="identity", width = 0.5)+
 geom_text(aes(label=sprintf("%0.1f", round(year_wise_region_max_temp, digits = 2))), 
 position=position_dodge(width=0.9), vjust=-0.50)+
 theme_minimal()+
 xlab("Region")+
 ylab("Temperature")+
 ggtitle("Max Temp with 12 years moving ") 

You can take a look at my graph in hear online plot link

However, I want to draw them in this way (all negative and positive values will be added) for a specific Region and then will draw the column.

Any guidelines are appreciated


Solution

  • In ggplot2 2.2.0 negative stacking is possible.

    Just remove the geom_text and changing something minor. Find the code

    plot_diff_max_temp_vs_year_region_col <- 
    ggplot(data=diff_max_temp[diff_max_temp$YEAR %in% c(1948, 1960, 1972, 1984, 1996, 2008, 2013),]
                                           )+
    geom_col(aes(x=Region, y=temp_dif_avg_max, fill=factor(YEAR)))+
     theme_minimal()+
     xlab("Region")+
     ylab("Temperature")+
     ggtitle("Max Temp with 12 years moving ") 
    ggplotly(plot_diff_max_temp_vs_year_region_col)
    

    I am not sure but you can take a look at this link Link