1

我正在尝试以 pivot_long 形式绘制数据,以使用 geom_tile 呈现为热图。但是,我在订购图中的瓷砖时遇到了麻烦。

示例数据https://drive.google.com/file/d/1WIjbN9-xP-1Wgc2Nx3GlterV8XhtnGyu/view?usp=sharing

这是我生成的图:

问题是我想要 y 轴标签,也就是“Drug.dose”,在添加的组合的“无”部分中按数值从高到低排列(因子设置为无、I30、I300 ...... I300_V100)

我的绘图代码如下:通过在我的 y 轴上使用 reorder()(如何保留 geom_tile ggplot 中的图块顺序),它按组合中添加的所有内容从高到低排列,因此您可以看到我在none 是 TN 0.1,但由于 I30、I300 等中的所有零,它进入了图的底部。列表中还有其他不一致之处。

我如何仅通过添加组合的 none 部分来重新排序?

library(ggplot2)

m <- ggplot(data)+
  geom_tile(aes(x=Combination, y=reorder(Drug.dose,Avg.percent), fill=Avg.percent))+
  geom_text(aes(x=Combination, y=reorder(Drug.dose,Avg.percent), label=Avg.percent), size=3)+
  scale_fill_gradientn(colors=pal)+
  theme(legend.text = element_text(size=10, face="bold", color = "black"))+
  theme(axis.text.x = element_text(size = 15,  face="bold", color = "black")) +
  theme(axis.text.y = element_text(size = 9,  face="bold", color = "black")) +
  theme(axis.title.x = element_text(size = 15,  face="bold", color = "black", vjust = 3))+
  theme(axis.title.y = element_text(size = 15,  face="bold", color = "black", hjust = 0.5))+
  theme(plot.title = element_text(size = 16))+
  theme(strip.text.y  = element_text(size = 10, face = "bold", color = "black"))+
  scale_x_discrete(position ="top") +
  xlab("Combination added")+
  ylab("Treatments in the screen")+
  ggtitle("Cluster 1 Enriched in TN response")


print(m)

 

在此处输入图像描述

4

2 回答 2

2

像这样的东西?只需创建一个静态变量来管理你的颜色渐变。

library(tidyverse)
levels <- c("none","I30","I300","I30_V10","I300_V100","V10","V100" )

# Data directory %>%
  read_csv %>% 
  pivot_wider(names_from = Combination,
              values_from = Avg.percent) %>% 
  mutate(color = none) %>% 
  pivot_longer(cols = c("none", starts_with(c(c("I","V"), ignore.case = F))),
               names_to = "Combination",
               values_to = "Avg.percent") %>% 
  mutate(Combination = factor(Combination,
                              levels = levels))-> data

m <- ggplot(data)+
  geom_tile(aes(x=Combination, y=reorder(Drug.dose, color), fill=Avg.percent)) +
  geom_text(aes(x=Combination, y=reorder(Drug.dose, color), label=Avg.percent), size=3)+
  # scale_fill_gradientn(colors=pal)+
  ggsci::scale_fill_material("red") +
  theme(legend.text = element_text(size=10, face="bold", color = "black"))+
  theme(axis.text.x = element_text(size = 15,  face="bold", color = "black")) +
  theme(axis.text.y = element_text(size = 9,  face="bold", color = "black")) +
  theme(axis.title.x = element_text(size = 15,  face="bold", color = "black", vjust = 3))+
  theme(axis.title.y = element_text(size = 15,  face="bold", color = "black", hjust = 0.5))+
  theme(plot.title = element_text(size = 16))+
  theme(strip.text.y  = element_text(size = 10, face = "bold", color = "black"))+
  scale_x_discrete(position ="top") +
  xlab("Combination added")+
  ylab("Treatments in the screen")+
  ggtitle("Cluster 1 Enriched in TN response")

print(m)
于 2020-11-04T20:33:50.747 回答
1

我认为最好的方法是在将数据放入 ggplot 之前对其进行排序。可能有使用tidyr或其他方法的解决方案,但我对此了解不多。

在 时获取配对值Combination=="none",并按以下顺序排序Avg.percent

index = data[data$Combination=="none", c("Drug.dose", "Avg.percent")]
index = index[order(index$Avg.percent),]

创建一个变量order,为 中的index每个级别提供值Drug.dose

for(i in unique(data$Drug.dose)){
  data$order[data$Drug.dose==i] = index[index$Drug.dose==i,2]}

然后在inorder的地方使用。输出(根据您的级别“无”应该是第一行):Avg.percentreorder()

在此处输入图像描述

于 2020-11-04T20:47:12.380 回答