Is there a way to save or export the ggplot data used for plotting? I do not mean the image itself but the information r stores in the global environment.
For example:
Data <- data.frame(
X = sample(1:10),
Y = sample(c("yes", "no"), 10, replace = TRUE))
p <- ggplot(data=Data, aes(x=Y, y=X)) +
geom_bar(stat="identity")
What I want is to export "p" as csv or txt. Is this possible? I tried "write.table(p)" but I get the error: "cannot coerce class "c("gg", "ggplot")" to a data.frame"

Datainstead?pis aggplotobject, not a data frame. If you want the data that was interpreted into geoms, usep$data, but this is likely the same asData. If you want the dataframe that has positions and other information used in assembling geoms, useggplot_build(p)$data