I'm trying to create a heatmap with columns of test data and rows of individual study participants. The participants can be classified into three distinct groups. I'd like to annotate the plot with the three groups and then cluster the data within each group to understand the differences between them.
I'm new to creating heatmaps, and I can't get the row annotations to work. I'm also not sure how to cluster only within each group once I do get the annotations working. I was thinking that the package "pheatmap.type" would work, but unfortunately, it's not available for R version 4.0.2.
I can't post exact data (confidential) but I've attached and example file and I'll describe what I've done so far and post the code. I have a data frame with the first column as labels that include the participant ID and the group (did this using row.names=1) and then 12 columns with numeric data (no NA's). I then ordered the data by the row names and used the scale function to scale the data and generate a matrix. I then tried to create an annotation row by adding the group info to a data frame in several different ways. What I've tried so far is below:
#dataframe with Group and ID as row names and 12 numerical columns
df_1_HM <- data.frame(df_1$Group_ID, df_1$Test1, df_1$Test2, df_1$Test3, df_1$Test4, df_1$Test5, df_1$Test6, df_1$Test7, df_1$Test8, df_1$Test9, df_1$Test10, df_1$Test11, df_1$Test12, row.names=1)
#ordering the dataframe so that the groups are in order
df_1_HM_ordered <- df_1_HM[ order(row.names(df_1_HM)), ]
#Z-scoring (scaling) data
df_HM_matrix_1 <- scale(df_1_HM)
#creating a color palette
my_palette <- colorRampPalette(c("white", "grey", "black"))(n = 100)
#Plotting heatmap
install.packages("gplots")
library(gplots)
#trying to plot the heatmap with annotation_row data
#The method below does not work for me. The plot will run with no errors but does not actually plot - it ends up becoming a list of 4 with no data.
pheatmap(df_HM_matrix_1,
scale="none",
color=my_palette,
fontsize=14,
annotation_row=annotation_row)
annotation_row = data.frame(
df_Group = factor(rep(c("Group 1", "Group 2", "Group 3"), c(11, 10, 7)))
)
rownames(annotation_row) = paste("df_Group", 1:28, sep = "")
rownames(annotation_row) = rownames(df_HM_matrix_1) # name matching
#I also tried to use a dataframe with just the groups as column 1 to get row annotation
pheatmap(df_HM_matrix_1,
scale="none",
color=my_palette,
fontsize=14,
annotation_row=df_Group)
df_Group <- data.frame(df_1$Group, df_1$ID)
#Also tried using the select function to create a dataframe for the row annotation
df_Group_1 <- select(df_1, Group)
#When I use either of the data frame methods above I get the following error: Error in cut.default(a, breaks = 100) : 'x' must be numeric
Any help with this at all would be awesome!!
Here is the example data:
structure(list(Group_ID = structure(1:28, .Label = c("Group1_10",
"Group1_13", "Group1_15", "Group1_2", "Group1_20", "Group1_26",
"Group1_27", "Group1_3", "Group1_6", "Group1_8", "Group2_1",
"Group2_12", "Group2_14", "Group2_16", "Group2_21", "Group2_23",
"Group2_25", "Group2_28", "Group2_7", "Group2_9", "Group3_11",
"Group3_17", "Group3_18", "Group3_19", "Group3_24", "Group3_4",
"Group3_5", "Group3_6"), class = "factor"), Test1 = c(1.44, 4.36,
0.75, 0.59, 1.67, 0.41, 2.42, 0.57, 0.89, 0.45, 0.31, 1.56, 2.13,
0.86, 0.12, 0.26, 1.47, 2.64, 3.92, 2.19, 0.43, 0.98, 1.93, 1.49,
1.43, 2.58, 2.49, 2.64), Test2 = c(1.44, 4.36, 0.75, 0.59, 1.67,
0.41, 2.42, 0.57, 0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26,
1.47, 2.64, 3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49,
2.64), Test3 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64, 3.92,
2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 2.64), Test4 = c(1.44,
4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57, 0.89, 0.45, 0.31, 1.56,
2.13, 0.86, 0.12, 0.26, 1.47, 2.64, 3.92, 2.19, 0.43, 0.98, 1.93,
1.49, 1.43, 2.58, 2.49, 0.31), Test5 = c(1.44, 4.36, 0.75, 0.59,
1.67, 0.41, 2.42, 0.57, 0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12,
0.26, 1.47, 2.64, 3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58,
2.49, 0.31), Test6 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42,
0.57, 0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 0.31),
Test7 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 1.49
), Test8 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 1.49
), Test9 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 1.49
), Test10 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 3.92
), Test11 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 3.92
), Test12 = c(1.44, 4.36, 0.75, 0.59, 1.67, 0.41, 2.42, 0.57,
0.89, 0.45, 0.31, 1.56, 2.13, 0.86, 0.12, 0.26, 1.47, 2.64,
3.92, 2.19, 0.43, 0.98, 1.93, 1.49, 1.43, 2.58, 2.49, 3.92
)), class = "data.frame", row.names = c(NA, -28L))
For annotations to work with pheatmap, factors must be ordered. To do this, add ordered = TRUE to factor():
annotation_row = data.frame(df_Group = factor(rep(c("Group 1", "Group 2", "Group 3"), c(11, 10, 7)), ordered = TRUE))
You could also use as.ordered() to accomplish the same thing.
To sort your heatmap row by annotation group, just add the argument cluster_rows = F to pheatmap():
pheatmap(df_HM_matrix_1,
scale="none",
color=my_palette,
fontsize=14,
annotation_row=annotation_row,
cluster_rows = F)
And here is what it looks like now:
Related
I have performed Spearman correlation for my data. Then I tried to cluster and plot my data using the "ward.D2" method for corrplot()and pheatmap(). However, the order of the variables is different between the two plots.
Could someone help me clarify this point, thus correcting my code and creating the two plots with the same order of clustered variables?
Thank you so much.
#A sample of my dataset:
dput(Data_corr)
structure(list(S_cHDLP = c(0.299999999999999, -2.78, 0.880000000000001,
2.48, 2.15, 5.31, 3.02, 1.19, 2.1, -1.18, -0.34, 1.25, -3.25,
-3.16, 0.19, -0.100000000000001, -2.16, -0.220000000000001, 0.77,
-2.12), H7P = c(-0.18, -0.48, -0.13, -0.21, 0.07, 0.64, -0.13,
-0.1, 0.12, -0.22, 0.09, -0.0399999999999999, -1.56, 0.39, 0.58,
-0.49, 0.2, 0.13, 0.11, 0.06), H6P = c(0, 0, 0, 0.16, -0.23,
0, 0, 0, -0.26, -0.28, 0.06, -0.17, 1.16, -0.12, -0.32, -0.17,
0.38, 0.05, 0.01, 0), H5P = c(0, 0.84, 0.47, 1.21, 0.01, 0.21,
1.36, 0.2, -0.12, 0.93, -1.01, 0, -0.58, -0.97, -1, 0.97, -0.89,
0.35, -0.59, -0.12), H4P = c(-0.12, -1.27, -0.18, 0.25, 1.02,
1.26, -0.62, -0.16, 0.25, -0.01, 0.44, 0.17, 0.19, 0.97, 2.35,
0.3, -0.18, 0.03, 0.0899999999999999, 0.38), H3P = c(-0.31, 0.39,
0.13, 0.29, 0, 0.02, -0.07, 0, 0, -0.32, 0, -0.79, 0, -0.53,
-0.71, -0.2, 2.08, 0.86, 0, 0), H2P = c(-1.28, -0.619999999999999,
-1.07, 1.96, 0.15, 4.92, 1.55, 3, -0.459999999999999, -0.56,
1.12, 3.44, -1.48, -1.27, 1.45, 0.609999999999999, -1.59, -1.57,
2.04, 2.03), H1P = c(1.58, -2.15, 1.96, 0.51, 2, 0.37, 1.47,
-1.83, 2.56, -0.62, -1.46, -2.19, -1.77, -1.9, -1.25, -0.73,
-0.57, 1.35, -1.28, -4.14), TRLZ_TRL = c(4.61, 1.49, -2.71, 1.54,
-5.46, 2.18, 3.48, 12.83, 7.51, 7.74, -8.38, -0.729999999999997,
6.11, -19.74, -0.869999999999997, -1.82, -1.57000000000001, 0.609999999999999,
-14.79, -18.65), LDLZ = c(-0.0599999999999987, -0.400000000000002,
-0.289999999999999, -1.2, -0.479999999999997, -0.59, -1.29, -0.0599999999999987,
0.210000000000001, -1.58, 1.97, 0.0800000000000018, -1, 1.95,
1.41, 0.00999999999999801, 0.430000000000003, -0.289999999999999,
0.68, 0.52), HDLZ = c(-0.200000000000001, -0.200000000000001,
-0.0700000000000003, 0, -0.0200000000000014, -0.0199999999999996,
-0.0399999999999991, -0.119999999999999, -0.0900000000000016,
-0.0500000000000007, -0.15, -0.16, -0.640000000000001, 0.42,
0.16, -0.130000000000001, 0.15, 0.41, -0.0300000000000011, 0.18
)), class = "data.frame", row.names = c(NA, -20L))
library(pheatmap)
library(corrplot)
CorMethod <- "spearman"
CorMatrix <- cor(Data_corr, method=CorMethod, use="pairwise.complete.obs")
## 1st Plot
Plot3<-pheatmap(CorMatrix, cluster_cols=T, cluster_rows=T, cutree_rows = 3, angle_col=45, fontsize_col=5, fontsize_row = 7, treeheight_col=0, clustering_method="ward.D2")
#2nd Plot
Plot8 <-corrplot(CorMatrix, method="square", type="lower", order="hclust", hclust.method="ward.D2", tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45)
You can create a corrplot with the same order given by pheatmap as follows:
#2nd Plot
library(RColorBrewer)
ord <- Plot3$tree_row$order
ReordCorMatrix <- CorMatrix[ord, ord]
Plot8 <-corrplot(ReordCorMatrix, method="square", type="lower", order="original",
hclust.method="ward.D2",
tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45,
col=colorRampPalette(rev(brewer.pal(n = 7, name="RdYlBu")))(100))
I would like to plot a time series of the Standardized Precipitation Index (SPI). Normally this looks somewhat like this:
You can see that the area under/above the curve is colored in blue/red. This is what I would like to plot too.
I know that there were kind of similar questions, like this and that one. This could bring be a bit further, but unfortunately not to the final result yet.
To make it easier to understand, here is some code so that everybody can reproduce it:
library(data.table)
library(ggplot2)
vec1 <- 1:310
vec2 <- c(1.78, 1.88, 1.10, 0.42, 0.73, 1.35, 1.34, 0.54, 0.20, 0.72, 1.29, 1.78, 1.30, 1.37, -0.13, 0.64, -0.13, 0.87, 0.47, -0.26, -0.27,
-0.81, -0.54, -0.77, -0.29, -0.22, -0.05, 0.41, 0.45, 0.91, -0.31, 0.67, 0.28, 0.93, 0.43, -0.04, -0.80, -1.20, -0.73, -0.98, 0.47, -0.01,
1.30, 1.45, 0.72, -0.59, -1.14, -0.33, 0.22, 0.49, 0.58, 0.36, 0.66, 0.64, 0.47, -0.60, 1.01, 1.50, 1.18, 0.82, 0.02, 0.57, 0.25,
1.20, 1.19, 0.71, -0.30, -1.37, -1.50, -1.03, -0.77, -1.08, -1.92, -2.32, -2.46, -1.61, -0.39, 0.67, 0.38, 0.62, -0.34, 0.01, -0.55, -0.74,
-1.95, -1.18, -0.96, 0.36, -0.96, -1.28, -2.29, -2.67, -0.65, -0.13, 0.61, 0.21, 0.57, 0.11, 0.37, 0.20, -0.14, -0.87, -0.84, 0.87, 1.33,
0.45, -0.76, -1.27, -0.65, -0.29, 0.54, 0.14, -0.55, -0.94, -0.98, -0.44, -0.37, 0.72, 0.70, 0.95, 0.89, 1.10, 1.51, 1.11, 1.77, 1.20,
1.23, -0.72, -1.43, -2.11, -1.37, -0.80, -0.34, -0.14, 0.22, -0.65, -0.44, -0.86, -0.46, -0.67, -0.91, -0.40, -0.09, 0.22, 0.96, 0.71, 0.51,
-1.61, -1.62, -1.43, -0.27, 1.08, 1.76, 1.30, 0.78, 1.02, 1.01, 0.56, -0.32, 0.37, 0.31, 1.36, 1.49, 1.42, 0.78, -0.19, 0.64, 0.39,
0.47, -1.13, -1.45, -0.52, 0.43, -0.19, -0.97, -0.27, 0.63, 1.01, 1.01, 0.83, -0.56, -1.71, -0.29, 1.06, 1.82, 1.28, 0.88, 1.08, 1.78,
1.47, 0.74, -0.34, 0.14, 1.09, 1.49, 1.30, 0.28, -0.25, 0.24, -0.33, 0.05, -0.86, -0.69, -1.03, -0.59, 0.32, 0.61, 0.84, -0.18, -0.67,
0.46, 0.31, -0.72, -2.26, -2.85, -0.69, -0.77, 0.64, -1.49, -1.69, -1.55, -0.28, -0.80, -1.15, -0.38, 0.31, 0.18, -0.27, -0.84, -0.94, -1.23,
-0.53, -1.52, -0.73, -0.93, 0.25, -0.11, 0.38, 0.48, 0.10, -0.02, 0.26, 1.39, 1.61, 0.83, 0.09, 0.95, 1.07, 0.77, 0.23, 0.26, 0.85,
0.93, 0.91, 1.10, 0.47, 0.74, 1.42, 1.17, 0.32, -0.40, 0.76, 1.44, 1.69, 1.03, 0.01, 0.46, 0.61, 0.60, -0.09, -0.31, -0.96, -0.91,
-0.06, 0.75, 1.32, 1.29, 0.55, 0.43, -1.25, 0.12, -0.05, 0.18, -0.77, -2.19, -1.85, -2.12, -1.51, -1.14, -0.79, -0.82, -1.13, -1.72, -2.14,
-1.95, -0.63, 0.70, 0.64, 0.17, -1.04, -0.58, -0.57, -0.57, -1.05, -1.11, -0.59, -0.07, 1.22, 0.30, -0.15)
df <- data.frame(vec1, vec2)
colnames(df) <- c("ID", "SPI")
df = as.data.table(df)
There you have the entire SPI time series, one value for each month between 1980 and 2005.
So far I came to that result:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw()
When you run the code you will see the following plot:
This is going into the right direction, but the small white gaps between values in the first half are disturbing and not supposed to be there. So there must be something wrong.
It is supposed to look like this, just with the two colors blue and red for the positive and negative values:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_area() +
theme_bw()
The code leads to this image:
You can see that there are no white gaps between the single values and I have absolutely no idea what leads to these errors.
Anybody with an idea how to solve that?
OP, what you are observing are the artifacts related to the resolution of your graphics driver and the space between columns. The areas you show are composed of many filled columns next to one another on the x axis. You do not specify the width= argument for geom_col(), so the default value leaves a space between the individual values on the x axis. It's best to illustrate if we take only a section of your data along the x axis:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw() +
xlim(0,100) # just the first part on the left
There's your white lines - it's the space bewtween the columns. When you have the larger picture, the appearance of the white lines has to do with the resolution of your graphics device. You can test this if you save your graphic with ggsave() using different parameters for dpi=. For example, on my computer saving ggsave('filename.png', dpi=72) gives no lines, but ggsave('filename.png', dpi=600) shows the white lines in places.
There's an easy solution to this though, which is to specify the width= argument of geom_col() to be 1. Be default, it's set to 0.75 or 0.8 (not exactly sure), which leaves a gap between the next value (fills ~75 or 80% of the space). If you set this to 1, it fills 100% of the space allotted for that column, leaving no white space in-between:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red", width=1) +
geom_col(data = df[SPI >= 0], fill = "blue", width=1) +
theme_bw() +
xlim(0,100)
What if you change the distance between columns?
Just added "width=1" inside "aes" of ggplot.
ggplot(data = df, aes(x = ID, y = SPI, width=1)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw()
I get this:
image.result
x = c(0.80, 1.23, -0.13, -0.65, 0.53, -0.10, -0.96, 1.63, 2.79, 2.02, -1.03, -0.86,
0.58, 0.59, 0.60, -1.77, -0.77, -0.73, -0.43, 2.60, -0.81, -2.81, -2.13, 1.66,
1.54, -0.15, -0.31, 0.09, 2.47, 0.24, -0.75, 2.09, 0.46, -0.80, -0.50, 2.58,
0.80, 0.39, 0.82, -0.58, -1.09, -0.29, -1.26, -1.72, 1.54, 1.06, 1.21, 0.15,
-0.57, -0.32, -1.44, -1.56)
z = acf(x, lag.max = 4, type = c("covariance"))
z
I am stuck in Number 3 question of this image:
In Correlation plot, How can I keep the labels in the same order as in datafile, without rearranging?
This is the scripts I have been using;
library(corrplot)
library(RColorBrewer)
library(Hmisc)
CORR <- df
M <- cor(CORE)
pM <- rcorr(M)
corrplot(pM$r,
type="upper",
order="hclust",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)
sample data
df <- structure(list(Cytosol_1 = c(2.8, 0.31, 1.21, 1.84, 0.93, 2.71, 2.03, 0.93, 0.89, 0.4, 0.32, 1.8, 1.16, 0.39, 1.16, 0.76, 1.17, 0.95, 0.58, 2.68, 0.88, 0.59, 1.49, 0.48, 0.51, 1.04, 0.89, 3.48, 1.47), Cytosol_2 = c(1.61, 0.22, 1.42, 1.97, 0.88, 1.46, 1.43, 0.74, 0.72, 0.43, 0.51, 2.07, 1.29, 0.71, 0.92, 0.57, 1.9, 0.84, 0.4, 2.72, 1.08, 0.96, 1.75, 0.24, 0.76, 0.99, 2.35, 2.06, 1.24 ), Cytosol_3 = c(1.76, 0.27, 0.77, 1.23, 0.93, 1.43, 0.7, 0.44, 0.58, 0.47, 0.57, 0.85, 0.79, 0.75, 0.95, 0.85, 1.49, 1.19, 0.72, 1.92, 1.11, 1.18, 1.03, 0.75, 0.58, 0.7, 0.79, 1.64, 1.14), Cytosol_4 = c(1.41, 0.98, 0.73, 2.31, 1.07, 1.1, 1.31, 0.66, 0.69, 0.51, 0.53, 1.3, 1.37, 1.55, 0.99, 1.27, 1.22, 0.89, 1.21, 1.56, 1.14, 0.7, 0.3, 0.63, 1.73, 1.49, 0.92, 1.8, 1.7), Cytosol_5 = c(2.36, 0.25, 1.43, 2.76, 0.91, 2.88, 2.73, 0.79, 0.71, 0.15, 0.92, 1.94, 1.12, 0.64, 2.07, 0.68, 1.51, 0.66, 0.51, 1.91, 1.61, 0.68, 0.73, 0.7, 0.94, 1.24, 2.45, 3.12, 1.58), Ribosome_6 = c(1.52, 1.09, 1.58, 1.29, 0.92, 1.12, 0.8, 0.51, 0.87, 0.58, 0.42, 0.72, 1.39, 1.14, 1.87, 1.11, 1.11, 1.07, 0.84, 1.11, 1.17, 0.45, 0.49, 0.59, 0.89, 0.79, 0.67, 1.66, 1.86), Ribosome_7 = c(4.16, 0.98, 1.79, 2.21, 1.21, 1.31, 1.01, 0.02, 0.82, 0.51, 0.81, 0.73, 2.34, 1.04, 1.99, 0.92, 2.2, 0.74, 0.25, 1.71, 1.43, 0.67, 1.19, 0.49, 1.5, 1.14, 0.92, 3.67, 2.68), Ribosome_8 = c(2.02, 0.95, 1.79, 1.47, 0.87, 1.88, 0.97, 0.51, 0.77, 0.66, 0.54, 1, 1.54, 0.92, 1.73, 1.32, 1.89, 0.97, 0.87, 1.26, 1.1, 0.61, 0.49, 0.57, 0.91, 0.76, 0.86, 3.37, 3.1), Ribosome_9 = c(2.67, 0.45, 1.45, 1.41, 0.56, 1.93, 1.29, 0.44, 0.58, 0.38, 0.3, 1.15, 1.5, 0.67, 1.38, 0.72, 1.71, 0.74, 0.5, 2.2, 1.36, 0.74, 1.06, 0.54, 0.72, 0.83, 1.27, 2.55, 1.4), Ribosome_10 = c(2.19, 0.55, 1.39, 1.56, 0.62, 1.67, 1.31, 0.47, 0.46, 0.35, 0.4, 1.02, 1.32, 0.7, 0.96, 0.6, 1.63, 0.94, 0.38, 1.6, 0.92, 0.71, 0.81, 0.56, 0.77, 0.73, 1.14, 2.42, 1.11 ), ER_17 = c(1.41, 0.29, 0.32, 0.76, 0.7, 2.75, 2.78, 0.8, 0.96, 0.28, 0.82, 2.15, 1.19, 0.55, 0.78, 0.97, 1.42, 1.22, 0.92, 1.84, 0.6, 0.69, 0.43, 0.39, 0.38, 0.48, 0.36, 2.17, 1.03), ER_18 = c(1.02, 0.43, 0.98, 1.91, 0.88, 3.19, 1.05, 1.65, 0.53, 1.08, 0.39, 1.1, 0.36, 0.56, 0.58, 1.13, 1.25, 1.03, 0.79, 1.67, 0.56, 0.84, 1.17, 1.05, 0.18, 0.69, 0.09, 1.58, 0.47), ER_19 = c(0.58, 0.72, 0.58, 1.42, 0.52, 2.18, 0.95, 0.32, 1.44, 0.86, 0.24, 0.8, 0.62, 0.34, 0, 1.29, 1.29, 0.84, 1.43, 4.48, 1.82, 0.97, 0.83, 1.25, 0.29, 0.03, 1.48, 1.72, 1.89), Extracellular_20 = c(2.77, 0.43, 2.23, 1.86, 0.51, 2.96, 1.41, 0.91, 0.61, 0.64, 0.84, 2.03, 1.34, 0.69, 0.82, 0.5, 1.18, 0.77, 0.59, 1.65, 1, 0.74, 1, 0.55, 1.38, 1.38, 1.82, 1.58, 1.02), Extracellular_21 = c(3.4, 0.67, 1.91, 1.76, 0.77, 1.65, 1.04, 0.48, 0.53, 0.34, 0.48, 1.24, 1.49, 1.07, 1.24, 0.81, 1.4, 0.85, 0.46, 1.52, 0.9, 0.81, 0.6, 0.57, 1.32, 1.3, 2.22, 1.29, 0.98), Extracellular_22 = c(0.52, 0.01, 0.33, 2.25, 1.05, 2.63, 2.5, 0.99, 0.53, 1.21, 1.43, 3.2, 0.32, 0.2, 0.23, 0.52, 1.07, 0.51, 0.55, 2.58, 1.5, 1.19, 2.16, 0.81, 0.02, 0.1, 0.2, 2.38, 1.36), Extracellular_23 = c(0.16, 0, 0, 1.8, 0.74, 1.45, 2.6, 0.8, 1.68, 1.63, 2.41, 1.46, 0.52, 0.67, 0.05, 1.12, 0.78, 0.62, 0.56, 1.74, 1.03, 1.74, 1.14, 1.33, 0, 0, 0, 0.96, 1.46)), class = "data.frame", row.names = c(NA, -29L))
The corrplot function has an "order" argument, which allows you to specify how the rows and columns of the plot are arranged. Setting order = 'original' preserves the ordering in the source data frame:
corrplot(pM$r,
type="upper",
order="original",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)
I am very new to R and would like to create a filled contour plot in R to show oxygen concentration (z) changes in water column (y) over time (x). Bellow is how I made the initial plot using filled.contour() function. My questions are:
1- How to change the coloration of the plot? I like it to be between dark blue (high oxygen) to red (low oxygen).
2- As depth appears on Y axis, how can I make it start from 22 m (maximum depth of the lake) instead of 0 m to make plot easier to understand?
Thanks you
mat <- structure(c(9.76, 11.25, 11.15, 8.56, 8.94, 6.51, 8.30, 9.16,
6.75, 6.16, 6.48, 7.18, 7.17, 5.34, 5.03, 9.78, 11.52, 11.22, 7.39,
8.97, 6.48, 8.28, 9.40, 6.69, 6.11, 6.34, 7.06, 6.45, 5.05, 4.51, 9.72,
11.57, 11.21, 8.35, 8.96, 6.47, 8.21, 9.37, 6.60, 6.08, 6.31, 6.90,
5.86, 4.51, 3.56, 10.16, 11.53, 9.75, 8.53, 8.96, 6.44, 8.12, 9.37,
6.48, 6.04, 6.25, 6.81, 5.62, 4.19, 3.21, 10.48, 9.70, 8.78, 7.89,
9.00, 6.43, 8.08, 8.41, 6.33, 6.02, 6.22, 6.68, 5.51, 3.73, 2.88, 8.95,
8.01, 7.48, 7.42, 7.89, 6.41, 7.95, 7.80, 6.13, 5.92, 6.20, 6.52, 4.89,
3.31, 2.27, 7.41, 5.06, 4.10, 4.45, 3.25, 6.40, 7.71, 7.46, 5.97, 5.90,
6.17, 6.15, 4.31, 2.94, 1.72, 5.71, 3.86, 1.27, 1.97, 0.61, 6.38, 5.94,
7.43, 5.89, 5.87, 6.15, 5.81, 3.70, 2.70, 1.26, 5.02, 3.30, 0.80, 0.27,
0.48, 6.34, 4.89, 7.10, 5.87, 5.87, 6.12, 5.52, 3.34, 2.23, 0.58, 3.90,
3.02, 0.43, 0.18, 0.41, 5.92, 2.43, 6.97, 5.91, 5.92, 6.10, 5.01, 3.01,
0.32, 0.18, 3.46, 2.51, 0.24, 0.19, 0.38, 0.51, 0.19, 6.03, 5.92, 5.93,
6.09, 4.79, 1.86, 0.26, 0.10, 2.87, 1.35, 0.18, 0.14, 0.36, 0.23, 0.03,
5.52, 5.90, 5.97, 6.08, 4.46, 0.72, 0.20, 0.09, 1.67, 0.72, 0.12, 0.10,
0.34, 0.14, 0.00, 1.64, 5.72, 5.27, 6.06, 4.05, 0.52, 0.15, 0.07, 0.82,
0.59, 0.09, 0.08, 0.33, 0.13, 0.00, 0.41, 5.66, 5.86, 6.05, 0.90, 0.44,
0.14, 0.06, 0.48, 0.43, 0.07, 0.07, 0.32, 0.11, 0.00, 0.25, 2.47, 5.85,
6.03, 0.49, 0.39, 0.13, 0.05, 0.38, 0.37, 0.07, 0.07, 0.33, 0.10, 0.00,
0.19, 0.14, 5.62, 6.02, 0.35, 0.38, 0.12, 0.05, 0.28, 0.34, 0.07, 0.06,
0.33, 0.09, 0.00, 0.16, 0.08, 4.11, 6.02, 0.28, 0.36, 0.11, 0.04, 0.30,
0.33, 0.09, 0.07, 0.32, 0.09, 0.00, 0.15, 0.06, 0.72, 6.02, 0.24, 0.35,
0.10, 0.03, 0.31, 0.31, 0.11, 0.08, 0.31, 0.08, 0.00, 0.14, 0.04, 0.10,
6.01, 0.21, 0.34, 0.09, 0.02, 0.38, 0.31, 0.09, 0.07, 0.30, 0.08, 0.00,
0.14, 0.03, 0.06, 6.01, 0.22, 0.33, 0.07, 0.02, 0.44, 0.30, 0.07, 0.05,
0.29, 0.07, 0.00, 0.13, 0.02, 0.04, 6.01, 0.26, 0.31, 0.05, 0.02, 0.41,
0.30, 0.04, 0.04, 0.28, 0.06, 0.00, 0.14, 0.01, 0.02, 6.02, 0.27, 0.31,
0.05, 0.02, 0.37, 0.29, 0.04, 0.03, 0.28, 0.06, 0.00, 0.14, 0.00, 0.01,
6.02, 0.26, 0.31, 0.05, 0.02), .Dim = c(15L, 23L), .Dimnames =
list(c("06/25/2015", "07/08/2015","07/22/2015", "08/04/2015",
"08/18/2015", "09/01/2015", "09/15/2015", "09/30/2015","10/14/2015",
"10/26/2015", "11/12/2015", "01/06/2016", "01/20/2016", "02/04/2016",
"02/16/2016"), c("0.5", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22")))
dates <- as.Date(dimnames(mat)[[1]], "%m/%d/%Y")
depths <- as.numeric(dimnames(mat)[[2]])
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth")
I couldn't figure out what you were talking about regarding starting at 22, but this will get you part of the way there.
cols = rev(colorRampPalette(c('darkred','red','blue','lightblue'))(24))
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth",
col = cols
)
By the way, if I reversed the coloring then just remove the rev() statement.