首页 > 解决方案 > geom_jitter 点大于定义值的小提琴图

问题描述

我想创建一个 geom_jitter 点大于定义值的小提琴图。

下面是我用来获取此小提琴图的图片和代码,其中包含箱形图和数据点的 geom_jitter。有没有办法让超过 58 的点的 geom_jitter 并隐藏所有其余部分?

library(nycflights13)
library(ggplot2)
library(dplyr)

weather3 <- weather %>%
  filter(month == 3)

viol_plot <- ggplot(weather3, aes(x = factor(month), y = temp)) +
  geom_violin()

viol_plot + geom_boxplot(width=0.2) + geom_jitter(shape=20, position = position_jitter(0.05))

viol_plot + geom_jitter(shape=20, position = position_jitter(0.05))

小提琴情节图片

谢谢!

标签: rggplot2plotviolin-plot

解决方案


只需将数据geom_jitter()与 ggplot 定义的原始定义分开定义即可。

weather3 <- weather %>%
   filter(month == 3)

viol_plot <- ggplot(weather3, aes(x = factor(month), y = temp)) +
   geom_violin()

viol_plot + geom_boxplot(width=0.2) + 
   geom_jitter(data=weather3[weather3$temp>58,], shape=20, position = position_jitter(0.05), color="blue")

为了清晰起见,我将抖动的点涂成蓝色。

在此处输入图像描述


推荐阅读