0

我喜欢使用 geom_density_ridges(),每个组也包含单独的点。然而,一些组的样本量较小(例如 n=1 或 2)排除了密度脊的产生。对于这些组,我希望能够绘制现有观测值的位置 - 即使不会显示概率密度函数。

在此示例中,我希望能够在适当的线上绘制 May 的 2 个数据点。

    library(tidyverse)
    library(ggridges)
    
    data("lincoln_weather")
    
    #pull weather from all months that are NOT May
    lincoln_weather_nomay<-lincoln_weather[which(lincoln_weather$Month!="May"),]
    
    #pull weather just from May
    lincoln_weather_may<-lincoln_weather[which(lincoln_weather$Month=="May"),]
    
    #recombine, keeping only the first two rows for the May dataset
    new_weather<-rbind(lincoln_weather_nomay,lincoln_weather_may[c(1:2),])
    
    ggplot( new_weather, aes(x=`Min Temperature [F]`,y=Month,fill=Month))+
      geom_density_ridges(alpha = 0.5,jittered_points = TRUE, point_alpha=1,point_shape=21) + 
      labs(x="Average temperature (F)",y='')+ 
      guides(fill=FALSE,color=FALSE)

geom_density_ridges 图,缺少低样本组的观测值(5 月)

如何将 May 观测值的点添加到适当的位置(即 May 槽)和沿 x 轴的适当位置?

4

1 回答 1

0

只需向该函数添加一个单独geom_point()的调用,您可以在其中对数据进行子集化以仅包含对先前未绘制的类别的观察。您可以应用任何常规自定义来“匹配”为其他类别绘制的点,或者使这些点“突出”。

ggplot( new_weather, aes(x=`Min Temperature [F]`,y=Month,fill=Month))+
  geom_density_ridges(alpha = 0.5,jittered_points = TRUE, point_alpha=1,point_shape=21) + 
  geom_point(data=subset(new_weather, Month %in% c("May")),
             aes(),shape=13)+
  labs(x="Average temperature (F)",y='')+ 
  guides(fill=FALSE,color=FALSE)

脊线图,为缺乏产生密度脊所必需的观察的类别添加了点

于 2021-01-29T20:54:21.000 回答