R 如何在ggplot2中围绕轴刻度标签创建框?

R 如何在ggplot2中围绕轴刻度标签创建框?,r,ggplot2,axis-labels,R,Ggplot2,Axis Labels,出于几个原因,我试图复制下面所示的怪诞情节。它违反了良好数据可视化的许多规则,因此出于培训目的,我的目标是使用ggplot2并解构它——一次删除或修改一个选择不当的功能。使用底部复制的数据和绘图下面的代码,我接近了,但无法找出如何包含一个显著的特征 问题:是否有办法在三个刻度标签周围重现黑色阴影矩形?(如果是这样的话,可以直接创建另一个因子变量来标识这三个标签,并将其字体更改为白色。) 顺便说一句,这个问题可能会提供一些使用grImport软件包的见解,但我无法理解它的教导,我正在寻找指定记

出于几个原因,我试图复制下面所示的怪诞情节。它违反了良好数据可视化的许多规则,因此出于培训目的,我的目标是使用
ggplot2
并解构它——一次删除或修改一个选择不当的功能。使用底部复制的数据和绘图下面的代码,我接近了,但无法找出如何包含一个显著的特征

问题:是否有办法在三个刻度标签周围重现黑色阴影矩形?(如果是这样的话,可以直接创建另一个因子变量来标识这三个标签,并将其字体更改为白色。)

顺便说一句,这个问题可能会提供一些使用
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")

plotpg19有点偏离主题,但这是一个建议的快速而肮脏的解决方法:将图形导出为PNG(位图)或SVG(基于矢量),然后手动添加带有白色文本的黑色块。

我认为最好的办法是在勾号标签的位置创建视口,并在那里绘制黑色矩形和白色文本。例如:

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)

vp1我能想到的唯一方法是创建绘图,并手动调用
ggplot\u build
ggplot\u gtable
获取绘图的网格版本,然后手动编辑相应的表格。您不妨打印出来并“将其涂成黑色”?;-)也许。但是你还需要扫描它:-)真的。我得想点别的@哈克
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)