简体   繁体   English

在geom_text中定义指南时更改图例符号

[英]Changing legend symbols when the guide is defined inside geom_text

Here is my data: 这是我的数据:

ddata1 <- structure(list(segments = structure(list(x = c(8.203125, 1.5, 
1.5, 1, 1.5, 2, 8.203125, 14.90625, 14.90625, 10.0625, 10.0625, 
5.6875, 5.6875, 3.875, 3.875, 3, 3.875, 4.75, 4.75, 4, 4.75, 
5.5, 5.5, 5, 5.5, 6, 5.6875, 7.5, 7.5, 7, 7.5, 8, 10.0625, 14.4375, 
14.4375, 12.125, 12.125, 10.5, 10.5, 9.5, 9.5, 9, 9.5, 10, 10.5, 
11.5, 11.5, 11, 11.5, 12, 12.125, 13.75, 13.75, 13, 13.75, 14.5, 
14.5, 14, 14.5, 15, 14.4375, 16.75, 16.75, 16, 16.75, 17.5, 17.5, 
17, 17.5, 18, 14.90625, 19.75, 19.75, 19, 19.75, 20.5, 20.5, 
20, 20.5, 21), y = c(0.597091229013013, 0.597091229013013, 0.353069357803605, 
0.353069357803605, 0.353069357803605, 0.353069357803605, 0.597091229013013, 
0.597091229013013, 0.448435999122362, 0.448435999122362, 0.390288662068433, 
0.390288662068433, 0.277787356115265, 0.277787356115265, 0.209941905126808, 
0.209941905126808, 0.209941905126808, 0.209941905126808, 0.179837725036859, 
0.179837725036859, 0.179837725036859, 0.179837725036859, 0.136782743294966, 
0.136782743294966, 0.136782743294966, 0.136782743294966, 0.277787356115265, 
0.277787356115265, 0.227863143853408, 0.227863143853408, 0.227863143853408, 
0.227863143853408, 0.390288662068433, 0.390288662068433, 0.356332108523753, 
0.356332108523753, 0.307670014691839, 0.307670014691839, 0.255894447541048, 
0.255894447541048, 0.145256016771056, 0.145256016771056, 0.145256016771056, 
0.145256016771056, 0.255894447541048, 0.255894447541048, 0.221845947877971, 
0.221845947877971, 0.221845947877971, 0.221845947877971, 0.307670014691839, 
0.307670014691839, 0.29024123584904, 0.29024123584904, 0.29024123584904, 
0.29024123584904, 0.255131135079098, 0.255131135079098, 0.255131135079098, 
0.255131135079098, 0.356332108523753, 0.356332108523753, 0.337359353946151, 
0.337359353946151, 0.337359353946151, 0.337359353946151, 0.202624960168806, 
0.202624960168806, 0.202624960168806, 0.202624960168806, 0.448435999122362, 
0.448435999122362, 0.438580594379611, 0.438580594379611, 0.438580594379611, 
0.438580594379611, 0.359137362193916, 0.359137362193916, 0.359137362193916, 
0.359137362193916), xend = c(1.5, 1.5, 1, 1, 2, 2, 14.90625, 
14.90625, 10.0625, 10.0625, 5.6875, 5.6875, 3.875, 3.875, 3, 
3, 4.75, 4.75, 4, 4, 5.5, 5.5, 5, 5, 6, 6, 7.5, 7.5, 7, 7, 8, 
8, 14.4375, 14.4375, 12.125, 12.125, 10.5, 10.5, 9.5, 9.5, 9, 
9, 10, 10, 11.5, 11.5, 11, 11, 12, 12, 13.75, 13.75, 13, 13, 
14.5, 14.5, 14, 14, 15, 15, 16.75, 16.75, 16, 16, 17.5, 17.5, 
17, 17, 18, 18, 19.75, 19.75, 19, 19, 20.5, 20.5, 20, 20, 21, 
21), yend = c(0.597091229013013, 0.353069357803605, 0.353069357803605, 
0, 0.353069357803605, 0, 0.597091229013013, 0.448435999122362, 
0.448435999122362, 0.390288662068433, 0.390288662068433, 0.277787356115265, 
0.277787356115265, 0.209941905126808, 0.209941905126808, 0, 0.209941905126808, 
0.179837725036859, 0.179837725036859, 0, 0.179837725036859, 0.136782743294966, 
0.136782743294966, 0, 0.136782743294966, 0, 0.277787356115265, 
0.227863143853408, 0.227863143853408, 0, 0.227863143853408, 0, 
0.390288662068433, 0.356332108523753, 0.356332108523753, 0.307670014691839, 
0.307670014691839, 0.255894447541048, 0.255894447541048, 0.145256016771056, 
0.145256016771056, 0, 0.145256016771056, 0, 0.255894447541048, 
0.221845947877971, 0.221845947877971, 0, 0.221845947877971, 0, 
0.307670014691839, 0.29024123584904, 0.29024123584904, 0, 0.29024123584904, 
0.255131135079098, 0.255131135079098, 0, 0.255131135079098, 0, 
0.356332108523753, 0.337359353946151, 0.337359353946151, 0, 0.337359353946151, 
0.202624960168806, 0.202624960168806, 0, 0.202624960168806, 0, 
0.448435999122362, 0.438580594379611, 0.438580594379611, 0, 0.438580594379611, 
0.359137362193916, 0.359137362193916, 0, 0.359137362193916, 0
)), .Names = c("x", "y", "xend", "yend"), row.names = c(NA, 80L
), class = "data.frame"), labels = structure(list(x = c(1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21), y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0), label = structure(1:21, .Label = c("R72", "R73", 
"R13", "R62", "R22", "R42", "R31", "R52", "R32", "R33", "R41", 
"R43", "R63", "R21", "R51", "R61", "R11", "R12", "R53", "R23", 
"R71"), class = "factor")), .Names = c("x", "y", "label"), row.names = c(NA, 
21L), class = "data.frame"), leaf_labels = NULL, class = "hclust"), .Names = c("segments", 
"labels", "leaf_labels", "class"), class = "dendro")

I add group factors 我加上团体因素

labs1 <- label(ddata1)
labs1$groups <- as.factor(c("G", "G", "A", "F","B","D","C","E","C","C","D","D","F","B","E","F","A","A","E","B","G"))

The following code 以下代码

library(ggplot2)
library(scales)
library(ggdendro)
ggplot(segment(ddata1)) + 
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) + 
  geom_text(data=label(ddata1), aes(label=label, x=x, y=-0.05, col=labs1$group)) +
  coord_flip() + 
  scale_y_reverse(expand = c(0.2, 0)) +
  theme_bw() +
  theme(axis.text.y=element_blank(),
        legend.text=element_text(size=14, face="bold")) +
  xlab("") +
  ylab("")

produces this plot: 产生这个情节:

例1

when i add 当我添加

+
guides(colour = guide_legend("Treatment", override.aes = list(size = 7, shape=19)))

the title is changed, the size of the legend key are changed, but the shape-argument is ignored: 更改标题,更改图例键的大小,但忽略形状参数:

例2

Why is that? 这是为什么?

Thank you! 谢谢!

To get points in the legend, you need to add a geom_point layer. 要在图例中获取点,您需要添加geom_point图层。 To prevent it from appearing in your actual plot, you can set alpha = 0 in the geom itself, then set alpha = 1 in the override: 为了防止它出现在实际绘图中,可以在几何图形本身中设置alpha = 0 ,然后在替代中设置alpha = 1

ggplot(segment(ddata1)) + 
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) + 
  geom_text(data=label(ddata1), aes(label=label, x=x, y=-0.05, col=labs1$group), show.legend = FALSE) +
  geom_point(data=label(ddata1), aes(x=x, y=-0.05, col=labs1$group), alpha = 0) +
  coord_flip() + 
  scale_y_reverse(expand = c(0.2, 0)) +
  theme_bw() +
  theme(axis.text.y=element_blank(),
        legend.text=element_text(size=14, face="bold")) +
  xlab("") +
  ylab("") +
  guides(colour = guide_legend("Treatment", override.aes = list(size = 7, alpha = 1)))

在此处输入图片说明

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

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