2016-06-14 9 views
5

Sto tracciando i seguenti dati in un grafico a barre ggplot.annotazione ggplot in un posto fisso nella tabella

structure(list(MEDIATYPE = c("BACKLIT TOWER", "BILLBOARDS", "BRIDGE PANEL", 
"BUILDING FACADES", "BUS SHELTER", "CANTILIVERS", "CYCLE SHELTER", 
"FOB", "FREE STANDING PANEL", "GANTRIES"), RENTAL = c(197, 278363, 
1423, 26, 35960, 6194, 70, 4845, 27, 9420)), .Names = c("MEDIATYPE", 
"RENTAL"), row.names = c(NA, 10L), class = "data.frame") 

Sto usando il seguente codice per rendere il grafico. Funziona bene. Tuttavia, il problema è che i valori di yax continuano a cambiare e l'annotazione nella parte superiore del grafico a volte scompare o in altri casi appare nel mezzo del grafico.

library(ggplot2) 
library(stringr) # str_wrap 


ggplot(b, aes(x=reorder(MEDIATYPE,-RENTAL), y=RENTAL, fill=MEDIATYPE)) + geom_bar(stat = "identity", width = 0.8) + 
    theme(legend.position = "none") + xlab("MEDIATYPE") + ylab("SPENDS") + 
    scale_x_discrete(labels = function(x) str_wrap(x, width = 1)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    geom_text(aes(label=RENTAL), vjust = 0.5,hjust = 1, angle = 90, colour = "white",size = 3) + 
    ggtitle("MEDIAWISE SPENDS") + 
    theme(plot.title=element_text(size=rel(1.4), lineheight = 1, face = "bold")) + 
    theme(axis.text = element_text(size = 8, color = "black")) + 
    theme(axis.title = element_text(size=10, face = "bold")) + 
    theme(panel.background = element_rect(fill = "grey95")) + 
    ggplot2::annotate(geom = "text", label = "Source:ABC Monitors", x = Inf, y = -Inf, color = "blue",size = 3,fontface = "italic",hjust = 1, vjust = -30) 

È possibile impostare dinamicamente la posizione dell'annotazione?

+0

suggerimento, è possibile inserire tutti i temi in un tema(), il codice sarà più pulita. – zx8754

+1

@ zx8754 Grazie per il suggerimento lo farò sicuramente. – Apricot

+0

Puoi fornire ulteriori informazioni su esattamente ciò che desideri? Vuoi che il test di annotazione "Fonte: monitor ABC" non si sposti? Vuoi impostarlo come una percentuale fissa dell'asse y? –

risposta

3

Invece di regolare la posizione utilizzando hjust e vjust perché non impostare la posizione y alla barra più alto?

ggplot(b, aes(x=reorder(MEDIATYPE,-RENTAL), y=RENTAL, fill=MEDIATYPE)) + geom_bar(stat = "identity", width = 0.8) + 
    theme(legend.position = "none") + xlab("MEDIATYPE") + ylab("SPENDS") + 
    scale_x_discrete(labels = function(x) str_wrap(x, width = 1)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    geom_text(aes(label=RENTAL), vjust = 0.5,hjust = 1, angle = 90, colour = "white",size = 3) + 
    ggtitle("MEDIAWISE SPENDS") + 
    theme(plot.title=element_text(size=rel(1.4), lineheight = 1, face = "bold")) + 
    theme(axis.text = element_text(size = 8, color = "black")) + 
    theme(axis.title = element_text(size=10, face = "bold")) + 
    theme(panel.background = element_rect(fill = "grey95")) + 
    ggplot2::annotate(geom = "text", label = "Source:ABC Monitors", 
        x = Inf, y = max(b$RENTAL), 
        color = "blue",size = 3,fontface = "italic",hjust = 1, vjust = 1) 

enter image description here

+0

Molte grazie darren17 ... Ho provato a riallineare l'asse y - ma l'ho incasinato con il vjust ..... questo funziona sicuramente. Grazie ancora. – Apricot

3

L'estensione dell'idea di @ user20650, textGrob consente di utilizzare le relative coordinate, ma utilizzare annotation_custom per limitare il grob al riquadro di stampa.

b = structure(list(MEDIATYPE = c("BACKLIT TOWER", "BILLBOARDS", "BRIDGE PANEL", 
"BUILDING FACADES", "BUS SHELTER", "CANTILIVERS", "CYCLE SHELTER", 
"FOB", "FREE STANDING PANEL", "GANTRIES"), RENTAL = c(197, 278363, 
1423, 26, 35960, 6194, 70, 4845, 27, 9420)), .Names = c("MEDIATYPE", 
"RENTAL"), row.names = c(NA, 10L), class = "data.frame") 

# Try a different y range 
#  b[2, 2] = 30000 

library(ggplot2) 
library(stringr) # str_wrap 
library(grid) 

label = textGrob(label = "Source:ABC Monitors", x = .95, y = 0.95, 
     just = c("right", "top"), 
     gp=gpar(fontface = "italic", col = "blue",size = 3)) 

p = ggplot(b, aes(x=reorder(MEDIATYPE,-RENTAL), y=RENTAL, fill=MEDIATYPE)) + geom_bar(stat = "identity", width = 0.8) + 
    theme(legend.position = "none") + xlab("MEDIATYPE") + ylab("SPENDS") + 
    scale_x_discrete(labels = function(x) str_wrap(x, width = 1)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    geom_text(aes(label=RENTAL), vjust = 0.5,hjust = 1, angle = 90, colour = "white",size = 3) + 
    ggtitle("MEDIAWISE SPENDS") + 
    theme(plot.title=element_text(size=rel(1.4), lineheight = 1, face = "bold")) + 
    theme(axis.text = element_text(size = 8, color = "black")) + 
    theme(axis.title = element_text(size=10, face = "bold")) + 
    theme(panel.background = element_rect(fill = "grey95")) + 
    annotation_custom(label, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) 

enter image description here

+0

Mille grazie, Sandy Muspratt. Ho provato a usare textGrob con annotate-custom da un esempio in SO - ma ancora non ho provato a specificare i valori dell'asse xey nell'etichetta stessa ... anche se devo ammettere che non ho mai usato textGrob. Ho trovato un suggerimento darren7 piuttosto semplice e l'ho usato.Grazie mille Sandy Muspratt per i suggerimenti. – Apricot

+0

Nessun problema. Darren's è una soluzione semplice. –

Problemi correlati