簡體   English   中英

geom_path 分組和按分類因子排序的路徑

[英]geom_path grouped and path ordered by categorical factor

我正在尋找一種解決方案,以根據設置為因子的定義順序對 geom_path 的路徑進行排序。

我一直在使用前兩個 PCA 維度。 使用library("factoextra")library("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 路徑的排序。

這是一個較小的示例,比較原始版本和排序版本。 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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM