在 geom_text 内定义指南时更改图例符号
Changing legend symbols when the guide is defined inside geom_text
这是我的数据:
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("")
产生这个情节:
当我添加
+
guides(colour = guide_legend("Treatment", override.aes = list(size = 7, shape=19)))
标题已更改,图例键的大小已更改,但 shape-argument 被忽略:
这是为什么?
谢谢!
要在图例中获得积分,您需要添加一个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)))
这是我的数据:
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("")
产生这个情节:
当我添加
+
guides(colour = guide_legend("Treatment", override.aes = list(size = 7, shape=19)))
标题已更改,图例键的大小已更改,但 shape-argument 被忽略:
这是为什么?
谢谢!
要在图例中获得积分,您需要添加一个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)))