gpt4 book ai didi

r - 订购 geom_tile 内的数据

转载 作者:行者123 更新时间:2023-12-04 00:06:13 28 4
gpt4 key购买 nike

我有一个数据框,我想生成一个 geom_tile()从中绘制,但我希望图形的排序不是基于字母顺序而是基于此数据框中的变量。

structure(list(V1 = c("a", "y", "w", "p", "v", "h", "i"), 
V2 = c("r", "w", "q", "m", "l", "q", "g"), V3 = c(
"5", "2", "9", "2", "1", "3", "0")), .Names = c("V1", "V2",
"V3"), class = "data.frame", row.names = c(NA, -8L))

我想根据变量 V3 对绘图进行排序,因为正常绘图会根据 V1 中的字母顺序对它们进行排序。和 V2 .

这怎么办??

最佳答案

我通常会尝试使用级别来预先修复我的数据:

x <- structure(list(V1 = c("a", "y", "w", "p", "v", "h", "i"), 
V2 = c("r", "w", "q", "m", "l", "q", "g"), V3 = c(
"5", "2", "9", "2", "1", "3", "0")), .Names = c("V1", "V2",
"V3"), class = "data.frame", row.names = c(NA, -8L))

x <- x[1:7,]

x$V1 <- factor(x$V1, levels=(x$V1)[order(x$V3)])

# Note it's not an ordered factor, it's a factor in the right order

ggplot(x, aes(V1, V3)) + geom_tile()

enter image description here

更新:

仍然不完全清楚您的尺寸,但可能类似于:
x$V2 <- factor(x$V2, levels=(x$V2)[order(x$V3)])

ggplot(x, aes(V1,V2,fill=V3)) + geom_tile()

enter image description here

关于r - 订购 geom_tile 内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713713/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com