我希望通过颜色和圈出来表示我的情节中的某些点(黑白观看)。我已经将shape参数用于单独的分组,但可以使用两个geom_point
图层来完成此操作。不幸的是,我无法弄清楚如何让传奇符合我的观点。这是一个显示我的意思的最小例子:
x <- data.frame(names = rep(letters[1:24],2),
grouping1 = factor(rep(c("Treatment", "Control"), each = 24)),
grouping2 = factor(rep(rep(c("Matched","Unmatched"), each = 12), 2)),
values1 = runif(48, 60, 120), values2 = runif(48, 11, 25))
ggplot(data = x, aes(x = values1, y = values2, group = names)) +
geom_point(aes(shape = grouping1, color = grouping2)) +
geom_line(alpha = 0.2) +
geom_point(data = x[x$grouping1 == 1 & x$grouping2 == 1,], aes(values1, values2),
shape = 21, size = 3, show.legend = TRUE)
如果我上面包含show.legend = TRUE
,则圈出图例中的所有内容。我想只有红点(如图中所示)也可以用图例中的边框圈出来。感谢任何帮助。
答案 0 :(得分:1)
如果您将两个组合在一起,那么在传奇
中将它们全部搞定就更简单了x$bothgroups <- paste(x$grouping1, x$grouping2)
然后使用具有颜色和填充特征的形状21-25。这样,你可以设置颜色,使用黑色作为你想要圈出的点,创建一个黑色轮廓,并使用相同的颜色填充你不想要圈出的点,所以它们显示为没有轮廓的纯色点
ggplot(data = x, aes(x = values1, y = values2, group = names)) +
geom_point(aes(shape = bothgroups, color = bothgroups, fill = bothgroups,
stroke = 1.5)) + #make border thicker so it is visible
geom_line(alpha = 0.2) +
scale_shape_manual(values = c(21, 24, 21, 21)) + #circle triangle circle circle
#fill sets color in center of points
scale_fill_manual(values = c("green", "green", "tomato", "green")) +
#color sets outline color - match to fill for points you don't want circled
scale_color_manual(values = c("green", "green", "black", "black"))
答案 1 :(得分:1)
如果使用可填充点,您可以非常接近您想要的内容。这样,您就可以将fill
和color
比例映射到grouping2
。这使得只有一个图例受到附加geom_point
图层的影响。然后,您可以使用override.aes
中的guide_legend
修改图例以删除第二组的圈子。
我使用override.aes
来保留填充点shape
图例,因为使用可填充点会更改默认情况下形状图例的外观。
这是一个选项,其中周围的圆圈与点的颜色相同:
ggplot(data = x, aes(x = values1, y = values2, group = names)) +
geom_point(aes(shape = grouping1, fill = grouping2, colour = grouping2)) +
geom_line(alpha = 0.2) +
geom_point(data = x[x$grouping1 == 1 & x$grouping2 == 1,],
aes(values1, values2, color = grouping2),
shape = 1, size = 3 ) +
scale_shape_manual(values = c(21, 24) ) + # fillable shapes
guides(fill = guide_legend(override.aes = list(color = c("#F8766D", NA),
shape = 21) ),
shape = guide_legend(override.aes = list(fill = "black") ) )
一个略有不同的选项,保持圆点周围的黑色。但是,它也会在填充点上添加黑色轮廓(即,使&#34;笔划&#34;颜色变黑)。另一组的笔触颜色与填充颜色保持一致。
ggplot(data = x, aes(x = values1, y = values2, group = names)) +
geom_point(aes(shape = grouping1, fill = grouping2, colour = grouping2)) +
geom_line(alpha = 0.2) +
geom_point(data = x[x$grouping1 == 1 & x$grouping2 == 1,],
aes(values1, values2, color = grouping2),
shape = 21, size = 3 ) +
scale_shape_manual(values = c(21, 24) ) +
scale_color_manual(values = c("black", "#00BFC4") ) +
guides(fill = guide_legend(override.aes = list(color = c("black", NA),
shape = 21) ),
shape = guide_legend(override.aes = list(fill = "black") ) )