2014-09-22 95 views
0

您好我woulds想知道是否有一個簡單的方法來提取圖形Y軸的數值,比如我有GGPLOT2提取Y軸值

set.seed(20) 
GG <- ggplot(data = data.frame(y=rnorm(20),x=1:20), aes(x=x,y=y))+geom_line() 

enter image description here

我想獲得向量: c(「 - 3」,「 - 2」,「 - 1」,「0」,「1」,「2」),可能有簡單的函數從數據中的所有值生成它們?或者,也許我可以從GG中提取它們?

編輯

data <- data.frame(y=c(4.99,99.20554),x=c(1,2)) 
ggplot(data=data,aes(x=x,y=y))+geom_line() 
pretty(data$y) 
[1] 0 20 40 60 80 100 

enter image description here

功能pretty()給人不錯,但不一樣的結果。

回答

2

當顯示GG對象時,會計算軸標籤位置和文本。您可以build對象並檢查:

> build = ggplot_build(GG) 
> build$panel$ranges[[1]]$y.labels 
[1] "-3" "-2" "-1" "0" "1" "2" 
> build$panel$ranges[[1]]$y.major_source 
[1] -3 -2 -1 0 1 2 

現在我懷疑這些東西都是非常內部,可能會改變並與ggplot2新版本打破你的代碼。

+1

不會有ggplot2的新版本 – baptiste 2014-09-22 10:28:27

+2

'ggplot3'然後... – Spacedman 2014-09-22 10:34:45