I am starting over using R and ggplot to visualize time series data of environmental variables. So far I love the oppurtnities of ggplot2 to visualize the data, easily choosing different periods and variables to plot and define aesthetics. But now I have encountered the first problem that I wasn´t really able to google:
- My goal is to plot several variables from different dataframes with individual aesthetics(fixed period, same y-Axis, different colors etc.) into one Plot
I have 8 dataframes ("TreeA
" - "TreeH
") structured like following, where TreeA
is the Name of the data frame, "Time" is the time of measurement, formatted in POSIXct
format, and Tleaf
, Tair
and Tdiff
are three of 16 variables:
TreeA
Zeit Tleaf Tair Tdiff ........
1: 2018-05-18 00:00:00 12.997 13.20000 -0.203
2: 2018-05-18 00:10:00 13.082 13.20000 -0.119
3: 2018-05-18 00:20:00 11.909 12.06700 -0.158
4: 2018-05-18 00:30:00 11.315 11.53300 -0.219
5: 2018-05-18 00:40:00 11.251 11.46700 -0.216
I have melt
ed the DFs to long format resulting
TreeA_long
Time variable value
1: 2018-05-18 00:00:00 Tleaf 12.997000000
2: 2018-05-18 00:10:00 Tleaf 13.082000000
3: 2018-05-18 00:20:00 Tair 11.909
4: 2018-05-18 00:30:00 Tair 11.315
5: 2018-05-18 00:40:00 Tdiff 1.251
From this I have been successfully plotting Graphs with this ggplot functionalities:
ggplot(subset(TreeA_long, variable %in% c("Tleaf","Tair","Tdiff")),
aes(x=Time,
y=value, color=variable)) +
geom_line() +
scale_x_datetime(limits=start.endKW21, labels = date_format("%d") , breaks = date_breaks("24 hours")) +
scale_y_continuous(limits = c(5,55),breaks = seq(10,55, by = 2)) +
labs(title="Mai/Juni Cbet1", x="Day", y="Temperature") +
theme(legend.position='right') +
scale_color_manual(values = c("Tleaf" = "green", "Tair" = "blue", "Tdiff" = "yellow"))
I have tried to add a second geom_line(data=TreeB_long)
for plotting variables from the second Dataframe in the same plot. It has worked to plot all the variables from TreeB
but of course I need to compare same variables and also I want to specify aesthetics (color of the lines, dashing lines etc. for each variable.
So my question is:
- How can I compare
TreeA
toTreeB
in one Plot? - Also I would be open to merge the different Dataframes, but it is not working to connect in long format with the same variable names
I hope that my questions are clear enough, and you can help me somehow. I believe that there is an easy solution to my problem, but as I said googling didn´t yield good results so far.
Thank you and have a good day! Konrad