2016-12-02 102 views
0

我想繪製熱圖,但x(和y)軸上的單位大小應該有所不同。下面的示例代碼:ggplot 2熱圖與變量軸

users = rep(1:3,3) 
Inst = c(rep("A",3),rep("B",3),rep("C",3)) 
dens = rnorm(9) 
n_inst = c(3,3,3,2,2,2,1,1,1) 
df <- data.frame(users, Inst, dens, n_inst) 

1  1 A 1.2521487  3 
2  2 A -0.1013088  3 
3  3 A 1.5770535  3 
4  1 B 1.1093957  2 
5  2 B 1.1059166  2 
6  3 B 0.6884662  2 
7  1 C -0.3864710  1 
8  2 C -1.0216373  1 
9  3 C 0.4500778  1 

z <- ggplot(df, aes(Inst, users)) + geom_tile(aes(fill = dens)) 
z + scale_x_discrete(breaks = n_inst) 

enter image description here 所以由此得出一個熱圖,但Inst所有單元具有相同的尺寸。我想要A的寬度爲CB的寬度的兩倍寬度C。所以我想n_inst給單位的寬度。

我試過scale_discret,但這並不能做到這一點

預先感謝您。

回答

1

你可以試試這個:

ggplot(df, aes(Inst, users)) + geom_tile(aes(fill = dens, width=n_inst)) 

enter image description here

+0

好的,謝謝。什麼是y軸的simular命令? –

+0

它的高度而不是寬度 –