Using ggplot, I want to create a correlation matrix where some of the tiles are labeled with the p-value. Within those tiles, I want the "p" to be italicized.
This is plot that I want, except the "p"s should be italicized.
Right now, I am using the aes(label) to fill the tiles, and creating those labels outside of the plot. That way, I could just create blank labels to be displayed inside the plot.
The plot is currently made using:
figure1 <- ggplot(iris.corr.data, aes(colvars, fct_rev(rowvars)))
figure1 +
geom_tile(colour="grey70", aes(fill=corr), size = 0.5) +
scale_fill_gradient2(name = "Pearson\nCorrelation",low = "#D7191C", mid = "white", high = "#2C7BB6", midpoint=0, limits=c(-1,1)) +
geom_text(aes(label= p_labels), size=2.5, colour="black") +
labs(x="",y="") +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, vjust = .5, size = 9))+
coord_fixed()
"p_labels" is created earlier using:
iris.corr.data$p_labels[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0] <- paste("p = ", formatC(iris.corr.data$p.value[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0], digits = 2))
where p.value is a variable containing all of the p-values for each of the correlations.
I have tried using the expression function when I create the p_labels variable, but I have been unsuccessful saving italicized characters in the variable. I think that I must have to express the italic in the plot itself, but I'm not sure how italicize part of the element in ggplot when I uses geom_text(aes(label = p_labels))
Here is the reproducible code using the iris dataset:
library("corrplot")
library("tidyverse")
corr.data = function(data) {
# Correlations
cor.vals = cor(data, use = "complete.obs")
# P-values
cor.p = cor.mtest(data, conf.level = .99)$p
rownames(cor.p) = rownames(cor.vals)
colnames(cor.p) = colnames(cor.vals)
cbind(rowvars=rownames(cor.vals), data.frame(cor.vals)) %>%
gather(colvars, corr, -rowvars) %>%
left_join(cbind(rowvars=rownames(cor.p), data.frame(cor.p)) %>%
gather(colvars, p.value, -rowvars))
}
df <- subset(iris, select = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))
iris.corr.data <- corr.data(df)
iris.corr.data$p_labels <- ""
iris.corr.data$p_labels[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0] <- paste("p = ", formatC(iris.corr.data$p.value[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0], digits = 2))
figure1 <- ggplot(iris.corr.data, aes(colvars, fct_rev(rowvars)))
figure1 +
geom_tile(colour="grey70", aes(fill=corr), size = 0.5) +
scale_fill_gradient2(name = "Pearson\nCorrelation",low = "#D7191C", mid = "white", high = "#2C7BB6", midpoint=0, limits=c(-1,1)) +
geom_text(aes(label= p_labels), size=2.5, colour="black") +
labs(x="",y="") +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, vjust = .5, size = 9))+
coord_fixed()
This should work (use italic()
along with a plotmath
expression and parse=TRUE
in geom_text()
):
iris.corr.data$p_labels[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0] <- paste("italic(p) == ", formatC(iris.corr.data$p.value[iris.corr.data$p.value < 0.1 & iris.corr.data$p.value > 0], digits = 2))
ggplot(iris.corr.data, aes(colvars, fct_rev(rowvars))) +
geom_tile(colour="grey70", aes(fill=corr), size = 0.5) +
scale_fill_gradient2(name = "Pearson\nCorrelation",low = "#D7191C", mid = "white", high = "#2C7BB6", midpoint=0, limits=c(-1,1)) +
geom_text(aes(label= p_labels), size=2.5, colour="black", parse=TRUE) +
#geom_text(aes(label= p_labels), size=2.5, colour="black") +
labs(x="",y="") +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, vjust = .5, size = 9))+
coord_fixed()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.