如何根据范围为geom点指定颜色

问题描述 投票:0回答:1

我有一个名为locs的数据框。 head(locs)是:

      Long      Lat variable1
1 71.61990 33.94370        13
2 71.74278 34.14943        13
3 72.00248 34.00935        52
4 72.03998 34.20004        11
5 72.47015 34.12015        12
6 71.43268 33.60269         0

我怎样才能创建一系列variable1?此外,我如何根据范围分配colors

我试过的代码:

base_world +
  geom_point(data=locs, 
             aes(x=Long, y=Lat), colour="Deep Pink", 
             fill="Pink",pch=21, size=3, alpha=I(0.7))

实际上,我的数据帧locs有123个观测值

r ggplot2
1个回答
0
投票

您可以使用cut()将变量划分为由您定义的范围。

locs$new_var <- cut(locs$variable1, breaks=c(0, 9, 19), labels=c(0-9, 9-19, 19+))

创建新变量后,将其用于颜色aes()

ggplot(locs, aes(x=Long,y=Lat,color=new_var)) + # or fill=new_var
    geom_point()
© www.soinside.com 2019 - 2024. All rights reserved.