I have a data frame T_mod
with 150 observations and 2920 variables, containing subsurface temperature values in °C over one year. It looks like this:
> T_mod[1:10, 1:6]
t=-24548400 t=-24537600 t=-24526800 t=-24516000 t=-24505200 t=-24494400
z=0.1 9.000187 9.004622 9.009004 9.013332 9.017607 9.021829
z=0.2 8.587763 8.592795 8.597776 8.602705 8.607583 8.612410
z=0.3 8.179728 8.185313 8.190848 8.196334 8.201770 8.207157
z=0.4 7.776561 7.782655 7.788702 7.794702 7.800653 7.806558
z=0.5 7.378704 7.385267 7.391785 7.398256 7.404682 7.411062
z=0.6 6.986564 6.993556 7.000504 7.007408 7.014268 7.021084
z=0.7 6.600512 6.607894 6.615235 6.622533 6.629789 6.637003
z=0.8 6.220886 6.228623 6.236319 6.243975 6.251591 6.259166
z=0.9 5.847995 5.856050 5.864068 5.872046 5.879986 5.887887
z=1 5.482113 5.490454 5.498759 5.507026 5.515257 5.523450
The rownames stand for depth. In 10 cm increments from 0.1 m to 15 m underground. Colnames indicate time in elapsed seconds. The cell values are temperatures in °C, for each point in time for a given depth.
I want to create a heatmap showing temperatures along time on the x-axis and depth on the y-axis. The plot below is created with the image.plot function in R base graphics using the following code:
image.plot(z = t(as.matrix(T_mod[150:1,])), legend.lab = "Temperature (°C)",
ylab = "Depth (m)", xlab = "Time")
The x axis represents time (one year in 3h intervals) and the y axis represents depth (0 to 15 m in 10 cm increments). Z values are temperatures for a given point in time and a specific depth. Obvisously, the axes ticks and tick labels make little sense as of now. The problem is the image
and image.plot
functions are somewhat rigid, not allowing to adjust axis ticks, labels, etc.
Now, someone has pointed me towards ggplot2 for greater flexibility in adjusting plot parameters but I have not used ggplot so far. Consequently, the code below does not work.
ggplot(T_mod, aes(x=time, y=Depth, z=Temperature)) +
geom_tile(aes(fill=Temperature)) +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2))+
ylab("Depth")+
xlab("Time")+
# possibly use stat_contour(binwidth = 0.1,aes(colour = ..level..),size=0.1) +
# ... and scale_fill_gradient(low = "red", high = "Green”) +
# ... and scale_colour_gradient(low = "black", high = "black",guide = "none")+
scale_y_continuous(expand = c(0,0),breaks=seq(20, 140, 20),limits=c(20,140),labels=lbl_y)+
scale_x_continuous(expand = c(0,0),breaks=seq(124, 2796, 240),limits=c(124,2796),labels=lbl_x)+
coord_cartesian(ylim=c(1,150),xlim=c(1,2920))+
theme(axis.text.x = element_text(size = 15),axis.text.y = element_text(size = 15),axis.title.x = element_text(size = 15),axis.title.y = element_text(size = 15),plot.title = element_text(size=15))+
ggtitle("Main title")
> lbl_y
[1] -2 -4 -6 -8 -10 -12 -14
> lbl_x
[1] "01 Sep" "01 Okt" "01 Nov" "01 Dez" "01 Jan" "01 Feb" "01 Mrz" "01 Apr" "01 Mai"
[10] "01 Jun" "01 Jul" "01 Aug"
The basic issue I believe is that I do not know how to assign depth, time, and temperature from the data frame to the aes()
call in the first row. Other examples use columns to specify that but the columns in my data frame indicate temperatures at one point in time and as infill I want all temperatures plotted. Any sugggestions on how to plot this with ggplot2 or how to make changes to the image.plot function above that allow axes to be set are greatly appreciated.
gather
your data so that temperature is not stored in multiple columns.ggplot
is designed to work with tidy data. – Calum You