繁体   English   中英

更改ggplot2中图例键中的符号

[英]Change the symbol in a legend key in ggplot2

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

我想用“F”,“K”和“G”替换图例键中重复的“a”。

这可能吗? 谢谢。

修改此答案的代码:想法是禁止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")) 

在此输入图像描述

手动重命名图例添加

+ scale_x_continuous(breaks=c(x1,x2,x3), labels=c("F", "K", "G"))

其中x1,x2,x3是点数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM