user3605914
user3605914

Reputation:

Change symbol colors in barchart legend

I have searched through this site and others and can't seem to find the problem in my code. I am trying to change the symbol colors in my barchart legend to correspond to the colors I specified when making the chart (using the lattice package).

For the legend, I am using the auto.key function. The bars correspond to bird counts for several treatments. I first had to aggregate the bird counts for each species by each treatment. I don't get any error messages with this code, but the symbol colors in the legend are the default colors rather than the 5 colors I specified to coordinate with the 5 bird species. Here is my code:

birds.trmt <- aggregate(cbind(BRSP, GTTO, HOLA, VESP, WEME)~treatment, 
    data=EM.bird.plots, FUN=sum)

library(lattice)

barchart(BRSP+GTTO+HOLA+VESP+WEME~treatment, 
     data=birds.trmt, 
     par.settings=list(
       superpose.symbol=list(col=c("brown", "green", "yellow","red", "blue"),
       fill=c("brown", "green", "yellow","red", "blue"))),
      col=c("brown", "green", "yellow", "red", "blue"),
      main="Bird Counts by Treatment", 
      xlab="treatment",
      ylab="songbird count", 
      cex.main=2, 
      cex.lab=1.2, 
      auto.key=list(space="right"))

Upvotes: 0

Views: 1288

Answers (2)

jen
jen

Reputation: 11

Thanks! I found a simpler solution--in my original code, "superpose.symbol" should have been "superpose.polygon". Once I changed that, the plot and legend were displayed correctly.

Upvotes: 1

John Paul
John Paul

Reputation: 12684

In place of your auto.key try this:

key= list(space="right", 
  text=list(c("label 1", "label 2", "label 3", "lablel 4", "label 5"),
  points=list(col=c("brown", "green", "yellow","red", "blue")
)

Make this part of the barchart() function. I don't know how you want your key to look - you can use pch= in the points list to change the type of point drawn. You can also make the key have rectangles instead of points by using rectangles = list( col=... instead of the points list I used. You can change the color of the text labels in the key by adding a col= argument there too.

Upvotes: 0

Related Questions