Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

r - Order data inside a 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 ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

enter image description here

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() 

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...