【问题标题】:creating a single colored point over a normal distribution in ggplot在ggplot中的正态分布上创建单个彩色点
【发布时间】:2014-10-31 21:31:49
【问题描述】:

我希望在 ggplot 中绘制正态分布,并在 @nrussell 的建议下使用了

ggplot(data.frame(x = c(-5, 5)), aes(x)) + stat_function(fun = dnorm)

我想知道是否有任何方法可以在 stat_function 的上下文中将单个彩色点直接分层到曲线上。例如,如果我想在 x 轴标记为 2 的位置放置一个点。

我已经尝试过geom_point,但这似乎更适合创建散点图:我似乎无法从stat_function 中导入美学来创建图层。

任何建议将不胜感激。

【问题讨论】:

    标签: r ggplot2 normal-distribution


    【解决方案1】:

    可能有一种方法可以使用另一个stat_function 层来做到这一点,但在玩了几分钟之后,似乎更容易使用geom_point 添加一个点:

    library(ggplot2)
    ##
    ggplot(
      data.frame(x = c(-5, 5)), 
      aes(x))+ 
      stat_function(fun = dnorm)+
      geom_point(
        data=data.frame(x=2,y=dnorm(2)),
        aes(x,y),
        color="red",
        size=4)
    ##
    

    【讨论】:

    • nrussell 是我今天发生的最好的事情。谢谢!
    【解决方案2】:

    使用annotate,只需指定x = 2, y = dnorm(2)。而不是试图从stat_function()中提取信息

    ggplot(data.frame(x = c(-5, 5)), aes(x)) +
      stat_function(fun = dnorm) +
      annotate(geom = "point", x = 2, y = dnorm(2), color = "red")
    

    注释最适合添加少量内容。要使用 geom_point(),您需要定义一个新的 data.frame,如果您想绘制多个点,这很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2021-11-12
      • 2018-12-10
      相关资源
      最近更新 更多