0

I want to create plots ('A_plot' and 'B_plot'), and stored in separately object/variables. Below code failed , anyone can help on this ? Thanks!

library(tidyverse)

test_data <- data.frame(
  category=c('A','A','A','B','B','B'),
  price=c(1,1,2,3,2,2),
  amount=c(1:6)
  )

for (i in unique(test_data$category)){
   paste0(i,'_plot') <-  test_data %>% filter(category== i ) %>% 
    ggplot(aes(x=price,y=amount))+geom_point()+
    labs(title=paste0(i,'_plot'))
}
anderwyang
  • 1,801
  • 4
  • 18
  • Why do you want to do that? Creating a bunch of variables with data in their name just makes things harder to work with down steam. It would be easier to keep things in a list. Then you can use mapping or apply functions to easily create and manipulate those values. You can't assign to the result of `paste()`. You could use `assign()` but that just usually leads to more downstream problems. – MrFlick Nov 04 '21 at 02:22
  • Thanks for your advice , the 'assing' can do it , but i will try to stored them in a list – anderwyang Nov 04 '21 at 03:14

0 Answers0