r - 如何仅使用部分数值变量来对 geom_tile ggplot 中的图 block 进行排序

标签 r ggplot2 geom-tile

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

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

这是我生成的图:

问题是我想要 y 轴标签,又名“药物.剂量”,在添加的组合的“无”部分中按数值从高到低排名(因子设置为无级别、I30、I300... .I300_V100)

我的绘图代码如下:通过在我的 y 轴( How to preserve the order of tiles in geom_tile ggplot )上使用 reorder() ,它按组合中添加的所有内容从高到低排名,因此您会看到我的最高值是 TN 0.1但由于 I30、I300 等中的所有零,它到达了图的底部。并且列表中还有其他不一致的地方。

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

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)

 

enter image description here

最佳答案

有这样的事吗?只需创建一个静态变量来管理你的颜色渐变即可。

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)

关于r - 如何仅使用部分数值变量来对 geom_tile ggplot 中的图 block 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64686414/

相关文章:

R(统计)范围错误使用 transformBy(),doBy 包的一部分

r - 对齐多个具有不同间距的图并在它们之间添加箭头

r - 为什么 Cohen 的 kappa 计算在此列联表上的不同包之间失败?

r - ggplot2:aspect.ratio 胜过 coord_equal 或 coord.fixed

r - 使用 ggplot 连接 map 中的 2 个点

r - 在 R 中的网格中模拟二维随机游走并使用 ggplot 绘图

r - 使用 geom_tile() 为热图中的每一列设置不同的颜色

r - 在 R 中对同名的列进行分组

r - 将绘图文本/二进制写入变量