我正在尝试将图例文本与使用geom_text
的因子变量生成的文本颜色进行颜色匹配。这是一个最小的工作示例:
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 pink", "should be blue"))
p1
我确信这是一个简单的解决方法。对先前帖子的任何建议或参考都会有所帮助。我没有发现任何特定的内容。
答案 0 :(得分:11)
根据上面的joran评论,您可以直接编辑grobs。这是一个相当丑陋的代码,所以道歉[使用grid
命令将有一个更优雅的方式来做到这一点 - 并希望有人会发布。
library(grid)
gglabcol <- function(plot1){
g <- ggplotGrob(plot1)
# legend grobs
g.b <- g[["grobs"]][[which(g$layout$name=="guide-box")]]
l <- g.b[[1]][[1]][["grobs"]]
# get grobs for legend symbols (extract colour)
lg <- l[sapply(l, function(i) grepl("GRID.text", i))]
# get grobs for legend labels
lb <- g.b[[1]][[1]][["grobs"]][grepl("label", g.b[[1]][[1]]$layout$name)]
# get change colour of labels to colour of symbols
for(i in seq_along(lg)) {
g.b[[1]][[1]][["grobs"]][grepl("label", g.b[[1]][[1]]$layout$name)][[i]][["children"]][[1]][["children"]][[1]]$gp$col <- lg[[i]]$gp$col
}
# overwrite original legend
g[["grobs"]][[which(g$layout$name=="guide-box")]] <- g.b
grid.draw(g)
invisible(g)
}
剧情
gglabcol(p1)
答案 1 :(得分:5)
有时使用grid
编辑功能编辑grob更容易 - 如果可以找到相关grob的名称。在这种情况下,可以找到它们,编辑很简单 - 将标签的颜色从黑色更改为红色或蓝色。
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))
查看grobs列表。我们要编辑的grob位于列表的底部,在“指南”框中。一组grobs - 名称以&#34;标签&#34;开头。有两个grob:
标签3-3.4-4-4-4
标签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)
如果要求键是点而不是字母怎么办?这可能很有用,因为&#39; a&#39;是图中的符号,它是图例键中的符号。这不是一个简单的编辑,如上所述。我需要一个点grob来代替文本grob。我在视口中绘制了凹凸,但如果我能找到相关视口的名称,则应该直接进行更改。
# Find the names of the relevant viewports
current.vpTree() # Scroll out to the right to find he relevant 'key' viewports.
viewport [key-4-1-1.5-2-5-2],viewport [key-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)
更新
考虑@ user20650的建议来更改图例键(请参阅下面的评论):
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
然后像以前一样继续更改图例文字的颜色。
答案 2 :(得分:3)
图中的颜色与图例中的颜色相同,但即使将绘图符号字体设置为粗体(或斜体),图例字体仍保持平滑。我不确定这是否是ggplot2
设计或预期行为的疏忽。对于某些颜色,粗体字体看起来比普通字体更饱和,使其看起来像一种不同的颜色。
无论如何,这里的垃圾比乱搞更容易,但这可能会让你得到你想要的东西。将geom_text
与普通字体一起使用,但连续两次或三次(或更多次)使用,这样你就会得到过量绘图。这将使符号和图例看起来与粗体字体类似,因为两者都将被过度绘制,并且图例符号将始终与绘图符号相同。
以下是一个例子:
library(ggplot2)
library(gridExtra)
# Original plot (with larger font size)
p1 <- ggplot(data=df) +
geom_text(aes(x=b, y=a, label=c, color=d), fontface='bold', size=8)
p1 <- p1 + scale_color_hue(name="colors should match",breaks=c("one", "two"),
labels=c("should be pink", "should be blue")) +
ggtitle("Original Plot with Bold Symbols and Plain Legend")
# New version with overplotting. (You don't need to specify 'plain' fontface.
# I've just included that to emphasize what the code is doing.)
p1.overplot <- ggplot(data=df) +
geom_text(aes(x=b, y=a, label=c, color=d), fontface='plain', size=8) +
geom_text(aes(x=b, y=a, label=c, color=d), fontface='plain', size=8) +
geom_text(aes(x=b, y=a, label=c, color=d), fontface='plain', size=8)
p1.overplot <- p1.overplot +
scale_color_hue(name="colors should match",
breaks=c("one", "two"),
labels=c("should be pink", "should be blue")) +
ggtitle("Both symbols and legend are overplotted 3 times")
答案 3 :(得分:3)
这是一个使用ggtext
并且避免直接编辑grob的解决方案。 (它确实涉及从图中提取颜色,但是后续步骤更加人性化。)
# Original code, but with a stripped-down call to `scale_color_hue` (since
# we're going to replace it).
library(ggplot2)
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(breaks=c("one", "two"))
# Load the `ggtext` library, which lets us style (parts of) text labels.
library(ggtext)
# Build the plot so we can extract the colors that were actually used. (If you
# supply colors manually instead, this step isn't necessary.)
g1 = ggplot_build(p1)
# Add a scale with labels that are colored appropriately, using <span> tags.
# Also specify that legend labels should be processed with `element_markdown`.
p1 +
scale_color_hue(name = "colors should match",
breaks = c("one", "two"),
labels = paste("<span style='color:",
unname(unlist(unique(g1$data[[1]]["colour"]))),
"'>",
c("should be pink", "should be blue"),
"</span>",
sep = "")) +
theme(legend.text = element_markdown())
答案 4 :(得分:0)
此答案基于Mike H.的question here和此问题的user20650给出的答案。
获得颜色:
pGrob <- ggplotGrob(p1)
g.b <- pGrob[["grobs"]][[which(pGrob$layout$name=="guide-box")]]
l <- g.b[[1]][[1]][["grobs"]]
# get grobs for legend symbols (extract colour)
lg <- l[sapply(l, function(i) grepl("GRID.text", i))]
clr <- mapply(FUN=function(x){x$gp$col},x=lg)
然后使用以下方法
gb <- which(grepl("guide-box", pGrob$layout$name))
gb2 <- which(grepl("guides", pGrob$grobs[[gb]]$layout$name))
label_text <- which(grepl("label",pGrob$grobs[[gb]]$grobs[[gb2]]$layout$name))
pGrob$grobs[[gb]]$grobs[[gb2]]$grobs[label_text] <-
mapply(FUN = function(x, y) {x[["children"]][[1]][["children"]][[1]]$gp <- gpar(col =y); return(x)},
x = pGrob$grobs[[gb]]$grobs[[gb2]]$grobs[label_text],
y = clr, SIMPLIFY = FALSE)
之后,您将获得相同的输出文件
grid.draw(pGrob)
我提供此答案是因为在代码的第二块中,在x$gp
函数中分配给mapply
的参数应该是gpar
对象。 Mike H.的答案中有一个错误,我正在解决。
谢谢。