r - 组合ggplot中的图例位置

标签 r ggplot2 legend

我是 R 的新手,更不用说 ggplot,所以我提前为下面示例中的任何问题道歉。

使用现有问题中的示例,我尝试将代码放在一起以组合两个单独的 ggplots。我想知道如何将最终图的图例移动到图形上方。我已经尝试将单个 ggplots 的 legend.postion 更改为“顶部”,但它似乎不起作用。

(请注意,我省略了第二个 y 轴的创建,以将示例中的代码保持在我认为说明我的问题所需的最低限度)

    library(ggplot2)
    library(gtable)
    library(reshape2)
    library(grid)
    library(scales)

    df.test <- data.frame(
        x_cat = factor(c(1, 2, 3, 4)),
        count = seq(1:4),
        line1 = seq(from = 1, to = 4, length.out = 4),
        line2 = seq(from = 0, to = 3, length.out = 4)
    )

    p1 <- ggplot( data = df.test , aes(x=x_cat, y=count) ) +
          geom_bar( stat="identity" ) +
          xlab( "X Label" ) +
          ylab( "Y Label 1" ) +
          theme(panel.background = element_rect(colour = "white"),
                panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                legend.position = "bottom")
    g1 <- ggplotGrob( p1 )

    df.test2 <- melt( df.test[-2] )
    p2 <- ggplot( data = df.test2 , aes(x=x_cat, y=value, colour=variable ) ) +
          geom_line( aes(group=variable) ) +
          ylab( "Y Label 2" ) +
          theme(panel.background = element_rect(fill = NA, colour = "white"),
                panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                legend.position = "bottom") 
    g2 <- ggplotGrob(p2)

    pp <- c(subset(g1$layout, name == "panel", se = t:r))
    g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l)

    pp <- c(subset(g2$layout, name == "guide-box", se = t:r))
    g <- gtable_add_grob(g, g2$grobs[[which(g2$layout$name == "guide-box")]], t=pp$t, l=pp$l, b=pp$b, r=pp$r)

    grid.draw(g)

最佳答案

您不需要组合两个单独的图。 ggplot 方法是将其视为具有两个图层的单个图,一个条形图层和一个线条图层。所以我们只需要弄清楚如何将这些层放在一个图上。例如:

library(ggplot2)
library(reshape2)

df.test <- data.frame(
  x_cat = factor(c(1, 2, 3, 4)),
  count = seq(1:4),
  line1 = seq(from = 1, to = 4, length.out = 4),
  line2 = seq(from = 0, to = 3, length.out = 4)
)

df.test2 = melt(df.test, id.var=c("x_cat", "count"))

ggplot() +
  geom_bar(data=subset(df.test2, variable=="line1"), aes(x=x_cat, y=count), 
           stat="identity" ) +
  geom_line(data=df.test2, aes(x=x_cat, y=value, colour=variable, group=variable)) +
  xlab( "X Label" ) +
  ylab( "Y Label 1" ) +
  theme(panel.background = element_rect(colour = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "top")

enter image description here

原来,我们这里不需要任何传奇体操。但是,如果您确实需要将单独的图与单个图例组合在一起,here , here , 和 here是一些例子。

关于r - 组合ggplot中的图例位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779223/

相关文章:

r - 按组选择前 n 行,行数不同

用字符串中的 0 替换重复的值

r - 将列名传递给R tidyr传播

r - 使用 ggplot_build 和 ggplot_gtable 后使用 ggsave 保存图形

r - 在 ggplot 中向 barplot 添加图例

matlab - 图例在matlab中给出了太多的数据

R Shiny 自动开始下载

r - ggplot2 facet 标签 - 第二行不显示

r - 在函数内传递 ggplot2 标题

excel - VBA 图例和绘图区域调整大小