2015-07-12 45 views
16

出於以下幾個原因,我試圖複製下面顯示的怪誕陰謀。它違反了良好數據可視化的許多規則,所以出於培訓目的,我的目標是使用ggplot2並解構它 - 逐個刪除或修改選擇不好的功能。使用底部複製的數據和劇情下方的代碼,我越來越接近,但一直無法弄清楚如何包含一個顯着特徵。使用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 順便說一句,這個問題uses symbols for tick labels可以使用提供一些見解grImport包,但它的教導超出了我的想法,我正在尋找圍繞指定刻度標籤的陰影框。

dput(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") 
+0

我能想到的唯一方法是創建的情節,並呼籲'ggplot_build'和'手動ggplot_gtable'得到的網格版本劇情,然後手動編輯適當的grobs。 –

回答

3

我認爲你最好的選擇是在刻度標籤的位置創建視口,並在那裏繪製一個黑色的矩形和白色文本。例如:

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) 

會給你下圖:enter image description here

0

稍微偏離主題,但,這是一個建議的快速和髒的解決方法:圖表導出爲PNG(位圖)或SVG(基於矢量)和手動白色文本添加黑塊。

+0

你可能會把它打印出來並「塗黑」? ;-) – RHA

+0

也許吧。但是,您還必須掃描它:-) – hackR

+0

正確。我必須想到別的東西! @hackR – RHA

相關問題