如何在 ggplot 中重新排列热图 y 轴上的对象?
How can I reorder the objects on the y-axis of my heatmap in ggplot?
这是我目前拥有的:
heat=read.csv("heat.csv")
head(heat)
heat$trap <- as.character(heat$trap)
class(heat$trap)
levels(heat$trap) <- c("T1", "T2", "T3", "T4","T5", "T6", "T7", "T8", "T9", "T10")
saeat <- ggplot(data = heat, mapping = aes(x = trip, y = trap, fill = sand)) + geom_tile() + ylab(label = "Trap") + xlab(label = "Month") + scale_fill_gradient(name = "Proportion of eggs", low = "#000033", high = "#FFFF33")
saeat
sand.heat <- veat + theme(strip.placement = "outside",plot.title = element_text(hjust = 0.5), axis.title.y = element_blank(), strip.background = element_rect(fill = "#EEEEEE", color = "#FFFFFF")) + ggtitle(label = "Sand treatment") + scale_y_discrete(breaks=c("T1", "T2", "T3", "T4","T5", "T6", "T7", "T8", "T9", "T10"))
swallows.heat
我找不到以这种方式对我的 y 轴进行排序的方法:T1 到 T10。但是 T10 在 T1 之后结束。关于如何更改此设置的任何建议?
我不确定是否以相同的方式使用 character
变量,但如果您使用 factor
变量,您可以按照您想要的方式重新排序级别。
# make a factor variable
heat$trap <- as.factor(heat$trap)
# check the current levels
levels(heat$trap)
[1] "T1" "T10" "T2" "T3" "T4" "T5" "T6" "T7" "T8" "T9"
# reorder the levels
heat$trap <- factor(heat$trap, levels = levels(heat$trap)[c(1, 3:10, 2)])
# check the correct order
levels(heat$trap)
[1] "T1" "T2" "T3" "T4" "T5" "T6" "T7" "T8" "T9" "T10"
这是我目前拥有的:
heat=read.csv("heat.csv")
head(heat)
heat$trap <- as.character(heat$trap)
class(heat$trap)
levels(heat$trap) <- c("T1", "T2", "T3", "T4","T5", "T6", "T7", "T8", "T9", "T10")
saeat <- ggplot(data = heat, mapping = aes(x = trip, y = trap, fill = sand)) + geom_tile() + ylab(label = "Trap") + xlab(label = "Month") + scale_fill_gradient(name = "Proportion of eggs", low = "#000033", high = "#FFFF33")
saeat
sand.heat <- veat + theme(strip.placement = "outside",plot.title = element_text(hjust = 0.5), axis.title.y = element_blank(), strip.background = element_rect(fill = "#EEEEEE", color = "#FFFFFF")) + ggtitle(label = "Sand treatment") + scale_y_discrete(breaks=c("T1", "T2", "T3", "T4","T5", "T6", "T7", "T8", "T9", "T10"))
swallows.heat
我找不到以这种方式对我的 y 轴进行排序的方法:T1 到 T10。但是 T10 在 T1 之后结束。关于如何更改此设置的任何建议?
我不确定是否以相同的方式使用 character
变量,但如果您使用 factor
变量,您可以按照您想要的方式重新排序级别。
# make a factor variable
heat$trap <- as.factor(heat$trap)
# check the current levels
levels(heat$trap)
[1] "T1" "T10" "T2" "T3" "T4" "T5" "T6" "T7" "T8" "T9"
# reorder the levels
heat$trap <- factor(heat$trap, levels = levels(heat$trap)[c(1, 3:10, 2)])
# check the correct order
levels(heat$trap)
[1] "T1" "T2" "T3" "T4" "T5" "T6" "T7" "T8" "T9" "T10"