Bjc51192
Bjc51192

Reputation: 327

Dynamically Assign character output of a variable to color

Very confused on why the following does not work.

I have a variable State. That dynamically changes based on an output of a dropdown.

For example, State="Alabama"

I am assigning the legend with the following colors:

 cols= c(State = "black", "All States" = "red") 

Which ultimately gets passed to scale_fill_manual

The above produces the correct Legend label for Alabama, however shows no bar/color in the legend.

If I instead do the following:

cols= c("Alabama" = "black", "All States" = "red") 

the legend is correct.

I have tried

paste0(State) = "black", as.character(State) = "black" 

to no avail.

Where am I going wrong?

Upvotes: 1

Views: 127

Answers (1)

akrun
akrun

Reputation: 887911

We may use setNames to create a named vector

State <- "Alabama"
cols = setNames(c("black", "red"), c(State, "All States")) 

Upvotes: 1

Related Questions