如何仅使用部分数字变量对 geom_tile ggplot 中的图块进行排序
How to order of tiles in geom_tile ggplot by using only part of the numeric variables
我正在尝试绘制 pivot_long 形式的数据以使用 geom_tile 呈现为热图。
但是我在订购图中的图块时遇到了问题。
样本数据https://drive.google.com/file/d/1WIjbN9-xP-1Wgc2Nx3GlterV8XhtnGyu/view?usp=sharing
这是我生成的图:
问题是我想要 y 轴标签,又名“Drug.dose”,在组合的“none”部分中按数值从高到低排序(因素设置为具有水平none, I30, I300.... I300_V100)
我的绘图代码如下:通过在我的y轴上使用reorder()(How to preserve the order of tiles in 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)
是这样的吗?只需创建一个静态变量来管理你的颜色渐变。
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)
我认为最好的方法是在将数据放入 ggplot 之前对其进行排序。可能有使用 tidyr
或其他方法的解决方案,但我对此了解不多。
得到Combination=="none"
时的配对值,按Avg.percent
排序:
index = data[data$Combination=="none", c("Drug.dose", "Avg.percent")]
index = index[order(index$Avg.percent),]
创建一个变量 order
,为 Drug.dose
中的每个级别提供 index
的值:
for(i in unique(data$Drug.dose)){
data$order[data$Drug.dose==i] = index[index$Drug.dose==i,2]}
然后用order
代替reorder()
中的Avg.percent
。输出(按照你的级别顺序“none”应该是第一行):
我正在尝试绘制 pivot_long 形式的数据以使用 geom_tile 呈现为热图。 但是我在订购图中的图块时遇到了问题。
样本数据https://drive.google.com/file/d/1WIjbN9-xP-1Wgc2Nx3GlterV8XhtnGyu/view?usp=sharing
这是我生成的图:
问题是我想要 y 轴标签,又名“Drug.dose”,在组合的“none”部分中按数值从高到低排序(因素设置为具有水平none, I30, I300.... I300_V100)
我的绘图代码如下:通过在我的y轴上使用reorder()(How to preserve the order of tiles in 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)
是这样的吗?只需创建一个静态变量来管理你的颜色渐变。
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)
我认为最好的方法是在将数据放入 ggplot 之前对其进行排序。可能有使用 tidyr
或其他方法的解决方案,但我对此了解不多。
得到Combination=="none"
时的配对值,按Avg.percent
排序:
index = data[data$Combination=="none", c("Drug.dose", "Avg.percent")]
index = index[order(index$Avg.percent),]
创建一个变量 order
,为 Drug.dose
中的每个级别提供 index
的值:
for(i in unique(data$Drug.dose)){
data$order[data$Drug.dose==i] = index[index$Drug.dose==i,2]}
然后用order
代替reorder()
中的Avg.percent
。输出(按照你的级别顺序“none”应该是第一行):