如何在ggplot2中创建包围坐标轴刻度标签的方框?

18

由于几个原因,我正在尝试复制如下所示的奇怪图表。它违反了许多良好数据可视化的原则,因此为了培训目的,我的目标是使用ggplot2并逐步去除或修改选择不当的特征。使用在底部复制的数据和图表下面的代码,我已经接近成功,但一直无法找到一种方法来包含一个值得注意的功能。

问题:是否有一种方法可以复制黑色阴影矩形框住的三个刻度标签? (如果有,可以轻松创建另一个因子变量来识别那三个标签,并将它们的字体更改为白色。)

enter image description here

ggplot(plotpg19, aes(x = risks, y = scores, fill = colors)) +
  geom_bar(stat = "identity", width = 0.6) +
  scale_fill_manual(values = c("grey50", "deepskyblue2", "mediumorchid3", "gold")) +
  geom_text(aes(label = scores), hjust = -0.4, size = 8, face = "bold") +
  coord_flip() +
  theme_bw() + labs(x = NULL, y = NULL) +
  theme(panel.grid.major = element_blank()) +
  guides(fill = FALSE) +
  scale_y_continuous(breaks = seq(0, 100, 20), labels = seq(0, 100, 20), expand = c(0, 0)) +
  theme(
    panel.border = element_blank(),
    axis.line = element_line(colour = "black", size = 5, linetype = "solid", lineend = "square")
  ) +
  geom_hline(yintercept = seq(0, 80, 10), colour = "grey30", size = .5, linetype = "dotted") +
  theme(axis.line.x = element_blank()) # to get a single axis border, must format both and then blank one

enter image description here

顺便说一句,这个问题使用符号作为刻度标签可能提供了一些使用grImport包的见解,但它的教程对我来说太过深奥,我正在寻找一个在指定刻度标签周围绘制阴影框的方法。

plotpg19 <- structure(list(risks.format = structure(c(2L, 3L, 1L, 4L, 5L, 
  7L, 8L, 6L), .Label = c("Geographic locations in which\n                                 the company operates", 
  "Inadequate resources for anti-bribery/\ncorruption compliance activities", 
  "Industries/sector(s) in which\n                                 the company operates", 
  "Lack of anti-bribery/corruption training\n                                 or awareness within the business", 
  "Lack of understanding\n                                 by top executives", 
  "Other", "Rogue employees", "Third parties/associates\n                                 acting on our behalf"
  ), class = "factor"), risks = structure(c(8L, 7L, 6L, 5L, 4L, 
  3L, 2L, 1L), .Label = c("Other", "Third parties/associates acting on our behalf", 
  "Rogue employees", "Lack of understanding by top executives", 
  "Lack of anti-bribery/corruption training or awareness within the business", 
  "Geographic locations in which the company operates", "Industries/sector(s) in which the company operates", 
  "Inadequate resources for anti-bribery/corruption compliance activities"
  ), class = "factor"), scores = c(15, 28, 71, 16, 5, 48, 55, 2
  ), colors = c("A", "A", "B", "A", "A", "C", "D", "A")), .Names = c("risks.format", 
  "risks", "scores", "colors"), row.names = c(NA, -8L), class = "data.frame")

我能想到的唯一方法是手动创建图形,然后调用 ggplot_buildggplot_gtable 来获取图形的网格版本,最后手动编辑适当的 grobs。 - Brian Diggs
2个回答

4

我认为最好的方法是在刻度标签的位置创建视口,并在此处绘制黑色矩形和白色文本。例如:

vp1 <- viewport(x = 0.225, y = 0.55, width = 0.45, height = 0.07)
grid.rect(gp = gpar(fill="black"), vp = vp1)
grid.text("Lack of anti-bribery corruption training or awareness within the business", gp=gpar(col="white", cex = 0.6), vp = vp1)

将会给您展示下面的图表:在此输入图片描述

0

有点偏题,但这是一个快速而简单的解决方法:将图形导出为PNG(位图)或SVG(矢量),然后手动添加黑块和白色文本。


2
你不妨把它打印出来,然后“涂黑”?;-) - RHA
也许吧。但是那样你还得扫描它 :-) - hackR
真的。我必须想出其他办法!@hackR - RHA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接