Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a second legend in the plot in R

I got the default legend based on a factor of a column. I colored the x-axis based on a factor of another column.

Can I add a legend for this x-axis color as well?

enter image description here

Merged Data( https://dl.dropbox.com/u/81597211/Untitled.pdf)

row.names   LCA_CASE_WORKLOC1_CITY  LCA_CASE_JOB_TITLE  LCA_CASE_WORKLOC1_STATE LCA_CASE_WAGE_RATE_FROM Company
    4726    REDWOOD SHORES  SOFTWARE DEVELOPER - ARCHITECT  CA  263500.00   ORACLE
    102663  DENVER  SOFTWARE ENGINEER (SOFTWARE DEVELOPER 5)    CO  170000.00   ORACLE
    103621  DENVER  SOFTWARE ENGINEER (SOFTWARE DEVELOPER 5)    CO  170000.00   ORACLE
    95210   SANTA CLARA SOFTWARE ENGINEER (SOFTWARE DEVELOPER 4)    CA  155000.00   ORACLE
    18858   SANTA CLARA SOFTWARE ENGINEER (CONSULTING SOLUTION DIRECTOR)    CA  150000.00   ORACLE
    19514   IRVING  CONSULTING TECHNICAL MANAGER    TX  150000.00   ORACLE
    57054   REDWOOD SHORES  SOFTWARE ENGINEER (SOFTWARE DEVELOPER 4)    CA  150000.00   ORACLE
    76335   REDWOOD SHORES  SOFTWARE ENGINEER (APPLICATIONS DEVELOPER 4)    CA  150000.00   ORACLE
    79964   REDWOOD SHORES  SOFTWARE ENGINEER (SOFTWARE DEVELOPER 5)    CA  150000.00   ORACLE

Code

library("ggplot2")
colour = factor(merged$Company)
xcolor = factor(merged$LCA_CASE_WORKLOC1_STATE)
qplot(merged[[2]], merged[[4]], colour = colour, xlab="Positions", ylab ="Salary", main="H1B Salary 2012") + theme(axis.text.x=element_text(angle=90,vjust=0.5, hjust=1, size=10, color= xcolor, lineheight=10)) + scale_y_continuous(breaks=seq(0,300000, 10000)) + theme(panel.grid.minor = element_line(colour = "red", linetype = "dotted")) + scale_x_discrete(merged[[2]])
like image 469
unj2 Avatar asked Mar 09 '13 21:03

unj2


1 Answers

This solution is not as versatile as we might want, but also it is not very difficult and technical. First some data:

y <- c(5, 2, 3, 2)
x <- factor(c("A", "B", "C", "A"))
z <- factor(c("D", "E", "F", "E"))

p <- qplot(x, y, geom = "point") +
  theme(axis.text.x = element_text(color = z))

A popular function g_legend (can be found e.g. here) is useful in this case, it takes a legend from a plot as a grob (hence this solution is not fast).

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  legend
}

So we save two legends, one for the points (x) and one for the x-axis (z).

legends <- list(g_legend(p + geom_point(aes(color = x))),
                g_legend(p + geom_point(aes(color = z)) + 
                          scale_color_manual(values = palette()[as.numeric(z)])))

Notice a difference in the second legend. palette() is used here because if e.g. z <- factor(c(1, 2, 3)) then element_text(color = z) uses different colors than geom_point(aes(color = z)), i.e. element_text(color = z) takes colors from base plot as e.g. 2 in plot(1, col = 2).

Finally, putting everything together:

library(gridExtra)
grid.arrange(p + geom_point(aes(color = x)) + guides(color = 'none'), 
             do.call(arrangeGrob, legends), nrow = 1, widths = c(0.8, 0.2))

enter image description here

like image 147
Julius Vainora Avatar answered Sep 27 '22 18:09

Julius Vainora