r - R中图例中的上标
问题描述
我正在尝试在图例中获取此文本输出:
我尝试了以下方法:
lab1 <- c(expression((CD44 (lo)^2 T Cells)]),
expression((CD44 (hi)^2 CD69 (lo)^2 T Cells)]),
expression((CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)
expression(CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells))
lab2 <- c(expression(paste(CD44 (lo)^2 T Cells)]),
expression(paste(CD44 (hi)^2 CD69 (lo)^2 T Cells)]),
expression(paste(CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)
expression(paste(CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells))
lab3 <- c(bquote((CD44 (lo)^2 T Cells)),
bquote((CD44 (hi)^2 CD69 (lo)^2 T Cells)),
bquote((CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)),
bquote((CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells)))
我的代码:
library(dplyr, warn.conflicts = FALSE)
library(tidyverse, warn.conflicts = FALSE)
library(stringr, warn.conflicts = FALSE)
library(matrixStats, warn.conflicts = FALSE)
library(pheatmap, warn.conflicts = FALSE)
library(heatmaps, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
dfc <- read.csv(url("https://github.com/learnseq/learning/raw/main/GSE133399_Fig2_FPKM.csv"))
values <- c('S100a10', 'Esm1', 'Itgb1', 'Anxa2', 'Hist1h1b',
'Il2rb', 'Lgals1', 'Mki67', 'Rora', 'S100a4',
'S100a6', 'Adam8', 'Areg', 'Bcl2l1', 'Calca',
'Capg', 'Ccr2', 'Cd44', 'Csda', 'Ehd1',
'Id2', 'Il10', 'Il1rl1', 'Il2ra', 'Lmna',
'Maf', 'Penk', 'Podnl1', 'Tiam1', 'Vim',
'Ern1', 'Furin', 'Ifng', 'Igfbp7', 'Il13',
'Il4', 'Il5', 'Nrp1', 'Ptprs', 'Rbpj',
'Spry1', 'Tnfsf11', 'Vdr', 'Xcl1', 'Bmpr2',
'Csf1', 'Dst', 'Foxp3', 'Itgav', 'Itgb8',
'Lamc1', 'Myo1e', 'Pmaip1', 'Prdm1', 'Ptpn5',
'Ramp1', 'Sdc4')
dfg <- dfc[match(rev(values), dfc$tracking_id), ]
dfg$CD44low_rep <- rowMeans(dfg[,c('CD44low_rep1', 'CD44low_rep2')], na.rm=TRUE)
dfg$CD44hi_CD69low_rep <- rowMeans(dfg[,c('CD44hi_CD69low_rep1', 'CD44hi_CD69low_rep2')], na.rm=TRUE)
dfg$CD44hi_CD69hi_CD103low_rep <- rowMeans(dfg[,c('CD44hi_CD69hi_CD103low_rep1', 'CD44hi_CD69hi_CD103low_rep2')], na.rm=TRUE)
dfg$CD44hi_CD69hi_CD103hi_rep <- rowMeans(dfg[,c('CD44hi_CD69hi_CD103hi_rep1', 'CD44hi_CD69hi_CD103hi_rep2')], na.rm=TRUE)
rownameshm <-paste(dfg[,1])
colnameshm <- paste(dQuote(colnames(dfg[0, 10:13])), collapse = ", ")
dfg$Mean <- rowMeans(dfg[,10:13])
dfg$sd <- rowSds(as.matrix(dfg[,10:13]))
zScore <- function(p){
for(n in 10:13){
p[[n]]=(as.numeric(p[[n]])-as.numeric(p[[14]]))/as.numeric(p[[15]])
}
return(p)
}
Matrix_zScore <- t(apply(dfg,1,zScore))
Matrix_zScore_temp <- mapply(Matrix_zScore[,10:13], FUN=as.numeric)
Matrix_zScore_temp <- matrix(data=Matrix_zScore_temp, ncol=4, nrow=57)
Matrix_zScore_temp1<-as.data.frame(Matrix_zScore_temp)
rownames(Matrix_zScore_temp) <- dfg$tracking_id
plot_frame <- reshape2::melt(Matrix_zScore_temp)
library(repr, warn.conflicts = FALSE)
options(repr.plot.width=5, repr.plot.height=8)
#lab1 <- c(expression((CD44 (lo)^2 T Cells)]),
# expression((CD44 (hi)^2 CD69 (lo)^2 T Cells)]),
# expression((CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)
# expression(CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells))
lab2 <- c(expression(paste(CD44 (lo)^2 T Cells)]),
expression(paste(CD44 (hi)^2 CD69 (lo)^2 T Cells)]),
expression(paste(CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)
expression(paste(CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells))
#lab3 <- c(bquote((CD44 (lo)^2 T Cells)),
# bquote((CD44 (hi)^2 CD69 (lo)^2 T Cells)),
# bquote((CD44 (hi)^2 CD69 (hi)^2 CD103 (lo)^2 T Cells)),
# bquote((CD44 (hi)^2 CD69 (hi)^2 CD103 (hi)^2 T Cells)))
ggplot(plot_frame, aes(Var2, Var1, fill = value)) +
geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = "", value = 0), size = 5,
aes(color = factor(Var2))) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = " ", value = 1), alpha = 0) +
scale_color_manual(values = c("black", "forestgreen", "red4", "blue4"),
labels = (lab2,
guide = guide_legend(override.aes = list(fill = NA))) +
scale_y_discrete(position = "right") +
labs(y = "", fill = "", color = " ", x = "") +
scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29"),
breaks = c(1.5, 0, -1.5),
labels = c("1.0", "0", "-1.0"),
limits = c(-1.5, 1.5),
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "fill") +
theme_minimal() +
theme (panel.grid.major = element_blank(), axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm")))) +
theme (axis.text.y = element_text(face="italic", size=7, color="black")) +
guides(
fill = guide_colourbar(
title = "Relative gene expression \n (z score)",
title.position = "right",
title.theme = element_text(angle = -90, size = 7.5),
direction = "vertical",
ticks = TRUE
)) +
theme(
legend.justification = c(-0.9, 0),
legend.direction = "vertical",
legend.key.size = unit(0.6, "cm"),
legend.key.width = unit(0.2,"cm"),
legend.title.align = 0.5,
axis.text.x = element_blank())
我根据这个社区的一些问题尝试了这些。
解决方案
这个例子对于仅仅创建一个图例来说似乎是不必要的复杂。这是一个简化的例子
library(ggplot2)
labs <- expression(
CD44^{lo}~"T Cells",
CD44^{hi}~CD69^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")
dd <- data.frame(x=1:4, y=1:4, z=letters[1:4])
ggplot(dd) +
aes(x,y, color=z) +
geom_point() +
scale_color_manual(
values = c("black", "forestgreen", "red4", "blue4"),
labels=labs)
请注意,它expression()
实际上返回一个容器对象,因此您可以在一次调用中传入所有表达式expression()
。?plotmath
此外,请查看帮助页面上允许的语法。
这将返回一个看起来像的图例
推荐阅读
- javascript - 反应在单击时复制呈现的 div
- vba - 使用 TempVars 去记录
- c# - WPF 中的 DispatcherUnhandledExceptionEventHandler 捕获异常是 Visual Studio 但未在客户端计算机中写入日志
- javascript - 为什么必须设置原型而不是直接在函数构造函数中调用方法?
- python - 在执行 PySpark 代码时遇到 Py4JJavaError
- drools - 如何在调用 kie api 容器时使用激活组?
- python - 在 Odoo 视图中以文本形式输出字段
- java - Jackson 的 @JsonProperty 枚举
- amazon-web-services - 为 Lambda 的 AWS Cloudwatch 上的 Paho MQTT 客户端启用日志
- python - 使用 Python 将存储传输作业从一个谷歌云存储区域转移到另一个“无法重写对象”。