本文介绍了在ggplot的正态分布上创建一个单一的彩色点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想在ggplot中绘制正态分布,在@nrussell的建议下,我使用了

I was looking to plot a normal distribution in ggplot, and at the suggestion of @nrussell I have used

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

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

I am wondering if there is any way to, within the context of stat_function, layer a single colored point directly onto the curve. For example, if I wanted to put a dot where the x axis is marked 2.

我已经尝试过geom_point,但这似乎在创建散点图中更好:我似乎无法从stat_function的美学角度出发来创建图层.

I have experimented with geom_point but this appears to be better at creating scatterplots: I can't seem to pipe in the aesthetics from the stat_function for the layer be created.

任何建议将不胜感激.

推荐答案

也许有一种方法可以在另一个stat_function层上进行此操作,但是在试用了几分钟之后,仅使用添加一个点:

There may be a way to do this with another stat_function layer, but after playing around with it for a few minutes it seemed easier to just use geom_point to add a single 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)
##

这篇关于在ggplot的正态分布上创建一个单一的彩色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:39