ggplot条形图 - 按照x轴因子分割填充图例

时间:2015-06-30 10:35:13

标签: r plot ggplot2 legend

是否可以按照图表x轴上的值分割fill条形图的ggplot图例?

例如使用此数据:

library(ggplot2)
data <- data.frame(val=c(2,4,5,6,7,8,9),var1=c("A","A","A","B","B","C","C"),
      var2=sample(LETTERS[1:7]))
ggplot(data,aes(x=factor(var1),y=val,fill=var2))+geom_bar(stat="identity")

我得到以下情节: enter image description here

我想有这样的东西,以便更容易找到每个fill颜色对应的内容:

enter image description here

1 个答案:

答案 0 :(得分:1)

评论中链接中的解决方案的替代方案。该解决方案假定数据以聚合形式提供,并且var2的每个类别都出现在var1的一个且仅一个类别中。也就是说,图例中的键数(及其顺序)是正确的。所有需要的是在适当的键之间插入空间,并将文本放入这些空间。它从初始绘图或其构建数据中获取构建绘图所需的信息。

library(ggplot2)
library(gtable)
library(grid)

set.seed(1234)
data <- data.frame(val = c(2,4,5,6,7,8,9),
      var1 = c("A","A","A","B","B","C","C"),
      var2 = sample(LETTERS[1:7]))

# Sort levels of var2
data$var2 = factor(data$var2, labels = data$var2, levels = data$var2)

p = ggplot(data, aes(x = factor(var1), y = val, fill = var2)) +
   geom_bar(stat = "identity")

# Get the ggplot grob
g = ggplotGrob(p)

# Get the legend
leg = g$grobs[[which(g$layout$name == "guide-box")]]$grobs[[1]]

# Get the labels from the ggplot build data
gt = ggplot_build(p)
labels = rev(gt$layout$panel_params[[1]]$x.labels)

## Positions of the labels
# Get the number of keys within each label from the ggplot build data
gt$data[[1]]$x
N = as.vector(table(gt$data[[1]]$x))
N = N[-length(N)]
# Get the positions of the labels in the legend gtable
pos = rev(cumsum(N)) + 3
pos = c(pos, 3)

# Add rows to the legend gtable, and add the labels to the new rows
for(i in seq_along(pos)){
leg = gtable_add_rows(leg, unit(1.5, "lines"), pos = pos[i]) 
leg = gtable_add_grob(leg, textGrob(labels[i], y = 0.1, just = "bottom"), 
         t = pos[i] + 1, l = 2)
}

# Put the legend back into the plot
g$grobs[[which(g$layout$name == "guide-box")]]$grobs[[1]] = leg

# Draw it
grid.newpage()
grid.draw(g)

enter image description here