微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将点添加到 geom_density_ridges 为具有少量观察的组

如何解决将点添加到 geom_density_ridges 为具有少量观察的组

我喜欢使用 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 plot,missing observations for low-sample-size group (May)

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

解决方法

只需向该函数添加一个单独的 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)

Ridgeline plot with points added for categories deficient in observations necessary to produce density ridge

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。