【问题标题】:Add a second legend in the plot in R在 R 的图中添加第二个图例
【发布时间】:2013-03-09 21:45:44
【问题描述】:

我得到了基于列因子的默认图例。我根据另一列的因子为 x 轴着色。

我也可以为这个 x 轴颜色添加一个图例吗?

合并数据(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

代码

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]])

【问题讨论】:

  • 你能展示你用来生成这个的代码吗(最好在一个可重现的例子中?)
  • @David:我附上了数据框。您还在遇到数据问题吗?
  • @kunj2aan,当您完全复制粘贴在新 R 会话上的内容时,您是否能够正确地获得绘图?
  • @Arun,你看到了什么错误?
  • 有(有些困难)的解决方法,但基本答案是否定的。您不能有多个色标。这是 ggplot2 内置的设计选择。正如我所说,有一些技巧可以绕过它,但这并不简单。

标签: r statistics ggplot2


【解决方案1】:

这个解决方案并不像我们想要的那样通用,但也不是很困难和技术。首先是一些数据:

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))

一个流行的函数g_legend(可以找到例如here)在这种情况下很有用,它把一个图例作为一个grob(因此这个解决方案并不快)。

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
}

所以我们保存了两个图例,一个用于点 (x),一个用于 x 轴 (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)])))

请注意第二个图例的不同之处。 palette() 在这里使用,因为如果例如z &lt;- factor(c(1, 2, 3)) 然后element_text(color = z) 使用与geom_point(aes(color = z)) 不同的颜色,即element_text(color = z) 从基本图中获取颜色,例如2 在plot(1, col = 2)

最后,把所有东西放在一起:

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2019-01-27
    • 2021-04-16
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多