A volte è più facile modificare un Grob utilizzando grid
's funzioni di editing - se i nomi dei grobs rilevanti possono essere trovati . In questo caso, possono essere trovati e la modifica è immediata: cambia il colore dell'etichetta dal nero al rosso o al blu.
library(ggplot2)
library(grid)
df <- data.frame(a=rnorm(10),b=1:10,c=letters[1:10],d=c("one","two"))
p1 <-ggplot(data=df,aes(x=b,y=a))
p1 <- p1 + geom_text(aes(label = c, color=d, fontface="bold"))
p1 <- p1 + scale_color_hue(name="colors should match",breaks=c("one", "two"),
labels=c("should be salmon", "should be sky blue"))
p1
# Get the ggplot grob
g <- ggplotGrob(p1)
# Check out the grobs
grid.ls(grid.force(g))
Guardare attraverso l'elenco di ghiotti. I grobs che vogliamo modificare sono verso la fine dell'elenco, nel set di "groove" della guida, con nomi che iniziano con "label". Ci sono due grobs:
etichetta-3-3.4-4-4-4
etichetta-4-3.5-4-5-4
# Get names of 'label' grobs.
names.grobs <- grid.ls(grid.force(g))$name
labels <- names.grobs[which(grepl("label", names.grobs))]
# Get the colours
# The colours are the same as the colours of the plotted points.
# These are available in the ggplot build data.
gt <- ggplot_build(p1)
colours <- unique(gt$data[[1]][, "colour"])
# Edit the 'label' grobs - change their colours
# Use the `editGrob` function
for(i in seq_along(labels)) {
g <- editGrob(grid.force(g), gPath(labels[i]), grep = TRUE,
gp = gpar(col = colours[i]))
}
# Draw it
grid.newpage()
grid.draw(g)
Cosa se fosse necessario che le chiavi fossero punti anziché lettere? Potrebbe essere utile perché la 'a' è un simbolo nella trama, ed è un simbolo nella chiave della legenda. Questa non è una semplice modifica, come sopra. Ho bisogno di un punto grob per prendere il posto del testo grob.Traggo delle gobbe nelle finestre, ma se riesco a trovare i nomi delle finestre pertinenti, dovrebbe essere semplice effettuare la modifica.
# Find the names of the relevant viewports
current.vpTree() # Scroll out to the right to find he relevant 'key' viewports.
finestra [tasto-4-1-1.5-2-5-2], finestra [tasto-3-1-1.4-2-4-2],
# Well, this is convenient. The names of the viewports are the same
# as the names of the grobs (see above).
# Easy enough to get the names from the 'names.grobs' list (see above).
# Get the names of 'key' viewports(/grobs)
keys <- names.grobs[which(grepl("key-[0-9]-1-1", names.grobs))]
# Insert points grobs into the viewports:
# Push to the viewport;
# Insert the point grob;
# Pop the viewport.
for(i in seq_along(keys)) {
downViewport(keys[i])
grid.points(x = .5, y = .5, pch = 16, gp = gpar(col = colours[i]))
popViewport()
}
popViewport(0)
# I'm not going to worry about removing the text grobs.
# The point grobs are large enough to hide them.
plot = grid.grab()
grid.newpage()
grid.draw(plot)
Aggiornamento
Tenuto conto di 's @ user20650 consiglio di cambiare la lege tasto nd (vedere il commento seguente):
p1 <-ggplot(data=df,aes(x=b,y=a))
p1 <- p1 + geom_text(aes(label = c, color=d, fontface="bold"))
p1 <- p1 + scale_color_hue(name="colors should match",breaks=c("one", "two"),
labels=c("should be salmon", "should be sky blue"))
GeomText$draw_key <- function (data, params, size) {
pointsGrob(0.5, 0.5, pch = 16,
gp = gpar(col = alpha(data$colour, data$alpha),
fontsize = data$size * .pt)) }
p1
Quindi procedere come prima per modificare il colore del testo della legenda.
+1 per fare il duro lavoro, anche se OP dovrebbe valutare se ne vale veramente la pena. – BrodieG
BrodieG - Come OP sono d'accordo con te, ma stavo cercando di rispondere alla richiesta dei revisori in un manoscritto dato che ci sono più di 2 livelli di fattori e il colore testuale può essere fuorviante. Forse c'è un modo migliore per tracciare questo. Lo esaminerò. –