我有两条要绘制的ND曲线-我该怎么做?
#meand and SDs of Gen 1 and 7
mean_gen1 <- 1.241
SD_gen1 <- 1.005
mean_gen7 <- 1.287
SD_gen7 <- 1.386
x_gen1 <- seq(-4, 4, length = 1000) * SD_gen1 + mean_gen1
x_gen7 <- seq(-4, 4, length = 1000) * SD_gen7 + mean_gen7
y_gen1 <- pnorm(x_gen1, mean_gen1, SD_gen1)
y_gen7 <- pnorm(x_gen7, mean_gen7, SD_gen7)
#plots
gen1_plot <- plot(x_gen1, y_gen1, type="n", xlab = "Height (Meters)", ylab = "", main = "Distribution of Pokemon Heights GEN1", axes = TRUE)+ lines(x_gen1, y_gen1)
gen7_plot <- plot(x_gen7, y_gen7, type="n", xlab = "Height (Meters)", ylab = "", main = "Distribution of Pokemon Heights GEN7", axes = TRUE)+ lines(x_gen7, y_gen7)
答案 0 :(得分:1)
我不认为您的图将保存在gen1_plot
和gen7_plot
变量中。但是一般的基本R绘图语法是将plot()
替换为gen7 lines()
调用,如下所示:
plot(x_gen1, y_gen1, type="l",
xlab = "Height (Meters)",
ylab = "",
main = "Distribution of Pokemon Heights",
axes = TRUE)
lines(x_gen7, y_gen7, col = 2)
看起来像这样:
由于您的问题是用ggplot2标记的,因此我还将为ggplot提供一个选项:
library(ggplot2)
# Put all the data you have in one data.frame
df <- data.frame(x = c(x_gen1, x_gen7),
y = c(y_gen1, y_gen7),
gen = rep(c("gen1", "gen7"), c(length(x_gen1), length(x_gen7))))
# Plot away
ggplot(df, aes(x, y, colour = gen)) +
geom_line() +
scale_x_continuous(name = "Height (Meters)") +
ggtitle("Distribution of Pokemon Heights")
看起来像这样: