R 图中的图例具有核密度、正态密度和直方图?
Legend in R plot that has kernel density, normal density and a histogram?
我目前是 R 的初学者,对如何为我正在处理的三个图插入图例有疑问。我正在使用 R 上名为 iris 的内置数据集。对于我认为为了让传说出现而应该起作用的东西,我有一些想法,但事实并非如此,因为只有情节出现。我附上了下面这些地块的图片。有人可以告诉我我需要做什么才能让传说出现在各自的情节上吗?先感谢您。
setosa_length <- iris$Sepal.Length[iris$Species == "setosa"]
hist(setosa_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(setosa_length), sd(setosa_length)))
lines(x, y, col="red")
lines(density(setosa_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
versicolor_length <- iris$Sepal.Length[iris$Species == "versicolor"]
hist(versicolor_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(versicolor_length), sd(versicolor_length)))
lines(x, y, col="red")
lines(density(versicolor_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
virginica_length <- iris$Sepal.Length[iris$Species == "virginica"]
hist(virginica_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(virginica_length), sd(virginica_length)))
lines(x, y, col="red")
lines(density(virginica_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
我强烈建议学习一些 tidyverse,因为它可以解决大部分问题,并且代码的可读性更高。
library(tidyverse)
# calculate the normal densities for the three species
x <- seq(4, 8, length.out=100)
iris.norm <- group_by(iris, Species) %>%
summarize(mean = mean(Sepal.Length),
sd = sd(Sepal.Length)) %>%
mutate(data = map2(mean, sd, ~ data.frame(Sepal.Length = x,
density = dnorm(x, .x, .y)))) %>%
unnest()
# plot histograms and densities on top of each other
ggplot(iris) +
geom_histogram(aes(x = Sepal.Length, y = ..density..),
color = "black", fill = "white", bins = 8) +
geom_line(aes(x = Sepal.Length, color = "Kernel Density"),
stat = "density") +
geom_line(data = iris.norm,
aes(x = Sepal.Length, y = density, color = "Normal Density")) +
facet_wrap(~Species, ncol = 1) +
theme_minimal()
我目前是 R 的初学者,对如何为我正在处理的三个图插入图例有疑问。我正在使用 R 上名为 iris 的内置数据集。对于我认为为了让传说出现而应该起作用的东西,我有一些想法,但事实并非如此,因为只有情节出现。我附上了下面这些地块的图片。有人可以告诉我我需要做什么才能让传说出现在各自的情节上吗?先感谢您。
setosa_length <- iris$Sepal.Length[iris$Species == "setosa"]
hist(setosa_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(setosa_length), sd(setosa_length)))
lines(x, y, col="red")
lines(density(setosa_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
versicolor_length <- iris$Sepal.Length[iris$Species == "versicolor"]
hist(versicolor_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(versicolor_length), sd(versicolor_length)))
lines(x, y, col="red")
lines(density(versicolor_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
virginica_length <- iris$Sepal.Length[iris$Species == "virginica"]
hist(virginica_length, freq=FALSE)
x <- seq(4, 8, length.out=100)
y <- with(iris, dnorm(x, mean(virginica_length), sd(virginica_length)))
lines(x, y, col="red")
lines(density(virginica_length), col="blue")
legend(1, 95, legend=c("Normal Density", "Kernel Density"), col=c("red",
"blue"), lty=1:2, cex=0.5)
我强烈建议学习一些 tidyverse,因为它可以解决大部分问题,并且代码的可读性更高。
library(tidyverse)
# calculate the normal densities for the three species
x <- seq(4, 8, length.out=100)
iris.norm <- group_by(iris, Species) %>%
summarize(mean = mean(Sepal.Length),
sd = sd(Sepal.Length)) %>%
mutate(data = map2(mean, sd, ~ data.frame(Sepal.Length = x,
density = dnorm(x, .x, .y)))) %>%
unnest()
# plot histograms and densities on top of each other
ggplot(iris) +
geom_histogram(aes(x = Sepal.Length, y = ..density..),
color = "black", fill = "white", bins = 8) +
geom_line(aes(x = Sepal.Length, color = "Kernel Density"),
stat = "density") +
geom_line(data = iris.norm,
aes(x = Sepal.Length, y = density, color = "Normal Density")) +
facet_wrap(~Species, ncol = 1) +
theme_minimal()