I am trying to plot some aerosol particle data in a heatmap using levelplot. I currently am using a wide dataset shown below:
df <- read.delim("location/data.txt", sep = ",")
rownames(df) <- APSDIAM
colnames(df) <- LocalDateTime
df
2021-03-03 15:19:50 2021-03-03 15:24:50 2021-03-03 15:29:50 2021-03-03 15:34:50
<0.523 "13.5077" "9.68141" "3.87432" "1.56717"
0.542 "9.60621" "8.17904" "3.33433" "1.51037"
0.583 "9.33741" "8.45423" "3.68633" "1.59357"
0.626 "8.35823" "8.60143" "3.74393" "1.54237"
0.673 "7.41105" "8.30703" "3.57113" "1.59997"
0.723 "6.65587" "7.88464" "3.50073" "1.62557"
0.777 "6.16308" "7.43025" "3.09754" "1.37597"
0.835 "4.7935" "6.68147" "2.82234" "1.39517"
0.898 "4.39671" "5.91988" "2.59195" "1.10718"
0.965 "3.55833" "4.8895" "2.43195" "0.972781"
1.037 "2.82234" "4.15992" "1.73437" "0.665587"
1.114 "2.43835" "3.66713" "1.49757" "0.639987"
1.197 "1.91356" "2.93114" "1.07518" "0.454391"
1.286 "1.47837" "2.27835" "1.09438" "0.447991"
1.382 "1.38237" "1.93916" "0.940781" "0.460791"
1.486 "1.26717" "1.63197" "0.710386" "0.339193"
1.596 "0.99838" "1.53597" "0.620788" "0.211196"
1.715 "0.844783" "1.48477" "0.435191" "0.223996"
1.843 "0.735985" "1.05598" "0.49279" "0.191996"
1.981 "0.601588" "0.825583" "0.441591" "0.211196"
2.129 "0.47999" "0.639987" "0.294394" "0.166397"
2.288 "0.390392" "0.563189" "0.223996" "0.121598"
2.458 "0.358393" "0.531189" "0.204796" "0.0319994"
2.642 "0.268795" "0.230395" "0.140797" "0.0447991"
2.839 "0.159997" "0.300794" "0.159997" "0.0191996"
3.051 "0.198396" "0.198396" "0.0767985" "0.0383992"
3.278 "0.140797" "0.147197" "0.0383992" "0.051199"
3.523 "0.0959981" "0.121598" "0.0639987" "0.0191996"
3.786 "0.0703986" "0.0191996" "0.0191996" "0.0191996"
4.068 "0.0319994" "0.0639987" "0.0447991" "0.0127997"
4.371 "0.0447991" "0.0383992" "0.00639987" "0.00639987"
4.698 "0.0127997" "0.0127997" "0.0191996" "0.0191996"
5.048 "0.0319994" "0" "0.0127997" "0.0127997"
5.425 "0.00639987" "0.0127997" "0.00639987" "0"
5.829 "0" "0.00639987" "0" "0"
6.264 "0.00639987" "0" "0" "0"
6.732 "0.00639987" "0" "0" "0"
7.234 "0" "0" "0" "0"
7.774 "0" "0" "0" "0"
8.354 "0" "0" "0" "0"
8.977 "0" "0" "0" "0"
9.647 "0" "0" "0" "0"
10.37 "0" "0" "0" "0"
11.14 "0" "0" "0" "0"
11.97 "0" "0" "0" "0"
12.86 "0" "0" "0" "0"
13.82 "0" "0" "0" "0"
14.86 "0" "0" "0" "0"
15.96 "0" "0" "0" "0"
17.15 "0" "0" "0" "0"
18.43 "0" "0" "0" "0"
19.81 "0" "0" "0" "0"
2021-03-03 15:39:50 2021-03-03 16:09:50 2021-03-03 16:14:50 2021-03-03 16:19:50
<0.523 "0.810384" "0.352793" "0.332793" "0.347993"
0.542 "0.780784" "0.313594" "0.275194" "0.351993"
0.583 "0.780784" "0.217596" "0.319994" "0.281594"
0.626 "0.812784" "0.249595" "0.217596" "0.262395"
0.673 "0.844783" "0.211196" "0.262395" "0.236795"
0.723 "1.03038" "0.115198" "0.185596" "0.217596"
0.777 "0.857583" "0.179196" "0.191996" "0.140797"
0.835 "0.870383" "0.172797" "0.159997" "0.140797"
0.898 "0.684786" "0.0639987" "0.102398" "0.0703986"
0.965 "0.563189" "0.102398" "0.0959981" "0.0767985"
1.037 "0.435191" "0.0831983" "0.108798" "0.0767985"
1.114 "0.396792" "0.0703986" "0.051199" "0.0255995"
1.197 "0.319994" "0.051199" "0.0319994" "0.0383992"
1.286 "0.319994" "0.0191996" "0.0191996" "0.0191996"
1.382 "0.249595" "0.00639987" "0.00639987" "0.0255995"
1.486 "0.198396" "0.0255995" "0.0127997" "0.0191996"
1.596 "0.159997" "0.0255995" "0.00639987" "0.0255995"
1.715 "0.121598" "0.0127997" "0.0127997" "0.00639987"
1.843 "0.0959981" "0.0127997" "0.0255995" "0"
1.981 "0.115198" "0.0127997" "0" "0.0127997"
2.129 "0.051199" "0.0191996" "0.0319994" "0"
2.288 "0.0575988" "0.00639987" "0.0127997" "0"
2.458 "0.0127997" "0" "0.0127997" "0"
2.642 "0.0383992" "0.00639987" "0.0127997" "0"
2.839 "0.0639987" "0" "0.00639987" "0"
3.051 "0.0191996" "0.00639987" "0" "0.00639987"
3.278 "0.0191996" "0" "0" "0"
3.523 "0.0127997" "0.00639987" "0" "0"
3.786 "0.0191996" "0.00639987" "0" "0"
4.068 "0.00639987" "0" "0" "0"
4.371 "0" "0" "0" "0"
4.698 "0" "0" "0" "0"
5.048 "0" "0" "0" "0"
5.425 "0" "0" "0" "0"
5.829 "0" "0" "0" "0"
6.264 "0" "0" "0" "0"
6.732 "0" "0" "0" "0"
7.234 "0" "0" "0" "0"
7.774 "0" "0" "0" "0"
8.354 "0" "0" "0" "0"
8.977 "0" "0" "0" "0"
9.647 "0" "0" "0" "0"
10.37 "0" "0" "0" "0"
11.14 "0" "0" "0" "0"
11.97 "0" "0" "0" "0"
12.86 "0" "0" "0" "0"
13.82 "0" "0" "0" "0"
14.86 "0" "0" "0" "0"
15.96 "0" "0" "0" "0"
17.15 "0" "0" "0" "0"
18.43 "0" "0" "0" "0"
19.81 "0" "0" "0" "0"
2021-03-03 16:24:50 2021-03-03 16:29:50 2021-03-03 16:59:50 2021-03-03 17:04:50
<0.523 "0.51199" "0.587188" "0.198396" "0.0743985"
0.542 "0.49919" "0.51839" "0.179196" "0.0895982"
0.583 "0.454391" "0.50559" "0.211196" "0.108798"
0.626 "0.287994" "0.390392" "0.300794" "0.0767985"
0.673 "0.294394" "0.383992" "0.172797" "0.127997"
0.723 "0.230395" "0.364793" "0.204796" "0.102398"
0.777 "0.179196" "0.249595" "0.191996" "0.140797"
0.835 "0.140797" "0.185596" "0.198396" "0.134397"
0.898 "0.108798" "0.140797" "0.134397" "0.102398"
0.965 "0.0575988" "0.108798" "0.179196" "0.0767985"
1.037 "0.0831983" "0.0959981" "0.140797" "0.0639987"
1.114 "0.051199" "0.0575988" "0.0959981" "0.0447991"
1.197 "0.0319994" "0.0639987" "0.0767985" "0.051199"
1.286 "0.0255995" "0.0319994" "0.0447991" "0.0703986"
1.382 "0.051199" "0.0319994" "0.0703986" "0.0319994"
1.486 "0" "0.0127997" "0.0319994" "0.0447991"
1.596 "0.0191996" "0.0127997" "0.0127997" "0.0383992"
1.715 "0.0255995" "0.00639987" "0.0383992" "0.0319994"
1.843 "0.0191996" "0.00639987" "0.051199" "0"
1.981 "0.0127997" "0.0191996" "0.0127997" "0.0191996"
2.129 "0" "0.0191996" "0.0127997" "0"
2.288 "0.00639987" "0" "0.00639987" "0.00639987"
2.458 "0.00639987" "0" "0.0255995" "0.0127997"
2.642 "0.0127997" "0.00639987" "0" "0"
2.839 "0" "0" "0" "0"
3.051 "0" "0" "0.0127997" "0"
3.278 "0" "0" "0" "0"
3.523 "0" "0" "0" "0"
3.786 "0" "0" "0" "0"
4.068 "0" "0" "0" "0"
4.371 "0" "0" "0" "0"
4.698 "0" "0" "0" "0"
5.048 "0" "0" "0" "0"
5.425 "0" "0" "0" "0"
5.829 "0" "0" "0" "0"
6.264 "0" "0" "0" "0"
6.732 "0" "0" "0" "0"
7.234 "0" "0" "0" "0"
7.774 "0" "0" "0" "0"
8.354 "0" "0" "0" "0"
8.977 "0" "0" "0" "0"
9.647 "0" "0" "0" "0"
10.37 "0" "0" "0" "0"
11.14 "0" "0" "0" "0"
11.97 "0" "0" "0" "0"
12.86 "0" "0" "0" "0"
13.82 "0" "0" "0" "0"
14.86 "0" "0" "0" "0"
15.96 "0" "0" "0" "0"
17.15 "0" "0" "0" "0"
18.43 "0" "0" "0" "0"
19.81 "0" "0" "0" "0"
2021-03-03 17:09:50 2021-03-03 17:14:50 2021-03-03 17:19:50
<0.523 "0.050399" "0.050399" "0.0543989"
0.542 "0.051199" "0.0639987" "0.0447991"
0.583 "0.0959981" "0.0831983" "0.0703986"
0.626 "0.102398" "0.0831983" "0.0639987"
0.673 "0.0639987" "0.0767985" "0.108798"
0.723 "0.115198" "0.0831983" "0.127997"
0.777 "0.0959981" "0.0831983" "0.108798"
0.835 "0.108798" "0.0895982" "0.0639987"
0.898 "0.0639987" "0.0575988" "0.0703986"
0.965 "0.0255995" "0.0319994" "0.0767985"
1.037 "0.0447991" "0.0127997" "0.0383992"
1.114 "0.0319994" "0.0383992" "0.0191996"
1.197 "0.0447991" "0.0383992" "0.0639987"
1.286 "0.0127997" "0.0127997" "0.00639987"
1.382 "0.0127997" "0.0127997" "0.0383992"
1.486 "0.0127997" "0.0127997" "0.0191996"
1.596 "0.00639987" "0.0191996" "0"
1.715 "0.0127997" "0.0127997" "0.0191996"
1.843 "0.0191996" "0.00639987" "0.00639987"
1.981 "0.0191996" "0.0191996" "0.0191996"
2.129 "0.0127997" "0" "0.0191996"
2.288 "0.00639987" "0.0191996" "0.00639987"
2.458 "0.00639987" "0" "0.0127997"
2.642 "0.00639987" "0.0127997" "0"
2.839 "0" "0" "0"
3.051 "0" "0" "0.00639987"
3.278 "0" "0" "0.00639987"
3.523 "0.00639987" "0" "0"
3.786 "0" "0" "0"
4.068 "0" "0" "0"
4.371 "0" "0" "0"
4.698 "0" "0" "0"
5.048 "0" "0" "0"
5.425 "0" "0" "0"
5.829 "0" "0" "0"
6.264 "0" "0" "0"
6.732 "0" "0" "0"
7.234 "0" "0" "0"
7.774 "0" "0" "0"
8.354 "0" "0" "0"
8.977 "0" "0" "0"
9.647 "0" "0" "0"
10.37 "0" "0" "0"
11.14 "0" "0" "0"
11.97 "0" "0" "0"
12.86 "0" "0" "0"
13.82 "0" "0" "0"
14.86 "0" "0" "0"
15.96 "0" "0" "0"
17.15 "0" "0" "0"
18.43 "0" "0" "0"
19.81 "0" "0" "0"
I then tried to use the code I found on the website https://www.r-graph-gallery.com/27-levelplot-with-lattice.html that suggested that with wide data I input it as a matrix without the usual 'z ~ x*y' part.
dfmat <- as.matrix(df)
levelplot(t(dfmat), col.regions = magma(100))
I am unsure how to modify the figure. I would like to change the y axis, with the size scale (<0.523 to 19.81, the rows in the original df data) set to be a log scale without all of the individual values ticks listed. Similarly, I would like to be able to clean up the x axis with the LocalDateTime to have fewer ticks that are reasonably space. The code also has the figure being really narrow and I believe this is because it is trying to make perfect squares for the data, despite the fact that there are more columns than rows.
I can plot the data as I desire it using Igor, shown below, but would like to be able to replicate this in R.
I have attempted to input the data in a 'z~x*y' format, but I have not been successful and I have failed to get the data to properly plot and have instead gotten this as a result.
Any assistance or suggestions would be appreciated! If you have further questions or need anymore information, I will try to supply it as best as I can.
I've not used lattice in forever; here's the ggplot2 implementation.
library(dplyr)
library(tidyr)
library(tibble)
library(ggplot2)
library(viridisLite)
# tidy format needed
df1 <- df %>%
rownames_to_column(var = "Aerodynamic Diameter (um)") %>%
pivot_longer(cols = -one_of("Aerodynamic Diameter (um)"),
names_to = "Timestamp",
values_to = "Value") %>%
mutate(
`Aerodynamic Diameter (um)` = as.numeric(`Aerodynamic Diameter (um)`),
Timestamp = as.POSIXct(Timestamp, format = "%Y-%m-%d %H:%M:%S"))
# Igor padded values, so manually set tile width
ggplot(data = df1,
mapping = aes(
x = Timestamp,
y = `Aerodynamic Diameter (um)`,
fill = Value)) +
geom_tile(height = 0.1, width = 2000) +
scale_fill_gradientn("",
colours = magma(50, begin = 0.2),
breaks = 0:7 * 2, limits = c(0, 14)) +
scale_y_log10(breaks = c(5:10/10, 2:10, 20),
expand = c(0, 0)) +
scale_x_datetime(expand = c(0, 0)) +
xlab(as.Date(df1$Timestamp[1])) +
ylab("Aerodynamic Diameter (\u03BCm)") +
theme(legend.key.height = unit(1.4, 'cm'))