jakes
jakes

Reputation: 2085

How to use concatenated strings as a name in c() function

Is there a way to use concatenated strings as element names in a vector? I've got a text1 variable which contains a column name, but at the later stage I create another columns with some suffices added to name. After gathering the data those newly created name become a factor variable key which I would like to use in scale_fill_manual and assign colors to factor levels. But I can't figure out how this can be passed as a vector names. A code is shown below:

text1 <- 'name'
ggplot(data) + 
  geom_area(aes(x,y, fill = key)) + 
  scale_fill_manual(values = c(paste0(text1, '_suffix') = 'blue', 
                               paste0(text1, '_suffix2') = 'red)

Any ideas?

Upvotes: 0

Views: 50

Answers (1)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

Seems like you are looking for setNames

text1 <- "what"
setNames(c("blue", "red"), paste0(text1, c('_suffix', '_suffix2')))

 what_suffix what_suffix2
      "blue"        "red"

Upvotes: 1

Related Questions