2016-12-06 225 views
1

我正在嘗試關注區域圖上的this ggplot2教程(非常不幸,它沒有評論來問我的問題),但出於某種原因,我的輸出與作者的輸出不同。我執行下面的代碼:ggplot2中的反向堆疊順序geom_order

library(ggplot2) 
charts.data <- read.csv("copper-data-for-tutorial.csv") 

p1 <- ggplot() + geom_area(aes(y = export, x = year, fill = product), data = charts.data, stat="identity") 

dataset如下:

> charts.data 
    product year export percentage sum 
1 copper 2006 4176   79 5255 
2 copper 2007 8560   81 10505 
3 copper 2008 6473   76 8519 
4 copper 2009 10465   80 13027 
5 copper 2010 14977   86 17325 
6 copper 2011 15421   83 18629 
7 copper 2012 14805   82 18079 
8 copper 2013 15183   80 19088 
9 copper 2014 14012   76 18437 
10 others 2006 1079   21 5255 
11 others 2007 1945   19 10505 
12 others 2008 2046   24 8519 
13 others 2009 2562   20 13027 
14 others 2010 2348   14 17325 
15 others 2011 3208   17 18629 
16 others 2012 3274   18 18079 
17 others 2013 3905   20 19088 
18 others 2014 4425   24 18437 

當我打印的情節我的結果是:

enter image description here

相反,同樣的代碼完全在教程中顯示了一個反轉順序的圖,看起來好多了,因爲較小的數量在底部:

enter image description here

我懷疑作者要麼被遺漏的一些代碼或輸出是不同的,因爲我們使用不同版本的GGPLOT2。如何更改堆疊順序以獲得相同的輸出?

sessionInfo()

> sessionInfo() 
R version 3.3.2 (2016-10-31) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] plyr_1.8.4  extrafont_0.17 ggthemes_3.3.0 ggplot2_2.2.0 

loaded via a namespace (and not attached): 
[1] Rcpp_0.12.8  digest_0.6.10 assertthat_0.1 grid_3.3.2  Rttf2pt1_1.3.4 gtable_0.2.0  scales_0.4.1  
[8] lazyeval_0.2.0 extrafontdb_1.0 labeling_0.3  tools_3.3.2  munsell_0.4.3 colorspace_1.3-1 tibble_1.2 

回答

2

你這裏有兩種選擇,兩者都需要你aes(fill)是一個factor

1)更改factor所以所需添加劑level的順序是第一:

df$product %<>% factor(levels= c("others","copper")) 

ggplot(data = df, aes(x = year, y = export)) + 
    geom_area(aes(fill = product), position = "stack") 

enter image description here

2)或保留因子作爲是(銅第一)和告訴position_stack(reverse = TRUE)

df$product %<>% as.factor() 
ggplot(data = df, aes(x = year, y = export)) + 
    geom_area(aes(fill = product), position = position_stack(reverse = T)) 

enter image description here

%<>%library(magrittr)

+0

非常感謝,這兩種解決方案工作的偉大。對於第一個解決方案,我首先必須使用'charts.data < - as.data.frame(charts.data)'將我的數據集轉換爲數據框。 此外,對於我的表達式'charts.data $ product%<>%因子(levels = c(「others」,「copper」))'throws'錯誤:找不到函數「%<>%」'。 什麼工作是'charts.data $ product < - factor(charts.data $ product,levels = c(「others」,「copper」))' – Vasilis

+0

剛剛在'%<>%'上看到了您的編輯,我沒有不知道這個圖書館,會檢查出來。再次感謝 – Vasilis

+0

不用擔心,樂意幫忙。 '%<>%'可以節省很多多餘的輸入,強烈建議 – Nate

1

我看到了您的電子郵件。我是該教程的作者之一。非常感謝詢問我的材料。

這是根據您的文章有關塊

p2 <- ggplot() + geom_area(aes(y = export, x = year, fill = product), 
          data = charts.data, stat="identity") 
p2 

請考慮該教程是在不同版本的GGPLOT2作出我們更新這些教程和那些沒有在一個坐寫的。

該教程來自一個Rmd文件,它顯示了一個圖表代碼行狀態。

你可以在https://leanpub.com/上看到我的老教程的演變形式,因爲它們演變成了一本書的標題爲「R的Ggplot2的Hitchhiker指南」的書的形狀。我告訴你,因爲我們更新了書的某些部分查看ggplot2 2.2.0是否正在生成我們正在使用舊版ggplot2版本所做的工作。

Nathan Day說什麼是有用的,但我檢查了兩次自己(剛纔),並獲得了與本教程中相同的情節。

這是我sessionInfo()

> sessionInfo() 
R version 3.3.1 (2016-06-21) 
Platform: x86_64-apple-darwin15.6.0 (64-bit) 
Running under: OS X 10.11.6 (El Capitan) 

locale: 
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] ggplot2_2.2.0 

loaded via a namespace (and not attached): 
[1] colorspace_1.2-7 scales_0.4.1  assertthat_0.1 lazyeval_0.2.0 plyr_1.8.4  
[6] tools_3.3.1  gtable_0.2.0  tibble_1.2  Rcpp_0.12.7  grid_3.3.1  
[11] munsell_0.4.3 

問候

+0

感謝您的回覆,我非常喜歡您的教程。我編輯我的問題,包括我的'sessionInfo()' – Vasilis

+0

謝謝@Vasilis。我也回答了你的GitHub問題。 – pachamaltese

1

@Vasilis

這個怎麼樣完全重複的例子?在發佈一些令人尷尬的解決方案之前,我確實想過一點點。

library(ggplot2) 
library(ggthemes) 
library(extrafont) 
library(forcats) 

charts.data.2 <- read.csv("copper-data-for-book.csv") 
charts.data.2 <- as.data.frame(charts.data.2) 
charts.data.2$product <- factor(charts.data.2$product, levels = c("others","copper"), 
    labels = c("Pulp wood, Fruit, Salmon & Others ","Copper")) 

fill <- c("#b2d183","#40b8d0") 

p2 <- ggplot() + 
    geom_area(aes(y = export, x = year, fill = product), data = charts.data.2, 
    stat="identity") + 
    scale_x_continuous(breaks=seq(2006,2014,1)) + 
    labs(x="Year", y="USD million") + 
    ggtitle("Composition of Exports to China ($)") + 
    scale_fill_manual(values=fill) + 
    theme(panel.border = element_rect(colour = "black", fill=NA, size=.5), 
    axis.text.x=element_text(colour="black", size = 10), 
    axis.text.y=element_text(colour="black", size = 10), 
    legend.key=element_rect(fill="white", colour="white"), 
    legend.position="bottom", legend.direction="horizontal", 
    legend.title = element_blank(), 
    panel.grid.major = element_line(colour = "#d3d3d3"), 
    panel.grid.minor = element_blank(), 
    panel.background = element_blank(), 
    plot.title = element_text(size = 14, family = "Tahoma", face = "bold"), 
    text=element_text(family="Tahoma")) + 
    guides(fill = guide_legend(reverse=T)) 
p2 

enter image description here

+0

謝謝,它效果很好! – Vasilis