I am wondering if it is somehow possible to access the columns of the provided data within a ggplot2
graph for the title. So something like that:
gg
I would try the following as it is not possible to pipe outside aes()
.
ggplot(mpg %>% filter(manufacturer == 'audi'),
aes(x = hwy, y = displ, label = model)) +
geom_point() +
geom_text(data = . %>% filter(hwy > 28)) +
facet_wrap(~manufacturer)+
theme(strip.background = element_blank(),
strip.text = element_text(hjust = 0, size = 14))
The idea is to use a facet with empty strip background. If there are more names or variables one has to create an extra faceting variable using e.g. mutate(gr = "title")
mpg %>%
mutate(title="This is my plot") %>%
ggplot(aes(x = hwy, y = displ, col=manufacturer)) +
geom_point() +
facet_wrap(~title)+
theme(strip.background = element_blank(),
strip.text = element_text(hjust = 0, size = 14))
As you asked a second question here are two solutions for creating individual plots for each group
# first solution
p <- mpg %>%
group_by(manufacturer) %>%
do(plots= ggplot(., aes(cyl, displ)) +
geom_point() +
ggtitle(unique(.$manufacturer))
)
p %>% slice(1) %>% .$plots
# second solution
mpg %>%
nest(-manufacturer) %>%
mutate(plot = map2(data, manufacturer, ~ggplot(data=.x,aes(cyl, displ))+
geom_point() +
ggtitle(.y))) %>%
slice(1) %>% .$plot
Or save the data using
map2(paste0(p$manufacturer, ".pdf"), p$plots, ggsave)