1

我正在寻找一种解决方案,以根据设置为因子的定义顺序对 geom_path 的路径进行排序。

我一直在使用前两个 PCA 维度。使用library("factoextra")andlibrary("FactoMineR")我用fviz_pca_ind().

raa_male <- fviz_pca_ind(
  pca.data,
  fill.ind = male_raa.df$Season,
  pointsize = male_raa.df$BRI,
  pointshape = 21,
  repel = TRUE
)

数据按个人排列(由文本标签显示)。

在此处输入图像描述

使用geom_path我想连接同一个人的点,按因子季节的路径顺序,c(Autumn, Winter, Spring). 但是我很难做到这一点

male_raa.df$Season <- factor(male_raa.df$Season, levels = c("Autumn", "Winter", "Spring"))

raa_male +
  geom_path(
    arrow = arrow(angle = 15, ends = "last", type = "closed"),
    alpha = 0.2,
    aes(group = male_raa.df$TagID)
  )

在此处输入图像描述

设置为因子的排序似乎不会转化为 geom_path 路径的排序。

4

1 回答 1

1

这是一个较小的示例,比较原始版本和排序版本。geom_path是根据数据中出现的顺序排序的,所以如果你想让它反映一个有序的因素,首先按那个排序。

df1 <- data.frame(x = 1:3,
                  y = c(1,2,1),
                  season = LETTERS[1:3])
df1$season = factor(df1$season, levels = c("B","C","A"))

library(ggplot2); library(dplyr)
ggplot(df1, aes(x,y, label = season)) +
    geom_path(arrow = arrow(angle = 15, ends = "last", type = "closed")) +
    geom_label()

在此处输入图像描述

ggplot(df1 %>% arrange(season), aes(x,y, label = season)) +
    geom_path(arrow = arrow(angle = 15, ends = "last", type = "closed"),) +
    geom_label()

在此处输入图像描述

于 2021-11-18T07:40:36.347 回答