GGPlot 翻转此图
GGPlot Flip This Plot
library(ggplot2)
library(reshape2)
HAVE = data.frame(VARS = c("cat", "dog", "fox", "rabbit"),
"a" = c(1,2,3,4),
"b" = c(2,3,1,4),
"c" = c(3,1,2,4))
HAVE1 = melt(HAVE, id.vars="VARS")
ggplot(HAVE1, aes(VARS, variable, fill= value)) +
geom_tile() +
scale_fill_gradient(low="white", high="blue")
我想制作一张从黄色(低值)到蓝色(高值)的彩色图。我希望顶部的列是 'variable',yticks 是 VARS,例如:请注意这只是一个例子!!
这是 pivot_longer
和 scale_x_discrete
的方法。请注意,tidyr
是 reshape2
的后继者,但作者相同。
我们可以重新排序 VARS
的因子水平,让它们按照您想要的顺序绘制。
我们也可以用 labs
和 element_blank
去掉轴标签。
library(ggplot2)
library(tidyr)
library(dplyr)
HAVE %>%
pivot_longer(-VARS,names_to = "variable", values_to = "value") %>%
ggplot(aes(x = variable, y = factor(VARS, rev(levels(VARS))), fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
scale_x_discrete(position = "top") +
labs(x = element_blank(), y = element_blank())
如果你想要方块是正方形的,只需添加coord_equal()
函数:
ggplot(HAVE1,aes(x = variable, y = factor(VARS, rev(levels(VARS))), fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
scale_x_discrete(position = "top") +
labs(x = element_blank(), y = element_blank()) +
coord_equal()
这里有一个选项 coord_flip
可以切换轴并使用 fct_rev
对因子水平重新排序
ggplot(HAVE1, aes(x = fct_rev(as_factor(VARS)), variable, fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
coord_flip()
library(ggplot2)
library(reshape2)
HAVE = data.frame(VARS = c("cat", "dog", "fox", "rabbit"),
"a" = c(1,2,3,4),
"b" = c(2,3,1,4),
"c" = c(3,1,2,4))
HAVE1 = melt(HAVE, id.vars="VARS")
ggplot(HAVE1, aes(VARS, variable, fill= value)) +
geom_tile() +
scale_fill_gradient(low="white", high="blue")
我想制作一张从黄色(低值)到蓝色(高值)的彩色图。我希望顶部的列是 'variable',yticks 是 VARS,例如:请注意这只是一个例子!!
这是 pivot_longer
和 scale_x_discrete
的方法。请注意,tidyr
是 reshape2
的后继者,但作者相同。
我们可以重新排序 VARS
的因子水平,让它们按照您想要的顺序绘制。
我们也可以用 labs
和 element_blank
去掉轴标签。
library(ggplot2)
library(tidyr)
library(dplyr)
HAVE %>%
pivot_longer(-VARS,names_to = "variable", values_to = "value") %>%
ggplot(aes(x = variable, y = factor(VARS, rev(levels(VARS))), fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
scale_x_discrete(position = "top") +
labs(x = element_blank(), y = element_blank())
如果你想要方块是正方形的,只需添加coord_equal()
函数:
ggplot(HAVE1,aes(x = variable, y = factor(VARS, rev(levels(VARS))), fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
scale_x_discrete(position = "top") +
labs(x = element_blank(), y = element_blank()) +
coord_equal()
这里有一个选项 coord_flip
可以切换轴并使用 fct_rev
ggplot(HAVE1, aes(x = fct_rev(as_factor(VARS)), variable, fill= value)) +
geom_tile() +
scale_fill_gradient(low="yellow", high="blue") +
coord_flip()