![](/img/trans.png)
[英]Plotting histograms for multiple datasets with percentages with ggplot2
[英]ggplot2: plotting multiple histograms in the same page, but one with inverted coordinates
我想從相同的數據中繪制兩個或更多個直方圖,但是反轉一個組的坐標,如下所示:
(注意:這個數字取自Grossman等2011,多元信號的復合物區分正選擇區域中的因果變異 )
例如,讓我們從ggplot2獲取鑽石數據集:
> library(ggplot2)
> head(diamonds)
carat cut color clarity depth table price x y z
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
我嘗試過的一種方法是使用stat_bin計算直方圖而不繪制它,更改它返回的計數列的值。
> my_sb <-stat_bin(data=diamonds, mapping=aes(x=x)
stat_bin的文檔說該函數應返回一個等於映射的data.frame,但添加四個新列(count,density,ncount,ndensity)。 但是,我無法在任何地方找到這些列:
# I supposed that this should contain a count, density columns, but it does not.
> print(head(my_sb$data))
carat cut color clarity depth table price x y z
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
另一種可能的方法是使用scale_y_reverse(),但我不知道如何將它應用於單個數據集。
我能想到的第三種方法是使用viewPorts,但我不太清楚如何實現它。
也許這樣:
ggplot(data = diamonds) +
geom_histogram(aes(x = x,y = ..count..)) +
geom_histogram(aes(x = x,y = -..count..))
僅供參考 - 我不記得過去我是怎么做到的,所以我用Google搜索了“ggplot2倒排直方圖”並點擊了第一個點擊 ,一個StackOverflow問題。
我不確定stat_bin
返回的proto對象究竟是如何構造的,但是新變量就在那里。 它的工作方式是geom_histogram
本身調用stat_bin
來執行binning,因此它可以訪問計算變量,我們可以將其映射到y
變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.