Scale colour identity legend labels

871 views
Skip to first unread message

Trevor

unread,
Jul 22, 2013, 11:31:54 PM7/22/13
to ggp...@googlegroups.com
I am hoping someone can help me figure out a problem I'm having. I'm trying to plot a scatter plot with points of different colors and boxes of different colors around each point. Using scale_colour_identity I can get the plot to look the way I would like it, but I can't figure out how to get the legend I would like. With the script below I get legend markers for the two colors of the points and the two colors of the boxes. I would like to have the legend only include the colors of the dots, not the boxes, and I would like the point labels in the legend to be the values in the Brand column of the data frame that correspond to each point color. I tried using the limits= and breaks= options within the scale-colour_identity function, but I couldn't figure out how to use them. Anyone have thoughts on solving this problem?

Thanks,
Trevor

attrib.by.brand<-structure(list(Attribute = structure(c(1L, 1L, 2L, 2L), .Label = c("a1",
"a2"), class = "factor"), Brand = structure(c(1L, 2L, 1L, 2L),
.Label = c("b1","b2"), class = "factor"), Rating = c(50L, 60L, 70L, 80L),
colors = structure(c(2L,1L, 2L, 1L), .Label = c("blue", "red"),
class = "factor"), sigColor = structure(c(1L,2L, 1L, 2L), .Label = c("black", "grey"),
class = "factor")), .Names = c("Attribute","Brand", "Rating", "colors", "sigColor"),
class = "data.frame", row.names = c(NA,-4L))

plot<-ggplot(attrib.by.brand,aes(Rating,Attribute))+
geom_point(aes(colour=colors))+
geom_point(size=8,shape=22,aes(colour=sigColor))+
scale_colour_identity(guide="legend",labels=attrib.by.brand$Brand)

plot

Trevor

unread,
Jul 23, 2013, 2:11:51 AM7/23/13
to ggp...@googlegroups.com
Well I think I found a solution, although it feels a bit clunky, so I'd still be interested in hearing other solutions.  I used the fillable dot shape (shape=21) so that I could use the scale_fill_identity to specify the dot colors and the legend and used scale_colour_identity to specify the box colors.  This has the mildly annoying feature of leaving the dots with a black border in the legend, but I can certainly live with that.  Here's the code I ended up with in case anyone is interested.

attrib.by.brand<-structure(list(Attribute = structure(c(1L, 1L, 2L, 2L), .Label = c("a1", 
                "a2"), class = "factor"), Brand = structure(c(1L, 2L, 1L, 2L), 
                .Label = c("b1","b2"), class = "factor"), Rating = c(50L, 60L, 70L, 80L), 
                colors = structure(c(2L,1L, 2L, 1L), .Label = c("blue", "red"), 
                class = "factor"), sigColor = structure(c(1L,2L, 1L, 2L), .Label = c("black", "grey"), 
                class = "factor")), .Names = c("Attribute","Brand", "Rating", "colors", "sigColor"), 
                class = "data.frame", row.names = c(NA,-4L))

colorsScale<-unique(attrib.by.brand$colors)
brandNames<-unique(attrib.by.brand$Brand)
brandNames2<-factor(brandNames, levels=brandNames[order(colorsScale)],ordered=T)

plot<-ggplot(attrib.by.brand,aes(Rating,Attribute))+
  geom_point(size=4,shape=21,aes(fill=colors,color=colors))+
  geom_point(size=8,shape=22,aes(colour=sigColor))+
  scale_colour_identity()+
  scale_fill_identity(guide="legend",labels=levels(brandNames2))

plot
Reply all
Reply to author
Forward
0 new messages