2016-06-14 8 views
5

Ich zeichne die folgenden Daten in ein ggplot Balkendiagramm.ggplot Annotation an einem festen Platz im Diagramm

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") 

Ich verwende den folgenden Code, um das Diagramm zu rendern. Es funktioniert gut. Das Problem besteht jedoch darin, dass sich die Yaxis-Werte ständig ändern und die Anmerkung oben im Diagramm manchmal verschwindet oder in anderen Fällen in der Mitte des Diagramms erscheint.

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) 

Ist es möglich, die Position der Annotation dynamisch festzulegen?

+0

Vorschlag, können Sie alle Themen in ein Thema setzen(), wird Code sauberer sein. – zx8754

+1

@ zx8754 Vielen Dank für den Vorschlag wird sicherlich tun. – Apricot

+0

Können Sie weitere Informationen über genau das, was Sie wollen, bereitstellen? Sie möchten, dass sich der Annotationstest "Quelle: ABC-Monitore" nicht bewegt? Möchten Sie es als festen Prozentsatz der Y-Achse festlegen? –

Antwort

3

Statt die Position des Einstellens hjust mit und VJust warum setzt nicht die y-Position in der höchsten Bar?

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

Vielen Dank darren17 ... Ich habe versucht, die Y-Achse neu auszurichten - aber es versaut es mit dem Vjust ..... das funktioniert sicherlich. Danke nochmal. – Apricot

3

Erweitern @ Benutzer20650 Idee, textGrob ermöglicht die Verwendung relativer Koordinaten, aber annotation_custom verwenden, um den Grob auf das Diagrammfeld einzuschränken.

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

Vielen Dank Sandy Muspratt. Ich habe versucht, TextGrob mit Annotate-custom aus einem Beispiel in SO - aber wieder nicht versuchen, die X-und Y-Achse Werte in der Bezeichnung selbst ... obwohl ich zugeben muss, ich habe noch nie textGrob verwendet. Ich fand darren7 Vorschlag ziemlich direkt und habe es benutzt.Vielen Dank Sandy Muspratt für die Vorschläge. – Apricot

+0

Keine Sorge. Darren ist eine einfache Lösung. –

Verwandte Themen