0
votes

I currently have two dataframes. I wish to get multiple bar plots from both of them in one plot using ggplot. I want to get an average of 'NEE' variable from different years(1850-1950,1951-2012,2013-2100) from both dataframes and plot side by side just like in this green barplot visualization(https://ars.els-cdn.com/content/image/1-s2.0-S0048969716303424-fx1_lrg.jpg). The header of two dataframes is as follows (this is only a portion).The header is the same for both dataframes from year 1850-1859:

How can I achieve plotting bar plots lets say for the year 1850-1852 , 1854-1856, 1857-1859 from both dataframes in one plot. I know the barplots will be the same in this case as both data frames are similar, but i would like to get an idea and I can edit the code to my desired years. (Note that I have 39125 obs with 9 variables)

This is what I have done so far (by following a solution posted by member in this website).I achieved data1 and data2 geom_col successfully.But how can i merge them together and plot geom_col of 1850-1852 , 1854-1856, 1857-1859 side by side from both dataframes?graph of data1 graph of data2 :

data1 %>%
      # case_when lets us define yr_group based on Year:
       mutate(yr_group = case_when(Year <= 1950 ~ "1850-1950",
                          Year <= 2012 ~ "1951-2012",
                          Year <= 2100 ~ "2013-2100",
                          TRUE         ~ "Other range")) %>%
      # For each location and year group, get the mean of all the columns:
  group_by(Lon, Lat, yr_group) %>%
  summarise_all(mean) %>%

  # Plot the mean Total for each yr_group
  ggplot(aes(yr_group, NEE)) + geom_col(position = 
"dodge")+theme_classic()+xlab("Year")+ylab(ln)+labs(subtitle="CCSM4 
RCP2.6")+
geom_hline(yintercept=0, color = "black", size=1)
1
You just want one bar with the average Total as the y? ggplot(df, aes(x = "1850-1860", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge") ??Jon Spring
@Jon Spring Thank you so much! Is it possible to get two geom_bars in one plot. I have two plots of geom_bar, how can I arrange them in one plot(not referring to grid.arrange). (I tried multiple solutions but not working for me)bar1<-ggplot(data1, aes(x = "1850-1860", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge",width=0.3) bar2<-ggplot(data1, aes(x = "2006-2100", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge",width=0.3)user10496589

1 Answers

1
votes

My preferred approach is usually to do the data summarization first and then send the output to ggplot. In this case, you might use dplyr from the tidyverse meta-package to add a variable relating to which time epoch a given year belongs to, and then collect the stats for that whole epoch.

For instance, just using your example data, we might group those years arbitrarily and find the averages for 1850-51, 1852-53, and 1854-55, and then display those next to each other:

library(tidyverse)
df %>%
  # case_when lets us define yr_group based on Year:
  mutate(yr_group = case_when(Year <= 1851 ~ "1850-51",
                           Year <= 1853 ~ "1852-53",
                           Year <= 1855 ~ "1854-55",
                           TRUE         ~ "Other range")) %>%
  # For each location and year group, get the mean of all the columns:
  group_by(Lon, Lat, yr_group) %>%
  summarise_all(mean) %>%

  # Plot the mean Total for each yr_group
  ggplot(aes(yr_group, Total)) + geom_col()

enter image description here

If you have multiple locations, you might use ggplot facets to display those separately, or use dodge within geom_col (equivalent to geom_bar(stat = "identity"), btw) to show the different locations next to each other.