此R代码产生一个ggplot2图,其中图例键包含以红色,蓝色和绿色重复的字母“ a”。
x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)
require(ggplot2)
ggplot(df, aes(x = x, y = y, col = s, label = s)) +
geom_text() +
scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig"))
Run Code Online (Sandbox Code Playgroud)
我想将图例键中重复的“ a”替换为“ F”,“ K”和“ G”。
请问这可能吗?谢谢。
为此答案改编代码:想法是禁止显示geom_text图例,但允许图例用于geom_point,但是将点的大小设置为零,以使点在图中不可见,然后在图例中设置点的大小和形状guides声明
x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)
#
require(ggplot2)
#
ggplot(df, aes(x = x, y = y, colour = s, label = s)) +
geom_point(size = 0, stroke = 0) + # OR geom_point(shape = "") +
geom_text(show.legend = FALSE) +
guides(colour = guide_legend(override.aes = list(size = 5, shape = c(utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"))))) +
scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig"))
Run Code Online (Sandbox Code Playgroud)
