R ggplot2条形图两个数据帧比较
我想在R中创建一个条形图,并比较两个不同data.frame中的相同值 我的数据是这样的 第一个DF:R ggplot2条形图两个数据帧比较,r,ggplot2,bar-chart,linechart,R,Ggplot2,Bar Chart,Linechart,我想在R中创建一个条形图,并比较两个不同data.frame中的相同值 我的数据是这样的 第一个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 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.
我想做的是视觉对比:
2004 2005 2006单位区域
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")
1500 1000 2000 X区域1
1000 2500 2800 Y区域1
2000 2050 1900 X区域2
2200 2100 2000 Y地区2
df1您至少应该在一个数据库中共享您的数据。同时展示你所做的尝试,并明确询问你在哪里陷入困境。这个问题有点像你现在给我们布置的作业。您可能应该首先将您的数据放入一个more中,以便更易于使用ggplot。谢谢!这对我来说太完美了!我不知道我必须转换df,现在更有意义了!