相关疑难解决方法(0)

在ggplot2图例中使用图像而不是标签

我在ggplot2中有一个情节,例如2行,在传说中我有"鲨鱼"和"老虎".有没有办法让鲨鱼/老虎图像出现在图例而不是文字中?

graphing r ggplot2

14
推荐指数
1
解决办法
1642
查看次数

在保留标签的同时删除ggplot图例符号

示例代码和图:

data <- data.frame( ID = c(LETTERS[1:26], paste0("A",LETTERS[1:26])),
                    Group = rep(c("Control","Treatment"),26),
                    x = rnorm(52,50,20),
                    y = rnorm(52,50,10))

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_text(color=c("blue","red")))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我正在尝试解决的是删除图例符号("a")并为组标签(控制和处理)着色,因为它们出现在图中(分别为蓝色和红色).

我试过了:

geom_text(show_guide = F)
Run Code Online (Sandbox Code Playgroud)

但这完全取消了传说.

为了保持简单,我可以使用注释...但是想知道是否有特定于图例的解决方案.

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) +
   geom_text(size=8, show_guide=F) +
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   annotate("text",label="Control", color="blue",x=20,y=80,size=8) +
   annotate("text",label="Treatment", color="Red",x=23,y=77,size=8)
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

9
推荐指数
2
解决办法
1564
查看次数

标签 统计

ggplot2 ×2

r ×2

graphing ×1

legend ×1