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?
suggerimento, è possibile inserire tutti i temi in un tema(), il codice sarà più pulita. – zx8754
@ zx8754 Grazie per il suggerimento lo farò sicuramente. – Apricot
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? –