beta
beta

Reputation: 5686

dynamic labels (character vetor) for plot in R

I feel pretty dumb for this question, but can't get my head around it: I craete a boxplot in R. Sometimes I have 5 underlying dataframes, sometimes more or less. Each dataframe results in a boxplot. So each boxplot should have a label in the form of Label 1, Label 2, Label 3, etc.

Since I only know the number of boxplots during runtime, I obviously cannot hardcode the labels as below:

boxplot(boxList, names=c('Label 1', 'Label 2')

So how can I dynamically create a character vector given a variable count indicating the number of boxplots.

For instance count = 3. Result should be: c('Label 1', 'Label 2', 'Label 3')

Upvotes: 0

Views: 578

Answers (1)

Pafnucy
Pafnucy

Reputation: 628

paste function will do, as it is vectorized

> count <- 3
> paste('Label', 1:count)

Output:

[1] "Label 1" "Label 2" "Label 3"

'Label' is pasted with every element of 1:count vector, separated by a space character (default for sep argument).

Upvotes: 2

Related Questions