2016-08-11 65 views
0

我有這樣如何使混凝土熱圖值

df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L, 
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All", 
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe", 
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"), 
    value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022, 
    -1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646, 
    1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA, 
-16L)) 

數據看起來像下面

#  name value 
#1  Bab 1.251 
#2 Kiss -1.018 
#3  All -1.074 
#4 where -1.137 
#5  No 1.018 
#6  ist 1.293 
#7 Howe 1.022 
#8 charli -1.008 
#9  TT 1.022 
#10 bra 1.252 
#11 hora -1.005 
#12 myr 1.694 
#13 few -1.068 
#14 delta 1.396 
#15 boro 1.646 
#16 kind 1.016 

當我繪製它

ggplot(df, aes(x = 1,y = name, fill = value)) + 
    geom_tile() + 
    ylab("") 

它繪製它隨機

enter image description here

但我想要具有與我在數據中一樣的順序。我也想讓ylim更小,但我做不到。我很欣賞任何建議

回答

1

問題是你的名字因素有它的水平sortet 按字母順序。你想要做的是重新排列name的等級。在繪圖之前執行此操作:

df$name <- factor(df$name, levels = df$name) 

這將從底部到頂部按原樣打印值。 對於頂部到底部的順序,使用

df$name <- factor(df$name, levels = rev(df$name)) 

最後,在設置「ylim」並沒有真正意義在這裏。您可能想要減小畫布的高度(例如,通過更改RStudio中的「圖」窗格的大小)。

+0

這裏不需要'ordered ='。這會創建一個「有序因子」,就像一個有序變量。只是設置的水平是最好的。 – MrFlick

+0

@MrFlick這不起作用。我建議你自己嘗試一下。有序確實需要停止ggplot排序水平。 – AlexR

+0

我確實嘗試過。 'df $ name < - factor(df $ name,levels = rev(df $ name)); ggplot(df,aes(x = 1,y = name,fill = value))+ geom_tile()+ ylab(「」)'不需要'ordered ='參數。 – MrFlick