2015-07-12 8 views
16

Per diverse ragioni, sto tentando di duplicare la trama grottesca mostrata di seguito. Violando molti precetti di buona visualizzazione dei dati, quindi per scopi di formazione il mio obiettivo è quello di utilizzare ggplot2 e decostruirlo - rimuovere o modificare le funzionalità scelte male una alla volta. Usando i dati riprodotti in basso e il codice sotto la trama, mi sto avvicinando ma non sono riuscito a capire come includere una caratteristica notevole.Con ggplot2, come si può creare una casella attorno a un'etichetta di graduazione?

Domanda: C'è un modo per riprodurre il rettangolo ombreggiato nero attorno alle tre etichette di graduazione? (Se è così, è semplice per creare un'altra variabile fattore di identificare quei tre etichette e cambiare il loro carattere al bianco.)

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 BTW, questa domanda uses symbols for tick labels potrebbe offrire alcuni spunti utilizzando il grImport pacchetto ma i suoi insegnamenti sono oltre me e sto cercando una casella ombreggiata attorno alle etichette di tick designate.

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

L'unico approccio mi viene in mente è quello di creare la trama e di chiamare '' ggplot_build' e ggplot_gtable' manualmente per ottenere la versione griglia di la trama e quindi per modificare manualmente i grobs appropriati. –

risposta

3

Penso che la soluzione migliore è creare finestre nella posizione delle etichette di graduazione e disegnare un rettangolo nero e un testo bianco. Per esempio:

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) 

vi darà il grafico qui sotto: enter image description here

0

Leggermente off-topic, ma questa è una soluzione rapida-and-dirty suggerito: esportare il grafico come PNG (bitmap) o SVG (vector-based) e aggiungere manualmente i blocchi neri con testo bianco.

+0

Si potrebbe anche stampare e "dipingere nero"? ;-) – RHA

+0

Forse. Ma poi dovresti anche scansionarlo :-) – hackR

+0

Vero. Devo pensare ad altro! @hackR – RHA

Problemi correlati