R 如何在ggplot中放置和包装图例

R 如何在ggplot中放置和包装图例,r,R,我想把图例放在绘图的右下角,就像原始的热图一样(我用红色圈出了这一部分) 数据如下: 关于这些数据的背景资料很少,似乎热图是基于4组平均值的z分数,因为他们有两组,我想可能是团队生成了每两列的平均值,并从这四组的平均值中得出了z分数。 我尝试了以下代码: library(dplyr) library(tidyverse) library(stringr) library(matrixStats) library(pheatmap) library(heatmaps) dfc <- re

我想把图例放在绘图的右下角,就像原始的热图一样(我用红色圈出了这一部分)

数据如下:

关于这些数据的背景资料很少,似乎热图是基于4组平均值的z分数,因为他们有两组,我想可能是团队生成了每两列的平均值,并从这四组的平均值中得出了z分数。 我尝试了以下代码:

library(dplyr)
library(tidyverse)
library(stringr)
library(matrixStats)
library(pheatmap)
library(heatmaps)

dfc <- read.csv(url("https://github.com/learnseq/learning/raw/main/GSE133399_Fig2_FPKM.csv"))

head(dfc)

dfg <- dfc %>% dplyr::filter(tracking_id %in% c(
    "Ifng", "Igfbp7", "Il13", "Il4", "Itgb1", "Rbpj",
    "Tnfsf11", "Xcl1", "Ern1", "Furin", "Il5", "Nrp1", "Ptprs",
    "Spry1", "Vdr", "Foxp3", "Prdm1", "Itgb8", "Lamc1", "Ptpn5",
    "Bmpr2", "Csf1", "Dst", "Myo1e", "Pmaip1", "Itgav", "Ramp1",
    "Sdc4", "Areg", "Calca", "Capg", "Ccr2", "Cd44", "Il10", "Il1rl1",
    "Maf", "Rora", "S100a4", "Adam8", "Adam19", "Anxa2", "Bcl2l1",
    "Csda", "Ehd1", "Hist1h1b", "Id2", "Il2ra", "Il2rb", "Lgals1",
    "Lmna", "Mki67", "Penk", "Podnl1", "S100a6", "Vim")) 

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)
head(dfg)
dim(dfg)
head(dfg)
rownameshm <-paste(dfg[,1])
rownameshm
colnameshm <- paste(dQuote(colnames(dfg[0, 10:13])), collapse = ", ")
colnameshm
dfg$Mean <- rowMeans(dfg[,10:13])
dfg$sd <- rowSds(as.matrix(dfg[,10:13]))
head(dfg)

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))
head(Matrix_zScore)
Matrix_zScore_temp <- mapply(Matrix_zScore[,10:13], FUN=as.numeric)
Matrix_zScore_temp <- matrix(data=Matrix_zScore_temp, ncol=4, nrow=55)
Matrix_zScore_temp1<-as.data.frame(Matrix_zScore_temp)

rownames(Matrix_zScore_temp) <- dfg$tracking_id
plot_frame <- reshape2::melt(Matrix_zScore_temp)

library("tidyverse")
library(repr)
options(repr.plot.width=4, repr.plot.height=8)


ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
  scale_y_discrete(position = "right") +
  labs(y = "", fill = "") +
  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29")) +
  theme_minimal() +  theme(
           legend.position = c(1, 0), 
      legend.justification = c(-0.9, 0),
          legend.direction = "vertical",
   legend.key.size = unit(0.5, "cm"),
  legend.key.width = unit(0.3,"cm"))

库(dplyr)
图书馆(tidyverse)
图书馆(stringr)
图书馆(matrixStats)
图书馆(pheatmap)
图书馆(热图)

dfc我创建了一些数据,其中每个值都在-1和1之间。然后,您可以使用以下命令:

ggplot(数据,aes(X,Y,fill=Z))+
geom_瓷砖(color=“white”,show.legend=TRUE)+
比例y离散(位置=“右”)+
实验室(y=,fill=)+
比例填充梯度n(
颜色=c(“#3C57A8”、“白色”、“DE2D29”),
断开=c(1,0,-1),
标签=c(1,0,-1),
极限=c(-1,1),
guide=“colorbar”
) +
向导(
填充=导向装置\u色条(
title=“相对基因表达\n(z分数)”,
title.position=“right”,
title.theme=元素\文本(角度=-90,大小=7),
direction=“垂直”,
ticks=TRUE
)
) +
主题_极小值()+
主题(
plot.margin=余量(r=单位(50,“厘米”),
legend.position=c(1.11,0.1025),
图例.键.尺寸=单位(0.5,“cm”),
图例.键.宽度=单位(0.3,“cm”),
图例.title.align=0.5
)
您可以使用
plot.margin
调整绘图的边距并为图例创建更多空间。在这种情况下,我们可以使用
边距(r=unit(50,“cm”)
在绘图的右侧添加空间。将其与
图例一起使用。position
将使您能够将图例精确定位到所需的位置

这并不准确,因为
guide\u colorbar()
title.hjust
参数似乎只调整标题的最后一行,但您可以通过减小标题的大小来稍微绕过这一点。然而,这可能会引导你更接近你所追求的


指南\u colorbar()
文档,它可能有助于标题。

这非常有用@JustinSingh!我感谢你的帮助。我只是试着在那里玩了一些数字,我几乎得到了我想要的,但是颜色不一样,有没有关于如何复制颜色的输入?另外,如何删除这些小线条(我用你的图片更新了问题)作为记号,你可以在
主题()中使用以下选项之一:
axis.text.x=element\u blank()
axis.text.y=element\u blank()
,和/或
axis.ticks=element\u blank()
。至于颜色,这可能是由于
比例填充
选项:
中断
标签
、和
限制
--
中断
标签
在这种情况下共同指定图例上的标签值;鉴于,
限制
限制数据并删除其范围之外的任何内容。在我的例子中,我创建的数据在-1到1的范围内,但从来没有每个值。您可以尝试删除
限制
。再次感谢您!我尝试了
axis.text.x=element\u blank(),axis.text.y=element\u blank()
它会删除文本,但不会删除刻度线@JustinSinghI删除了我的另一条评论,因为你注意到的那些行实际上不是记号。起初我没有注意到,但实际上是热图后面背景中的网格。要删除此项,我们将
panel.grid.major=element\u blank()
添加到
主题()
。(至于我删除的注释,
axis.ticks.y
axis.ticks.x
是记号标记的实际选项,而不是
axis.text.*
I输入错误)您可以通过设置轴记号标签的边距来调整轴记号标签,类似于
plot.margin
theme()
选项的外观与此类似:
axis.text.y.right=element\u text(边距=边距(l=单位(-3,“cm”))
。在这里,我将左侧的边距设置为负数,以删除右侧y轴刻度标签左侧的部分边距,以便它们更靠近绘图。希望有帮助!而且,为了避免长时间的讨论,如果您需要进一步的帮助,我们可以转移到聊天室。
zmap001 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
  scale_y_discrete(position = "right") +
  labs(y = "", fill = "") +
  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29")) +
  theme_minimal() +
theme(
           legend.position = c(1, 0), 
      legend.justification = c(0.9, 0),
          legend.direction = "vertical",
   legend.key.size = unit(0.5, "cm"),
  legend.key.width = unit(0.3,"cm")) 

zmap002 <- zmap001 + guides(title = "Relative gene expression\n        (z score)", subtitle = "(z score)")
zmap002

zmap01 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
  scale_y_discrete(position = "right") +
  labs(y = "", fill = "") +
  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29")) +
  theme_minimal() +
theme( legend.position = c(1, 0), 
      legend.justification = c(0.9, 0),
          legend.direction = "vertical",
   legend.key.size = unit(0.5, "cm"),
  legend.key.width = unit(0.3,"cm"), legend.title = element_text(angle = -90)) 

zmap02 <- zmap01 + guides(fill = guide_legend(title.position = "right", title = "Relative gene expression\n        (z score)", subtitle = "(z score)"))
zmap02

zmap01 <- ggplot(plot_frame, aes(Var2, Var1, fill = value), colour=str_wrap()) + 
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
  scale_y_discrete(position = "right") +
  labs(y = "", fill = "") +
  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29")) +
  theme_minimal() +
theme( plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"), complete = TRUE, 
           legend.position = c(1, 0), 
      legend.justification = c(0.9, 0),
          legend.direction = "vertical",
   legend.key.size = unit(0.5, "cm"),
  legend.key.width = unit(0.3,"cm"), legend.title = element_text(angle = -90)) 

zmap02 <- zmap01 + guides(fill = guide_legend(title.position = "right", title = "Relative gene expression\n(z score)"))
zmap02