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

标签 r ggplot2 ggdendro

这是我的数据:

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

我添加了群体因素

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

以下代码

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

产生这个情节:

ex1

当我添加时

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

标题已更改,图例键的大小已更改,但形状参数被忽略:

ex2

这是为什么?

谢谢!

最佳答案

要获取图例中的点,您需要添加一个geom_point图层。为了防止它出现在实际绘图中,您可以在 geom 本身中设置 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)))

enter image description here

关于r - 当在 geom_text 中定义指南时更改图例符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380195/

相关文章:

从图例中删除特定标记

r - 使用 ggdendrogram 创建树状图

r - 操纵 Y 轴限制不起作用(ggplot2 百分比条形图)

r - 使用 Count 在 R 中进行数据透视

version-control - R 的最佳版本系统

r - 记录 ggplot2 统计扩展 - devtools::document() 未创建 packagename-ggproto.Rd

r - directlabels geom_dl() 中的标签大小

r - R 中的 3D 曲面图,给定 x,y,z 坐标

r - 如何将分类变量添加到百分比堆积条形图中?

r - 减小树状图(或 y 轴)ggplot 的大小