特别强调通过在ggplot中圈出来进行观察

时间:2017-10-27 07:34:05

标签: ggplot2 geometry

我有一个包含31个级别的分类数据的数据集。我想用ggplot在散点图中显示它们的分布,但我想特别强调一些数据点,比如这里的红色圆圈:

enter image description here

我倾向于在数据点[x = 10,y = 6]处的观察点周围有一个红色虚线圆圈。优选地,该解决方案是可持续的,但是手动盘旋建议也是欢迎的:)。这是我的剧本

library(ggplot2)

#dataframe
df1 <- data.frame(name = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e"),
             n = rep(1:31, 1),
             value = c(3, 2, 5, 1, 1, 6, 7, 9, 8, 6, 10, 11, 11, 11, 13, 15, 17, 16, 18, 18, 20, 20, 23, 22, 22, 23, 25, 26, 28, 29, 29))

#set correct data type
df1$name <- as.factor(df1$name)

#produce color vector
color <- grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] 
col_sample <- sample(color, 31)
col_sample <- as.vector(col_sample)

#scatterplot
median_scatter <- ggplot(data = df1,
                     aes(x = n, 
                         y = value,
                         colour = name))
median_scatter + 
  geom_point() +
  scale_colour_manual(values=col_sample)

1 个答案:

答案 0 :(得分:1)

您可以定义数据的子集,即df1[df1$name == "j", ],它对应于在另一个geom_point上绘制的兴趣点,并选择一个开放圆形的形状,并定义颜色,大小和按你的喜好划水。

median_scatter + 
  geom_point() +
  scale_colour_manual(values=col_sample) +
  geom_point(data=df1[df1$name == "j", ], colour="red", shape=1, size=4, stroke=1.5)

不幸的是,没有虚线圆形可用。

enter image description here