繁体   English   中英

在 R 中使用 ggplot2 堆叠/抖动重叠点

[英]stack/jitter overlapping points using ggplot2 in R

我有一个连续变量,其中大多数观察结果都集中在光谱的一端。 我想在一个有点像这样的图形中显示它:

我想复制的例子

我用 position_jitter 试过了:

ver_ocupacoes |> 
ggplot(mapping = aes(x = n, y = 0))+ 
geom_point(alpha = 0.5, position = position_jitter(width = 0, height = 10, seed = 1))

并得到了这个: jitter_example

还尝试制作 geom_dotplot 代替:

ver_ocupacoes |>
  ggplot(mapping = aes(x = n))+
  geom_dotplot(stackdir = "center",
               stackratio = 0.5,
               dotsize = 0.2,
               method="dotdensity",
               stackgroups = T,
               binpositions="all")

结果是这样的:

点图示例

所以两个都不是我想要的。 关于如何像第一个示例中那样围绕中心轴抖动点的任何想法?

使用geom_dotplot ,您可以使用height = 0且宽度大约等于 binwidth 的position_jitter 从外观上看,将 binwidth 设置得更窄一些也会有所帮助。

ver_ocupacoes |>
ggplot(mapping = aes(x = n))+
  geom_hline(yintercept = 0) +
  geom_dotplot(stackdir = "center",
               stackratio = 0.5,
               dotsize = 1,
               method ="dotdensity",
               stackgroups = TRUE,
               binpositions = "all",
               binwidth = 5000,
               position = position_jitter(width = 5000, height = 0),
               color = 'red4', fill = 'red') +
  scale_x_continuous(labels = scales::comma) +
  theme_minimal(base_size = 20) +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

在此处输入图像描述


使用的数据

显然,我们没有您的数据,但以下似乎是一个合理的近似值,并在上面的示例中使用:

set.seed(1)
ver_ocupacoes <- data.frame(n = 1e5 * rexp(200, 2)^2.5/3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM