我想在R中创建一个条形图,并在两个不同的data.frame
中比较相同的值我的数据看起来像这样
First DF:
2004 2005 2006 unit region
1 1500 1000 2000 X region1
2 1000 2500 2800 Y region1
3 2000 2050 1900 X region2
4 2200 2100 2000 Y region2
etc.
第二次DF:
2004 2005 2006 unit region
1 5 10 12 PP region1
2 3 5 8 SS region1
3 8 12 11 PP region2
4 7 5 5 SS region2
etc.
我想要做的是视觉比较:
如果有人可以帮助我,我会非常感激,试图在一整天都这样做,而不是能够继续前进。
谢谢!!!
答案 0 :(得分:1)
2004 2005 2006 unit region
1500 1000 2000 X region1
1000 2500 2800 Y region1
2000 2050 1900 X region2
2200 2100 2000 Y region2
df1 <- read.table(con <- file("clipboard"), header = T)
2004 2005 2006 unit region
5 10 12 PP region1
3 5 8 SS region1
8 12 11 PP region2
7 5 5 SS region2
df2 <- read.table(con <- file("clipboard"), header = T)
# Barplot (clustered) - region1 unit X with the same region1 from second DF unit PP. Years (2004, 2005, 2006)
df1$df <- 1
df2$df <- 2
require(reshape2)
require(ggplot2)
df <- rbind(df1, df2)
df <- melt(df, id.vars=c("region", "unit", "df"))
ggplot(df[(df$region=="region1" & df$df == 1) | (df$region == "region1" & df$unit == "PP"),],
aes(variable, value)) +
geom_bar(aes(fill = factor(df)), position = "dodge", stat="identity")
# Line chart the same data as above
ggplot(df[(df$region=="region1" & df$df == 1) | (df$region == "region1" & df$unit == "PP"),],
aes(variable, value)) +
geom_line(aes(fill = factor(df)), stat="identity")
# Barplot (clustered) - a set of 10 regions with unit Y with the same 10 regions from second table unit SS. Years (2004, 2005, 2006) I would like to have a barplot (clustered barplot).
cat("For this one you'd need to provide a suitable example, as the current example has only 2 regions")