2012-08-17 164 views
5

是否可以使用ggplot2包中的geom_ribbon,但是範圍是水平繪製的而不是垂直繪製的。如果「geom_ribbon」提出選項xmin/xmax,這將是微不足道的,但事實並非如此。使用ggplot2中的geom_ribbon反轉X和Y軸

這裏是我的數據,用於測試的簡化繪圖函數和我獲得的數字。所以在這個例子中,我想讓「深度」軸垂直顯示。

prof <- read.table(text = " depth prop mean error 
1 5 FALSE 2.12 0.035 
2 15 FALSE 2.06 0.035 
3 25 FALSE 1.54 0.035 
4 35 FALSE 0.92 0.033 
5 45 FALSE 0.71 0.028 
6 55 FALSE 0.60 0.026 
7 65 FALSE 0.54 0.024 
8 75 FALSE 0.49 0.023 
9 85 FALSE 0.46 0.022 
10 95 FALSE 0.43 0.022 
11 105 FALSE 0.41 0.022 
12 115 FALSE 0.39 0.021 
13 5 TRUE 2.18 0.061 
14 15 TRUE 2.11 0.061 
15 25 TRUE 1.58 0.054 
16 35 TRUE 0.97 0.047 
17 45 TRUE 0.76 0.042 
18 55 TRUE 0.67 0.043 
19 65 TRUE 0.61 0.040 
20 75 TRUE 0.54 0.033 
21 85 TRUE 0.48 0.028 
22 95 TRUE 0.45 0.027 
23 105 TRUE 0.43 0.025 
24 115 TRUE 0.40 0.022", header=TRUE) 

ggplot(prof, aes(colour = prop, fill = prop)) + 
    geom_ribbon(aes(x = depth, ymin = mean-error, ymax = mean+error), 
    alpha = 0.6, colour = NA) 

enter image description here

回答

7

已經看清楚coord_flip()這是未經檢驗的,因爲你沒有在一個簡單的格式提供數據。發佈dput(your.data.frame)的輸出以提供數據。

ggplot(prof, aes(x = depth, ymin = mean - error, ymax = mean + error, 
    fill = prop)) + geom_ribbon() + coord_flip() 

andpleaseusethelargebaratthebottomofyourkeyboardwhentypingcodewhitespacemakestextmuchmorereadablecodeisalsotext

+2

+1尤其是對最後一段(雖然你也應該得到它,因爲你的答案的作品)。 – A5C1D2H2I1M1N2O1R2T1 2012-08-17 09:23:01

+0

謝謝,這是我需要的,以及scale_x_reverse() – fstevens 2012-08-17 09:26:49