首页 > 解决方案 > 在ggplot中注释方程

问题描述

curve(dnorm, from = -3, to = 3, n = 1000)
text(-2, 0.3, expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                                       " ", e^{
                                         frac(-(x - mu)^2, 2 * sigma^2)
                                       })), cex = 1.2)

如何在中添加上述表达式文本ggplot2?如果我使用表达式,那么我会收到错误消息,说输入必须是向量,而不是表达式。

ggplot(data.frame(x = c(-3, 3)), aes(x))+
  stat_function(fun = dnorm)+
  annotate("text", label = , 
           x = -2, 
           y = 0.3, 
           color = "black") 

标签: rggplot2

解决方案


尝试这个:

library(ggplot2)
#Label
Lab <- expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                         " ", e^{
                           frac(-(x - mu)^2, 2 * sigma^2)
                         }))
#Code
ggplot(data.frame(x = c(-3, 3)), aes(x))+
  stat_function(fun = dnorm)+
  annotate("text", label = Lab, 
           x = -2, 
           y = 0.3, 
           color = "black",parse=T) 

输出:

在此处输入图像描述


推荐阅读