在R中的ggplot中使用stat_ecdf

问题描述 投票:1回答:1

我正在尝试复制类似的图形

ggplot2_ecdf

我的数据看起来像这样

  Category Value
  A          2
  A          3
  A          4
  A          2
  A          4
  B          2
  B          1
  B          6
  C          1
  C          2
  C          3
  C          3

我想以类别为X轴,以y轴为值来绘制分布。由于其中一些具有相似的值,因此使用stat_ecdf()可以很好地可视化类别的曲线分布,以水平移动相似点(类似于链接中的图)。

我在ggplot中使用了beeswarm图,但想使用stat_ecdf以获得位移分布(每个条目显示为每个类别的点)。并以红色添加中线。

我尝试过的

a <- ggplot(df, aes(x=Category, y=value)) +
stat_ecdf()+
scale_y_continuous() +
theme_light() +
theme(axis.text.x = element_text(angle = 90)) +
xlab('category') +
ylab('values')
a   
r ggplot2
1个回答
0
投票

我今天的时间有些限制,但这也许可以为您指明正确的方向。

a <- ggplot(data = df,
            aes(x = value)) +
       stat_ecdf(geom = "point",
                 size = 1,
                 pad = FALSE) +
       xlab("category") +
       ylab("values") +
       facet_wrap(~ Category,
                  scales = "free_x",
                  strip.position = "bottom") +
       coord_cartesian(clip = "off") +
       theme_minimal() +
       theme(axis.text.x = element_blank(),
            panel.grid.minor = element_blank(),
            panel.grid.major = element_blank())

a   

更新:我玩了一些。希望这看起来更好。

© www.soinside.com 2019 - 2024. All rights reserved.