代码之家  ›  专栏  ›  技术社区  ›  Amp

在ggplot2中用离散轴上的堆叠垂直线显示分位数

  •  0
  • Amp  · 技术社区  · 8 月前

    我正在写一篇论文,并展示了很多发行版,当然,这意味着很多盒子和小提琴的情节。然而,这些可能很无聊,并不总是能显示我的数据的全部情况。我曾经看到一个图,它使用了堆叠在离散x值上的线条集,其中y轴是某个分位数的某个分位的值。这些分位数沿y轴的聚类将指示分布。例如:

    part of my goal

    我的问题是,我不喜欢连续的x轴。离散的x轴将使标记更容易,并启用我想添加到绘图中的其他功能。我知道我可以在帖子中完成所有的标签,但我认为在ggplot2中找到一种方法会很酷。我对Stack Overflow还是有点陌生,所以如果我需要澄清什么,请告诉我。

    构建上图的代码如下。请注意,我不关心数据格式或使用哪种geom_*,我只想要其中一个具有离散x轴的图。

    library(ggplot2)
    
    cutoffs <- seq(0, 1, by = 0.05)
    
    a <- sqrt(seq(1, 10000, length.out = 100))
    b <- (seq(1, 10, length.out = 100))^2
    c <- seq(1, 100, length.out = 100) 
    
    quant_data <- rbind(data.frame('class' = 'a',
                             'quantile' = quantile(a, probs = cutoffs)),
                  data.frame('class' = 'b',
                             'quantile' = quantile(b, probs = cutoffs)),
                  data.frame('class' = 'c',
                             'quantile' = quantile(c, probs = cutoffs)))
    
    num_data <- data.frame('class' = c(rep('a', 100), rep('b', 100), rep('c', 100)),
                      'val' = c(a, b, c))
    
    x_bases <- c(1, 2, 3)
    names(x_bases) <- c('a', 'b', 'c')
    
    quant_data$xmin <- x_bases[quant_data$class] - 0.2
    quant_data$xmax <- x_bases[quant_data$class] + 0.2
    
    num_data$xnum <- x_bases[num_data$class]
    
    ggplot()+
      geom_linerange(data = quant_data, mapping = aes(xmin = xmin, xmax = xmax, y = quantile), linewidth = 2)
    
    3 回复  |  直到 8 月前
        1
  •  2
  •   Axeman    8 月前

    这比你想象的要容易:

    quant_data$xnum <- x_bases[quant_data$class]
    
    ggplot(quant_data, aes(class, quantile)) +
      geom_linerange(aes(xmin = xnum - 0.2, xmax = xnum + 0.2))
    

    你一定要小心 xnum 是正确的,在某种意义上与因子水平相同 class 。所以也许用这个 after_stat() 方法,以动态获取正确的x坐标:

    ggplot(quant_data, aes(class, quantile)) +
      geom_linerange(aes(xmin = after_stat(x) - 0.2, xmax = after_stat(x) + 0.2))
    

    这肯定是我的首选。

    enter image description here

        2
  •  1
  •   Allan Cameron    8 月前

    我认为最简单的方法是使用垂直误差条,上下值相同。这允许您直接在分类轴上绘制值,并指定线条的宽度,而无需任何其他计算:

    library(ggplot2)
    
    cutoffs <- seq(0, 1, by = 0.05)
    
    a <- sqrt(seq(1, 10000, length.out = 100))
    b <- (seq(1, 10, length.out = 100))^2
    c <- seq(1, 100, length.out = 100) 
    
    quant_data <- rbind(data.frame('class' = 'a',
                                   'quantile' = quantile(a, probs = cutoffs)),
                        data.frame('class' = 'b',
                                   'quantile' = quantile(b, probs = cutoffs)),
                        data.frame('class' = 'c',
                                   'quantile' = quantile(c, probs = cutoffs)))
    
    ggplot(quant_data, aes(y = quantile)) +
      geom_errorbar(aes(x = class, ymax = quantile, ymin = quantile), 
                    linewidth = 2, width = 0.5) +
      theme_gray(20)
    

    enter image description here

        3
  •  0
  •   Josh Allen    8 月前

    如果将x轴标签视为因素,则应能解决问题

    library(ggplot2)
    library(dplyr)
    
    
    add_numeric = quant_data |>
      mutate(numeric_class = case_when(class == 'a' ~ 1,
                                      class == 'b' ~ 2,
                                     class == 'c' ~ 3))
    
     factor_labels  = ggplot()+
      geom_linerange(data = add_numeric, mapping = aes(x = as.factor(numeric_class), xmin = xmin, xmax = xmax, y = quantile), linewidth = 2)
    
    non_factor_labels = ggplot()+
      geom_linerange(data = add_numeric, mapping = aes(x = numeric_class, xmin = xmin, xmax = xmax, y = quantile), linewidth = 2)
    
    library(patchwork)
    
    factor_labels/non_factor_labels
    

    创建于2024年8月15日 reprex v2.1.1

    推荐文章