我有三个数据框,按年份描述某些标签的频率。每个 DF 具有相同的列标题,但其中一个缺少 1+ 列(因为频率为零)和一行,因为该年没有频率。
df1 <- data.frame(Year = c("2000", "2001", "2002", "2003", "2004"),
Country = c(1, 4, 5, 2, 26),
Flag = c(23, 2, 4, 2, 5),
Anthem = c(3, 7, 8, 2, 3)
)
df2 <- data.frame(Year = c("2000", "2001", "2002", "2003", "2004"),
Country = c(1, 4, 5, 2, 26),
Anthem = c(23, 2, 4, 2, 5),
Flag = c(3, 7, 8, 2, 3)
)
df3missing <- data.frame(Year = c("2001", "2002", "2003", "2004"),
Anthem = c(4, 5, 2, 26),
Country = c(2, 4, 2, 5)
)
如果我现在绘制它们,颜色将不会代表每个表的相同列标题,因为表中的顺序不同。并且一张表的列数和行数不同。
df11 <- melt(df1, id.vars="Year")
df22 <- melt(df2, id.vars="Year")
df3missing2 <- melt(df3missing, id.vars="Year")
ggplot(df11, aes(x = Year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity")
ggplot(df22, aes(x = Year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity")
ggplot(df3missing2, aes(x = Year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity")
有没有办法重新组织表格,使所有列都按相同的顺序排列,以便 3 个图中的相同颜色对应于所有表格的同一列?在我的数据中,有 10 多列。
也许这会让您更接近您想要的输出。
首先将
NA
添加到缺失的帧中,然后获取与 melt
一起使用的列顺序。这会产生某种程度对称的结果。
最后,手动选择自己的配色方案,以获得相同变量的相同颜色。
library(reshape2)
library(ggplot2)
df3 <- df3missing
df3$Flag <- NA
cc <- colnames(df1)
dff1 <- melt(df1[,cc])
dff2 <- melt(df2[,cc])
dff3 <- melt(df3[,cc], id.vars="Year")
ggplot(dff1) +
geom_bar( aes( Year, value, fill=variable ), stat = "identity" ) +
scale_fill_manual("legend", values = c("Country" = "red", "Flag" = "orange", "Anthem" = "blue"))
ggplot(dff2) +
geom_bar( aes( Year, value, fill=variable ), stat = "identity" ) +
scale_fill_manual("legend", values = c("Country" = "red", "Flag" = "orange", "Anthem" = "blue"))
ggplot(dff3) +
geom_bar( aes( Year, value, fill=variable ), stat = "identity" ) +
scale_fill_manual("legend", values = c("Country" = "red", "Flag" = "orange", "Anthem" = "blue"))