How do I access the data frame that has been passed to ggplot()?
mtcars %>% {
ggplot(., aes(mpg, hp)) +
labs(title = paste("N =", nrow(.))) +
geom_point()
}
Note that when wrapping the whole ggplot
call in {...}
curly braces, you must use the .
dot pronoun for the data argument in ggplot(., ...)
. Then you can call back that object using the .
pronoun anywhere in the call.
How to extract summary() to a data frame applicable for data visualization in ggplot()?
You can access the resulting list up to the level where the table for the plot is found and save it as a data.frame. You can check the structure of the list by using str
.
summ_list <- summary(fit.aj,conf.int=FALSE,newdata=data.frame(age=50,sex="Male"),cause=1)
df<-as.data.frame(summ_list $table$`1`$`age=50, sex=Male`)
#The desired plot with ggplot
ggplot(df, aes(x=time)) +
geom_line(aes(y=cuminc)) +
geom_ribbon(aes(ymin = lower,
ymax = upper))
ggplot2, how do you use access dataframe elements from within the function?
The trick here is to use aes_string
instead of using aes
. The former allows you to pass strings as aesthetics. This allows you to pass a variable containing the string of the aesthetic, e.g. 'wt' in case of plotting mtcars. You need to modify your code like this:
library(ggplot2)
test <- function(fdata, fx) {
p <- ggplot(fdata, aes_string(x = fx, y = "mpg")) + geom_point()
print(p)
}
test(mtcars, "wt")
Extract data from a ggplot
To get values actually plotted you can use function ggplot_build()
where argument is your plot.
p <- ggplot(mtcars,aes(mpg))+geom_histogram()+
facet_wrap(~cyl)+geom_vline(data=data.frame(x=c(20,30)),aes(xintercept=x))
pg <- ggplot_build(p)
This will make list and one of sublists is named data
. This sublist contains dataframe with values used in plot, for example, for histrogramm it contains y
values (the same as count
). If you use facets then column PANEL
shows in which facet values are used. If there are more than one geom_
in your plot then data will contains dataframes for each - in my example there is one dataframe for histogramm and another for vlines.
head(pg$data[[1]])
y count x ndensity ncount density PANEL group ymin ymax
1 0 0 9.791667 0 0 0 1 1 0 0
2 0 0 10.575000 0 0 0 1 1 0 0
3 0 0 11.358333 0 0 0 1 1 0 0
4 0 0 12.141667 0 0 0 1 1 0 0
5 0 0 12.925000 0 0 0 1 1 0 0
6 0 0 13.708333 0 0 0 1 1 0 0
xmin xmax
1 9.40000 10.18333
2 10.18333 10.96667
3 10.96667 11.75000
4 11.75000 12.53333
5 12.53333 13.31667
6 13.31667 14.10000
head(pg$data[[2]])
xintercept PANEL group xend x
1 20 1 1 20 20
2 30 1 1 30 30
3 20 2 2 20 20
4 30 2 2 30 30
5 20 3 3 20 20
6 30 3 3 30 30
Use Name of Data Frame Passed to Function as Plot Title
You have not given a minimal example to show the problem. The following works for me:
a <- expand.grid(x=1:3, y=1:2)
f <- function(df){qplot(x, y, data=a, main=deparse(substitute(df)))}
f(a)
Were you doing something else?
Related Topics
R: How to Get the Last Element from Each Group
R: Expand and Fill Data Frame by Date in Series
Lookup Values Corresponding to the Closest Date
Subset Dataframe Such That All Values in Each Row Are Less Than a Certain Value
Referring to Variables by Name in a Dplyr Function Returns Object Not Found Error
How to Compare a Value in a Column to the Previous One Using R
Multiply Permutations of Two Vectors in R
How Many Non-Na Values in Each Row for a Matrix
Clustering Algorithm for Obtaining Equal Sized Clusters
R "For Loop" Error Messages {}
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Really Fast Word Ngram Vectorization in R
Dplyr 'Rename' Standard Evaluation Function Not Working as Expected
Creating Tree Diagram for Showing Case Count Using R
R: Unexpected Results from P.Adjust (Fdr)
Raster Package Taking All Hard Drive
Select Unique Values with 'Select' Function in 'Dplyr' Library