使用 R 中的 ggplot2 按不同组对密度图进行分层
Stratifying a density plot by different groups using ggplot2 in R
我在 R 中有一个名为 x
的数据框,它有数百行。每一行都是一个人。我有两个变量,Height
,它是连续的,Country
,它是一个因子。我想绘制所有个人身高的平滑直方图。我想按 Country
对其进行分层。我知道我可以使用以下代码做到这一点:
library(ggplot2)
ggplot(x, aes(x=Height, colour = (Country == "USA"))) + geom_density()
这将来自美国的每个人都绘制为一种颜色 (true),将来自任何其他国家/地区的每个人绘制为另一种颜色 (false)。然而,我真正想做的是用一种颜色绘制来自美国的每个人,用另一种颜色绘制来自阿曼、尼日利亚和瑞士的每个人。我将如何调整我的代码来执行此操作?
我整理了一些数据来说明:
head(iris)
table(iris$Species)
df <- iris
df$Species2 <- ifelse(df$Species == "setosa", "blue",
ifelse(df$Species == "virginica", "red", ""))
library(ggplot2)
p <- ggplot(df, aes(x = Sepal.Length, colour = (Species == "setosa")))
p + geom_density() # Your example
# Now let's choose the other created column
p <- ggplot(df, aes(x = Sepal.Length, colour = Species2))
p + geom_density() + facet_wrap(~Species2)
编辑 以去除图中不需要的 "countries" ,只需将它们从图中使用的数据框中取出(注意带有颜色不完全匹配,但可以在数据框本身内更改):
p <- ggplot(df[df$Species2 %in% c("blue", "red"),], aes(x = Sepal.Length, colour = Species2))
p + geom_density() + facet_wrap(~Species2)
要覆盖这些线,只需取出 facet_wrap
:
p + geom_density()
我很喜欢上面的出色答案。这是我的模组。
df <- iris
df$Species2 <- ifelse(df$Species == "setosa", "blue",
ifelse(df$Species == "virginica", "red", ""))
homes2006 <- df
names(homes2006)[names(homes2006)=="Species"] <- "ownership"
homes2006a <- as.data.frame(sapply(homes2006, gsub,
pattern ="setosa", replacement = "renters"))
homes2006b <- as.data.frame(sapply(homes2006a, gsub, pattern = "virginica",
replacement = "home-owners"))
homes2006c <- as.data.frame(sapply(homes2006b, gsub, pattern = "versicolor",
replacement = "home-owners"))
##somehow sepal-length became a factor column
homes2006c[,1] <- as.numeric(homes2006c[,1])
library(ggplot2)
p <- ggplot(homes2006c, aes(x = Sepal.Length,
colour = (ownership == "home-owners")))
p + ylab("number of households") +
xlab("monthly income (NIS)") +
ggtitle("income distribution by home ownership") +
geom_density()
我在 R 中有一个名为 x
的数据框,它有数百行。每一行都是一个人。我有两个变量,Height
,它是连续的,Country
,它是一个因子。我想绘制所有个人身高的平滑直方图。我想按 Country
对其进行分层。我知道我可以使用以下代码做到这一点:
library(ggplot2)
ggplot(x, aes(x=Height, colour = (Country == "USA"))) + geom_density()
这将来自美国的每个人都绘制为一种颜色 (true),将来自任何其他国家/地区的每个人绘制为另一种颜色 (false)。然而,我真正想做的是用一种颜色绘制来自美国的每个人,用另一种颜色绘制来自阿曼、尼日利亚和瑞士的每个人。我将如何调整我的代码来执行此操作?
我整理了一些数据来说明:
head(iris)
table(iris$Species)
df <- iris
df$Species2 <- ifelse(df$Species == "setosa", "blue",
ifelse(df$Species == "virginica", "red", ""))
library(ggplot2)
p <- ggplot(df, aes(x = Sepal.Length, colour = (Species == "setosa")))
p + geom_density() # Your example
# Now let's choose the other created column
p <- ggplot(df, aes(x = Sepal.Length, colour = Species2))
p + geom_density() + facet_wrap(~Species2)
p <- ggplot(df[df$Species2 %in% c("blue", "red"),], aes(x = Sepal.Length, colour = Species2))
p + geom_density() + facet_wrap(~Species2)
facet_wrap
:
p + geom_density()
我很喜欢上面的出色答案。这是我的模组。
df <- iris
df$Species2 <- ifelse(df$Species == "setosa", "blue",
ifelse(df$Species == "virginica", "red", ""))
homes2006 <- df
names(homes2006)[names(homes2006)=="Species"] <- "ownership"
homes2006a <- as.data.frame(sapply(homes2006, gsub,
pattern ="setosa", replacement = "renters"))
homes2006b <- as.data.frame(sapply(homes2006a, gsub, pattern = "virginica",
replacement = "home-owners"))
homes2006c <- as.data.frame(sapply(homes2006b, gsub, pattern = "versicolor",
replacement = "home-owners"))
##somehow sepal-length became a factor column
homes2006c[,1] <- as.numeric(homes2006c[,1])
library(ggplot2)
p <- ggplot(homes2006c, aes(x = Sepal.Length,
colour = (ownership == "home-owners")))
p + ylab("number of households") +
xlab("monthly income (NIS)") +
ggtitle("income distribution by home ownership") +
geom_density()