David L
David L

Reputation: 47

Mismatch in legend/color in R plot

I created several plots in R. Occasionally, the program does not match the color of the variables in the plot to the variable colors in the legend. In the attached file (Unfortunately, I can't yet attach images b/c of reputation), the first 2 graphs are assigned a black/red color scheme. But, the third chart automatically uses a green/black and keeps the legend with black/red. I cannot understand why this happens.

How can I prevent this from happening? I know it's possible to assign color, but I am struggling to find a clear way to do this.

Code:

plot(rank, abundance, pch=16, col=type, cex=0.8)
legend(60,50,unique(type),col=1:length(type),pch=16)

plot(rank, abundance, pch=16, col=Origin, cex=0.8)
legend(60,50,unique(Origin),col=1:length(Origin),pch=16)


Below is where color pattern won't match

plot(rank, abundance, pch=16, col=Lifecycle, cex=0.8)
legend(60,50,unique(Lifecycle),col=1:length(Lifecycle),pch=16)

data frame looks like this:

Plant    rank   abundance  Lifecycle    Origin   type
X         1         23       Perennial   Native  Weedy
Y         2         10       Annual      Exotic  Ornamental
Z         3         9        Perennial   Native  Ornamental

Upvotes: 2

Views: 3625

Answers (1)

Tad Dallas
Tad Dallas

Reputation: 1189

First, I create some fake data.

 df <- data.frame(rank = 1:10, abundance = runif(10,10,100), 
       Lifecycle = sample(c('Perennial', 'Annual'), 10, replace=TRUE))

Then I explicitly say what colors I want my points to be.

cols=c('dodgerblue', 'plum')

Then I plot, using the factor df$Lifecycle to color points.

plot(df$rank, df$abundance, col = cols[df$Lifecycle], pch=16)

When the factor df$Lifecycle is used above, it converts it to a numeric reference to cols, such that it sorts the values alphabetically. Therefore, in the legend, we just need to sort the unique df$Lifecycle values, and then hand it our color vector (cols).

legend(5, 40, sort(unique(df$Lifecycle)), col=cols, pch=16, bty='n')

Hopefully this helps.

Upvotes: 1

Related Questions