【发布时间】:2019-01-16 09:59:53
【问题描述】:
拥有以下示例数据集:
set.seed(20)
N <- 20
df1 <- data.frame(x = rnorm(N),
y = rnorm(N),
grp = paste0('grp_', sample(1:500, N, T)),
lab = sample(letters, N, T))
# x y grp lab
# 1 1.163 0.237 grp_104 w
# 2 -0.586 -0.144 grp_448 y
# 3 1.785 0.722 grp_31 m
# 4 -1.333 0.370 grp_471 z
# 5 -0.447 -0.242 grp_356 o
我想绘制所有点,但只标记它们的子集(例如,那些df1$x>0)。当我对geom_point 和geom_text 使用相同的color=grp 美学时,它工作正常:
ggplot(df1, aes(x=x,y=y,color=grp))+
geom_point(size=4) +
geom_text(aes(label=lab),data=df1[df1$x>1,],size=5,hjust=1,vjust=1)+
theme(legend.position="none")
但如果我想将点设计更改为fill=grp,标签的颜色将不再匹配:
ggplot(df1, aes(x=x,y=y))+
geom_point(aes(fill=grp),size=4,shape=21) +
geom_text(aes(label=lab,color=grp),data=df1[df1$x>1,],size=5,hjust=1,vjust=1)+
theme(legend.position="none")
我知道调色板是不同的,因为子集的级别与整个数据集的级别不同。但是,使用相同调色板强制执行的最简单解决方案是什么?
【问题讨论】: