向分类数据的ggplot添加法线曲线

时间:2019-02-22 08:31:49

标签: r ggplot2 normal-distribution

已要求我创建一个图表,以可视化方式显示平均值为100和标准差为15的特定测试的人的得分。

下面的代码根据要求可视化的方式创建绘图。我特意评论了实际上添加得分和这些得分的置信区间的行,因为要求是覆盖一条均值达到峰值的正态曲线,而不管得分,得分数或图表范围如何。范数的峰值应始终到达图的顶部,因此在这种情况下,得分为4的顶部,法线的尾部应出现在图的极限处,在本例中为60和140(但可以是任何数字,例如40和160)。

library(ggplot2)

data <- data.frame(name = c("Score 1", "Score 2", "Score 3", "Score 4"),
                   value = c(89,110,116,76),
                   sem = c(1.1,0.86,1.6,1.3),
                   categories = c("mh","cog","mh","cog"))

z <- 1.96

# plot the main data
p <- ggplot(data, aes(x = name, y = value, ymin = value - (sem * z), ymax = value + (sem * z)))

# Add ranges
p <- p + coord_flip() +
  geom_rect(xmin = -Inf, xmax = Inf, ymin = 90, ymax = 110, alpha = 0.5, fill = "grey50") +
  geom_rect(xmin = -Inf, xmax = Inf, ymin = 85, ymax = 90, alpha = 0.5, fill = "grey70") +
  geom_rect(xmin = -Inf, xmax = Inf, ymin = 110, ymax = 115, alpha = 0.5, fill = "grey70") +
  geom_hline(yintercept = 100) +
  geom_hline(yintercept = 90) +
  geom_hline(yintercept = 110) +
  geom_hline(yintercept = 85) +
  geom_hline(yintercept = 115) +
  labs(title = "", x = "Test", y = "Score") +
  theme(plot.title = element_text(size = 16, face = "bold"),
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 14, face = "bold"))

# Place the breaks and limits on the plot
p <- p + scale_y_continuous(breaks = c(60,70,80,90,100,110,120,130,140), limits = c(60,140), expand = c(0,0))

# normal curve overlay here
# ?????

# Add representation of score and confidence interval
# p <- p + geom_point(aes(colour = categories)) +
#     geom_errorbar(aes(colour = categories),  width = .5)

# Add data labels to plot
# p <- p + geom_text(aes(label = value), size = 2) +
#   geom_text(aes(label = round(value - (sem * z), digits = 0), y = value - (sem * z)), size = 2, hjust = 1.25) +
#   geom_text(aes(label = round(value + (sem * z), digits = 0), y = value + (sem * z)), size = 2, hjust = -0.25)

# print the plot
p

我看过诸如this或以下解决方案:

plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5))

或:

p + stat_function(fun = dnorm, color="red", args=list(mean=100, sd=15))

但是在将它们集成到其余代码中时遇到麻烦。

美学方面还有很多其他操作,但我认为它们与本示例无关。

0 个答案:

没有答案