【问题标题】:R Add Frequency Distribution "Ticks" to ggplotR将频率分布“刻度”添加到ggplot
【发布时间】:2021-11-01 17:00:23
【问题描述】:

我知道这可能不是最好的例子,但我想在 x 轴上方添加 x 变量 (lstat) 的分布,如示例图中所示。我提供了 MWE 的数据和简单折线图的代码。

但是,到目前为止,我还没有关于如何实现这一点的线索。有什么提示吗?

library(ggplot2)
library(mlbench)

data(BostonHousing)

ggplot(data = BostonHousing) +
  geom_line(aes(x = lstat, y = medv))

Example Plot 表示 x 轴上方的频率刻度:

【问题讨论】:

    标签: r ggplot2 frequency-distribution


    【解决方案1】:

    如果要指定刻度线的高度,也可以使用geom_segment 函数。

    library(tidyverse)
    library(mlbench)
    
    data(BostonHousing)
    
    ggplot(data = BostonHousing) +
      geom_line(aes(x = lstat, y = medv)) +
      geom_segment(aes(x = lstat, xend = lstat, yend = 3, y = 0))
    
    

    【讨论】:

    • 这是一个非常好的方法 - 很好的答案!
    【解决方案2】:

    我相信您正在寻找的东西称为“地毯图”,您可以使用 ggplot 的 geom_rug() 函数来实现它,例如

    library(tidyverse)
    library(mlbench)
    
    data(BostonHousing)
    
    ggplot(data = BostonHousing) +
      geom_line(aes(x = lstat, y = medv)) +
      geom_rug(aes(x = lstat))
    

    reprex package (v2.0.1) 于 2021 年 9 月 3 日创建

    【讨论】:

    • 我也会给你+1,因为你的答案绝对正确!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2019-06-27
    相关资源
    最近更新 更多