Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 return values

Tags:

r

ggplot2

The documentation for ggplot2's stat_bin function states that it returns a new data frame with additional columns. How does one actually get access to this data frame?

Is it possible?

simple <- data.frame(x = rep(1:10, each = 2))
tmp <- stat_bin(data=simple, binwidth=0.1, aes(x))

I have figured out that tmp is an environment, and ls(tmp) will show what objects are in the environment, but after exploring each of these objects, I'm not seeing anything like what is described as a return value.

like image 592
rmflight Avatar asked Sep 04 '12 16:09

rmflight


People also ask

What does GG in ggplot represent?

The ggplot2 package is a relatively novel approach to generating highly informative publication-quality graphics. The “gg” stands for “Grammar of Graphics”.

What is the difference between ggplot and ggplot2?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

What are Geoms in R?

Geoms. A layer combines data, aesthetic mapping, a geom (geometric object), a stat (statistical transformation), and a position adjustment. Typically, you will create layers using a geom_ function, overriding the default position and stat if needed.

What is AES ggplot?

Aesthetic Mapping ( aes ) In ggplot2 , aesthetic means “something you can see”. Each aesthetic is a mapping between a visual cue and a variable. Examples include: position (i.e., on the x and y axes) color (“outside” color)


2 Answers

NOTE: With the current (as of 2022-05-24) version of ggplot2 (3.3.6), the approach described below no longer works. In place of print(tmp) , you should now use ggplot_build(tmp), as noted in @showteth's now-accepted answer.


As Luciano Selzer mentions, the calculations that produce the table shown below aren't performed until print time. (A look at ggplot2:::print.ggplot() will show that, in its final line, it returns the table invisibly, so it can be captured by assignment for further examination.)

tmp <- ggplot(data=simple) + stat_bin(aes(x), binwidth=0.1)
x <- print(tmp)
head(x[["data"]][[1]])
#   y count    x ndensity ncount density PANEL group ymin ymax xmin xmax
# 1 0     0 0.95        0      0       0     1     1    0    0  0.9  1.0
# 2 2     2 1.05        1      1       1     1     1    0    2  1.0  1.1
# 3 0     0 1.15        0      0       0     1     1    0    0  1.1  1.2
# 4 0     0 1.25        0      0       0     1     1    0    0  1.2  1.3
# 5 0     0 1.35        0      0       0     1     1    0    0  1.3  1.4
# 6 0     0 1.45        0      0       0     1     1    0    0  1.4  1.5
like image 53
Josh O'Brien Avatar answered Sep 22 '22 20:09

Josh O'Brien


In ggplot2 (3.3.5), I can not get access to the returned data frame with ggplot2:::print.ggplot(), but I get success with ggplot_build:

simple <- data.frame(x = rep(1:10, each = 2))
tmp <- ggplot(data=simple) + stat_bin(aes(x), binwidth=0.1)
# with ggplot_build
x <- ggplot_build(tmp)
head(x[["data"]][[1]])
#   y count   x xmin xmax density ncount ndensity flipped_aes PANEL group ymin ymax colour   fill size linetype alpha
# 1 2     2 1.0 0.95 1.05       1      1        1       FALSE     1    -1    0    2     NA grey35  0.5        1    NA
# 2 0     0 1.1 1.05 1.15       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 3 0     0 1.2 1.15 1.25       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 4 0     0 1.3 1.25 1.35       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 5 0     0 1.4 1.35 1.45       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 6 0     0 1.5 1.45 1.55       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
like image 36
showteth Avatar answered Sep 24 '22 20:09

showteth