如何在 R 中连接极坐标上的特定属性?

How to connect specific attributes over polar coordinates in R?

我在我的图中突出显示了数据集中的特定活动(进食、休息和睡眠)。现在我想在我的极坐标上依次连接这些突出显示的点。

这是我的数据集:

Activity  Latitude  Longitude
Feeding   21.09542  71.06014
Resting   21.09564  71.06064
Sleeping  21.09619  71.06128
Walking   21.09636  71.06242
Walking   21.09667  71.06564
Resting   21.09483  71.06619

你能帮我解决这个问题吗?

好的,我从头开始:我原来的答案过于庞大和不灵活。

只需添加以下内容即可获取每个 Activity 的路径,无需过滤。

+ geom_path(aes(colour=ACTIVITY,x=Latitude,y=Longitude))

如果您只想绘制选定的活动:

+ geom_path(data=Data[Data$ACTIVITY %in% c("Sleeping","Resting"),],aes(colour=ACTIVITY,x=Latitude,y=Longitude))

所选活动将在 c(...) 向量中列出,每个名称都被引用。

更新:OP 澄清说他想连接任何固定点,这是通过 运行 以下内容实现的:

+ geom_path(data=Data[Data$ACTIVITY!="Walking",],colour="red",aes(x=Latitude,y=Longitude))

请注意,颜色=ACTIVITY 已从美学中移除,我们考虑所有固定点 (!="Walking") 来绘制路径。

结合两个答案的代码:

set.seed(1)
mydf=data.frame(Activity=sample(c("Walking","Walking","Walking","Walking","Walking","Resting","Feeding","Sleeping"),20,T),Latitude=rnorm(20,21,0.5),Longitude=rnorm(20,71,0.5))
mydf$Order=1:nrow(mydf)

# Plot
library(ggplot2)
ggplot(data=mydf)+
geom_point(aes(x=Latitude,y=Longitude,colour=Activity),size=5)+
geom_path(aes(x=Latitude,y=Longitude),size=1.2)+
geom_text(aes(x=Latitude,y=Longitude,label=Order))+
geom_path(data=mydf[mydf$Activity!="Walking",],colour="red",aes(x=Latitude,y=Longitude)) +
coord_polar(theta="y")

# Example dataframe    
set.seed(1)
mydf=data.frame(Activity=sample(c("Walking","Feeding","Resting","Sleeping"),20,T),Latitude=rnorm(20,21,0.5),Longitude=rnorm(20,71,0.5))
mydf$Order=1:nrow(mydf)

如果你想按顺序连接点而不考虑 activity,请执行以下操作(为了清楚起见,我添加了变量 mydf$Order 来标记点)。

# Plot
library(ggplot2)
ggplot(data=mydf)+
  geom_point(aes(x=Latitude,y=Longitude,colour=Activity))+
  geom_path(aes(x=Latitude,y=Longitude))+
  geom_text(aes(x=Latitude,y=Longitude,label=Order))+
  coord_polar(theta="y")

如果你想按活动连接点,请考虑 CMichael 的答案。