在geom_tile中订购数据 [英] Order data inside a geom_tile
问题描述
我有一个数据框,我想从它生成一个 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
。
我通常会尝试使用关卡来修复我的数据:
$ (列表(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 =data.frame,row.names = c(NA,-8L))
x < - x [1:7,]
x $ V1 < - 因子(x $ V1,levels =(x $ V1)[order(x $ V3)])
#注意这不是一个有序的因子,它是正确顺序的一个因子
ggplot(x,aes(V1,V3))+ geom_tile()
更新:
仍然不完全清楚您的尺寸,但可能是这样的:
x $ V2< ; - 因子(x $ V2,levels =(x $ V2)[order(x $ V3)])
ggplot(x,aes(V1,V2,fill = V3))+ geom_tile )
i have a data frame and i want to generate a geom_tile()
plot from it , but I want the graph to be ordered not based on the alphabetic but based on a variable inside this data frame.
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))
I want to order the plot based in the variable V3
, because the normal plotting will order them based on the alphabetic in V1
and V2
.
How this can be done ??
I usually try to use levels to fix my data before hand:
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()
UPDATE:
Still not exactly clear on your dimensions, but perhaps something like:
x$V2 <- factor(x$V2, levels=(x$V2)[order(x$V3)])
ggplot(x, aes(V1,V2,fill=V3)) + geom_tile()
这篇关于在geom_tile中订购数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!