我在ggplot2中有一个情节,例如2行,在传说中我有"鲨鱼"和"老虎".有没有办法让鲨鱼/老虎图像出现在图例而不是文字中?
示例代码和图:
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)