0

I have a dataframe like this:

library(tidyverse)

my_data <- tibble(name = c("Justin", "Janet", "Marisa"),
                  x = c(100, 50, 75),
                  y = c(2, 3, 6))

Each name is unique, and I want to make a bar graph for each person without having to do it line by line. I also want to save each plot as a unique object because I'll be inputting it into a power point using the officer package. Last, the names won't always be the same, but each name will always be unique.

For instance, I want one plot for Janet, one plot for Justin, and one plot for Marisa. I don't want them faceted but instead as their own objects.

Any thoughts?

1 Answer 1

1

We can get the data in long format first and for each individual name create the plot.

library(tidyverse)
long_data <- my_data %>% tidyr::pivot_longer(cols = -name, names_to = 'col')

plots_list <- map(unique(my_data$name), ~long_data %>%
                   filter(name == .x) %>%
                   ggplot() + aes(name, value, fill = col) + 
                   geom_bar(stat = 'identity', position = 'dodge') + 
                   scale_fill_manual(values = c('red', 'blue')) + 
                   ggtitle(paste0('Plot for ', .x)))

This will return list of plots where individual plots can be accessed via plots_list[[1]], plots_list[[2]] etc.

plots_list[[1]]

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

sorry I should have been clearer--I want 3 plots. One for Janet, one for just, one for marisa
So same individual bar graphs for each? Or do you want to have facets?
not facets--the same bar graph for each individual, ideally each as their own object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.