R

时间:2015-06-03 05:25:37

标签: r graph ggplot2 legend

我正在尝试使用ggplot2和gridExtra包为两个聚类条形图添加图例。两个图表都有相同的图例,即时间(帖子前)。

我可以得到两个相同大小的图形彼此相邻,但是如果我只是在右图上省略legend.position = "none"参数,则包含图例但它会收缩图形。任何人都可以建议在右图的右侧包含图例的方法吗?

第一个图表的单元格和列名称如下

Graph 1
    Time GroupDC1C0         CWSQTot
1   Pre  High Expectancy    43.54545
2   Post High Expectancy    28.81818
3   Pre  Low Expectancy     43.31111
4   Post Low Expectancy     36.55556

Graph 2
    Time  GroupDC1C0        CWSQCrav
1   Pre   High Expectancy   4.977273
2   Post  High Expectancy   1.659091
3   Pre   Low Expectancy    4.955556
4   Post  Low Expectancy    3.688889

ggplot2中两个图的代码如下

    require(ggplot2)
clusteredBarsTot <- ggplot(cellMeansTot, aes(GroupDC1C0,CWSQTot)) + 
  geom_bar(aes(fill = Time), stat = "identity", position = "dodge", size = .5) +
  scale_fill_manual(values = c("#999999", "#666666")) +
  expand_limits(y = 50) +
  xlab("") + ylab("CWSQ Score") +
  theme_bw() 

clusteredBarsCrav <- ggplot(cellMeansCrav, aes(GroupDC1C0,CWSQCrav)) + 
  geom_bar(aes(fill = Time), stat = "identity", position = "dodge", size = .5) +
  scale_fill_manual(values = c("#999999", "#666666")) +
  expand_limits(y = 6) +
  xlab("") + ylab("CWSQ Craving Score") +
  theme_bw() 

titleFont <- element_text(face = "bold", color = "black", size = 16, vjust = 1.5)
titleFontX <- element_text(face = "bold", color = "black", size = 16, vjust = 0.01)
axisTextFont <- element_text(face = "plain", color = "black", size = 13) # y-axis numbers size
axisTextFontX <- element_text(face = "plain", color = "black", size = 11, angle = 45, hjust = 1, vjust = 1) #x-axis day labels
legendTitle <- element_text(face = "bold", color = "black", size = 14)
legendText <- element_text(face = "plain", color = "black", size = 13)


# add fix-ups to graph (fonts, angles on axis labels etc.) via theme argument
clusBarTot <- clusteredBarsTot + theme(title = titleFont, 
                      axis.title = titleFont,
                      axis.title.x = titleFontX,
                      axis.text  = axisTextFont,
                      axis.text.x = axisTextFontX,
                      legend.position = "none",
                      panel.grid.minor = element_blank(), 
                      panel.grid.major = element_blank()) 


clusBarCrav <- clusteredBarsCrav + theme(title = titleFont, 
                                       axis.title = titleFont,
                                       axis.title.x = titleFontX,
                                       axis.text  = axisTextFont,
                                       axis.text.x = axisTextFontX,
                                       legend.position = "none",
                                       panel.grid.minor = element_blank(),
                                       panel.grid.major = element_blank()) 

require(gridExtra)

#####side by side bar graphs
grid.arrange(clusBarTot, clusBarCrav, ncol=2)

##### draw lines on Totals graph
grid.lines(c(0.205, 0.365), c(0.87, 0.87))
grid.lines(c(0.205, 0.205), c(0.87, 0.855))
grid.lines(c(0.365, 0.365), c(0.87, 0.855))
####### draws a label on the Total graph
grid.text(expression(italic(p)==".006"),
          x= unit(0.29, "npc"), y = unit(0.9, "npc"), 
          gp=gpar(fontsize = 11))

##### draw lines on Craving graph
grid.lines(c(0.695, 0.861), c(0.845, 0.845))
grid.lines(c(0.695, 0.695), c(0.845, 0.83))
grid.lines(c(0.861, 0.861), c(0.845, 0.83))
####### draws a label on the Craving graph
grid.text(expression(italic(p)~"< .001"), x= unit(0.785, "npc"), y = unit(0.875, "npc"),
          gp=gpar(fontsize = 11))

我在stackexchange上看到一个帖子,该帖子编写了一个用常规图例布置图表的方法代码。但是,这些图形显示在一列中,图例位于单独的列中。我试着玩这个代码并在函数中输入我的两个图,但是我不太了解R以便像这样反向工程代码。代码如下:

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 2,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(clusBarTot, clusBarCrav)

正如你在代码中我的naff评论所说,我对R和编程仍然很陌生。

1 个答案:

答案 0 :(得分:0)

你正确地使用这个。如果要将图例的位置从底部(正如您所引用的问题中所请求的)更改为右侧,则以您说图例位置应该正确的方式更改代码(第三行代码并且您希望有3列,新代码应如下所示:

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="right"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 3,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(clusBarTot, clusBarCrav)