Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...