플롯의 x 축에 점을 추가하는 방법

Nov 13 2020

저는 히트 맵 코딩을 연습하고 있고,자가 학습자입니다. 원래 히트 맵과 같이 플롯 하단에 범례가있는 점을 원합니다 (이 부분은 빨간색으로 동그라미 표시).

데이터는이 링크에 있습니다 .

이 데이터에 대한 배경은 거의 없지만 히트 맵은 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"))

내 히트 맵은 다음과 같습니다.

답변

3 AllanCameron Nov 14 2020 at 13:34

geom_point몇 가지 큰 포인트와 색상 미학을 사용 하여 호출을 추가 할 수 있습니다 . Var1값을 "" 로 설정하여 알파벳순으로 먼저 나오고 y 축에 공간을 만듭니다. 그러면 히트 맵에서 x 축을 식별하는 데 필요한 색상 범례도 자동으로 추가됩니다.

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 = c("CD44 T Cells",
                                "CD44 CD69 T Cells",
                                "CD44 CD69 CD103-lo T Cells",
                                "CD44 CD69 CD103-hi T Cells"),
                     guide = guide_legend(override.aes = list(fill = NA))) +
  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29")) +  
  scale_y_discrete(position = "right") +
  labs(y = "", fill = "", color = " ", x = "") +
  theme_minimal() +  
  theme(legend.justification = c(-0.9, 0),
        legend.direction     = "vertical",
        axis.text.x          = element_blank(),
        legend.key.size      = unit(0.5, "cm"),
        legend.key.width     = unit(0.3,"cm"),
        axis.text.y          = element_text(face = "italic"),
        panel.grid           = element_blank())