r - 使用 R 在 ggplot2 中添加图例
问题描述
在绘制样本大小与功率的关系图时,我在用 ggplots 添加 R 中的图例时遇到问题。我试过 guide_legend() 但它失败了。非常感谢你的帮助。
# Load the library and input the data
library(ggplot2)
n <- 2:10
control <- rep(150, 4)
infected <- c(150, 170, 200, 250)
all <- c(control, infected)
sigma <- c(35, 40, 45)
# Compute the population mean
mu <- mean(all)
# Compute the sum of the tau squared
tau2 <- sum((all-mu)^2)
# Compute the gamma
gamma.1 <- (n*tau2)/(sigma[1]^2)
gamma.2 <- (n*tau2)/(sigma[2]^2)
gamma.3 <- (n*tau2)/(sigma[3]^2)
# Compute the power
power.1 <- 1-pf(qf(.95, 7, 16), 7, 16, gamma.1)
power.2 <- 1-pf(qf(.95, 7, 16), 7, 16, gamma.2)
power.3 <- 1-pf(qf(.95, 7, 16), 7, 16, gamma.3)
# Plot the power vs the sample size
data <- data.frame(n, power.1, power.2, power.3)
ggplot(data, aes(x = n)) +
geom_point(aes(y=power.1), size = 3.5, color = "blue") +
geom_line(aes(y=power.1), size = 0.5) +
geom_point(aes(y=power.2), size = 3.5, color = "red") +
geom_line(aes(y=power.2), size = 0.5) +
geom_point(aes(y=power.3), size = 3.5, color = "black") +
geom_line(aes(y=power.3), size = 0.5) +
xlab("Sample Sizes") +
ylab("Power") +
ggtitle("Power versus Sample size")
解决方案
推荐阅读
- discord.js - 是否可以跟踪用户在语音频道中的时间?
- python - Itertools groupby 按两个值组织字典列表
- angular - 通过 NGC 编译 TypeScript 源错误:超出最大调用堆栈大小 - 如何为 Angular 应用启用调试
- css - Text-Shadow with transparent text color
- node.js - Nodejs:如何读取和遍历ServerResponse?(或者,如何读取客户端 URL 参数?)
- terraform - DestinationAddressPrefix/DestinationAddressPrefixes 的 Azure Terraform NSG 规则创建错误为“AzureMonitor”
- wordpress - 徽标未显示且更改未正确更新
- java - 如何获得价值是选择中的brand.brandName
- kubernetes - 负载测试 kubernetes 服务(使用 istio 服务网格)
- sql - 如何使用 SQL 计算表的频率百分比?