0

I currently have two dataframes: dataframe5 and dataframe 6.

I have created a temporal plot for both of the dataframes in a single plot( Total vs Year) I require to find the mean of these two lines and plot it in the same graph.temporalplot

My attempts have not been working so far

library(ggplot2)
df <- merge(dataframe5, dataframe6, by = 'Total')

ggplot(df) +
  geom_line(aes(Year.x, Total), color = '#0087E9', size = 5) +
  theme_minimal() +
  theme(axis.text = element_text(color = 'black', size = 16),
        axis.line = element_line(color = 'black'))

Note: Both of the dataframes consist of 88 observations

  • Can you provide the output of `dput(dataframe5)` and `dput(dataframe6)`? Or simply `dput(head(dataframe5))` for the two if the other is too much. – Anders Ellern Bilgrau Feb 25 '19 at 19:24
  • 1
    If it's really just 2 data frames, same rows, same columns, same order, and all your data is numeric, then by far the quickest solution is to use `df_mean = (dataframe5 + dataframe6) / 2`. – Gregor Thomas Feb 25 '19 at 19:38

2 Answers2

1

Here's a general answer that will scale up to as many data frames as you have:

library(dplyr)
df_list = list(df5 = df5, df6 = df6)
library(dplyr)
big_df = bind_rows(df_list, .id = "source")
big_df = big_df %>% group_by(Year) %>% summarize_if(is.numeric, mean) %>%
  mutate(source = "Mean") %>%
  bind_rows(big_df)

ggplot(big_df, aes(x = Year, y = Total, color = source)) +
  geom_line()

enter image description here

Naming the list more appropriately will help with the plot labels. If you do have more data frames, I'd strongly recommend reading my answer at How to make a list of data frames.

Using this data:

df5 = read.table(text = "Year     VegC    LittC   SoilfC   SoilsC    Total
1 2013 1.820858 1.704079 4.544182 1.964507 10.03363
2 2014 1.813573 1.722106 4.548287 1.964658 10.04863
3 2015 1.776853 1.722110 4.553425 1.964817 10.01722
4 2016 1.794462 1.691728 4.556691 1.964973 10.00785
5 2017 1.808207 1.708956 4.557116 1.965063 10.03936
6 2018 1.831758 1.728973 4.559844 1.965192 10.08578",
header = T)

df6 = read.table(text =
" Year     VegC    LittC   SoilfC   SoilsC    Total
1 2013 1.832084 1.736137 4.542052 1.964454 10.07474
2 2014 1.806351 1.741353 4.548349 1.964633 10.06069
3 2015 1.825316 1.729084 4.552433 1.964792 10.07164
4 2016 1.845673 1.735861 4.553766 1.964900 10.10020
5 2017 1.810343 1.754477 4.556542 1.965033 10.08640
6 2018 1.814503 1.728337 4.561960 1.965191 10.07001",
header = T)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

If I understand you right, you would like to plot for the year 2013 10,054185

If you have for every year one line you can create a new col and add this to your existing ggplot:

df <- dataframe5$Year
df$total5 <- dataframe5$Total
df$total6 <- dataframe6$Total
df$totalmean <- (df$total5+df$total6)/2

By plotting df$totalmean you should get the mean of the line. Just add the lines by + Geometrie_line(...)in the existing ggplot.

Toby Kal
  • 27
  • 7