我正在尝试使用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)
最佳答案
有这样的事吗?只需创建一个静态变量来管理你的颜色渐变即可。
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/