在多个表和绘图之间统一重新排列列的方法?
Way to rearrange columns uniformly between multiple tables and plot?
我有 3 个 DF 按年份描述某些标签的频率。每个 DF 都有相同的列标题,除了一个缺少 1+ 列,因为频率为零,还有 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)
)
如果我现在绘制它们,颜色将不会代表每个 table 的相同列标题,因为 table 中的顺序不同。并且一个 table 具有不同的列数和行数。
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")
有没有办法重新组织 table,使所有列的顺序相同,从而 3 个图中的相同颜色对应于所有 table 的同一列?在我的数据中,有 10 多列。
TIA
也许这会让您更接近您想要的输出。
首先将 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"))
我有 3 个 DF 按年份描述某些标签的频率。每个 DF 都有相同的列标题,除了一个缺少 1+ 列,因为频率为零,还有 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)
)
如果我现在绘制它们,颜色将不会代表每个 table 的相同列标题,因为 table 中的顺序不同。并且一个 table 具有不同的列数和行数。
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")
有没有办法重新组织 table,使所有列的顺序相同,从而 3 个图中的相同颜色对应于所有 table 的同一列?在我的数据中,有 10 多列。
TIA
也许这会让您更接近您想要的输出。
首先将 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"))