使用R中的ggplot2按不同组对密度图进行分层

使用R中的ggplot2按不同组对密度图进行分层,r,ggplot2,histogram,categories,density-plot,R,Ggplot2,Histogram,Categories,Density Plot,我在R中有一个名为x的数据帧,它有数百行。每一排都是一个人。我有两个变量,高度,这是连续的,还有国家,这是一个因素。我想画一个平滑的柱状图,显示所有个体的高度。我想按国家对其进行分层。我知道我可以通过以下代码实现这一点: library(ggplot2) ggplot(x, aes(x=Height, colour = (Country == "USA"))) + geom_density() 这会将来自美国的每个人绘制为一种颜色(真),将来自任何其他国家的每个人绘制为另一种颜色(假)。然而,

我在R中有一个名为
x
的数据帧,它有数百行。每一排都是一个人。我有两个变量,
高度
,这是连续的,还有
国家
,这是一个因素。我想画一个平滑的柱状图,显示所有个体的高度。我想按
国家对其进行分层。我知道我可以通过以下代码实现这一点:

library(ggplot2)
ggplot(x, aes(x=Height, colour = (Country == "USA"))) + geom_density()

这会将来自美国的每个人绘制为一种颜色(真),将来自任何其他国家的每个人绘制为另一种颜色(假)。然而,我真正想做的是用一种颜色描绘每个来自美国的人,用另一种颜色描绘每个来自阿曼、尼日利亚和瑞士的人。我将如何调整我的代码来实现这一点?

我制作了一些数据以供说明:

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

我很喜欢完成上面的优秀答案。这是我的MOD

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()

df您可以尝试在
x
中创建一个列,类似于:
country2,据我所知,您希望每个国家使用一种密度,美国使用一种颜色,其他国家使用另一种颜色,对吗?然后在aes函数中添加group=Country。@如果我想在同一个图上绘制两个完全不同的平滑直方图,该怎么办?有没有办法只绘制第一个,将其留在屏幕上,然后以不同的颜色覆盖另一个?不确定您的确切意思-根据下面的绘图进行评论。如果我想在同一个绘图上覆盖绿色和蓝色线,该怎么办?完全消除红线?(指线条本身的颜色)
p <- ggplot(df[df$Species2 %in% c("blue", "red"),], aes(x = Sepal.Length, colour = Species2))
p + geom_density() + facet_wrap(~Species2)
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()