Pigeon
Pigeon

Reputation: 423

ggplot2 extracting y axis values

Hi I woulds like to know is there an easy way to extract y axis values from graph, for example I have

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

I would like to get vector: c("-3","-2","-1","0","1","2"), maybe there is simple function which generates them from all values in data? Or maybe I can extract them from GG ?

Edit

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

function pretty() gives nice, but not the same results.

Upvotes: 0

Views: 2245

Answers (1)

Spacedman
Spacedman

Reputation: 94202

The axis label positions and texts are computed when the GG object is displayed. You can build the object and inspect it:

> 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

Now I would suspect these things are very internal and possibly might change and break your code with a new version of ggplot2.

Upvotes: 3

Related Questions