sampak sampak - 3 months ago 26
R Question

ggplot2 barplot two data frames comparison

I wanted to create a barplot in R and compare the same value in two different data.frame

My data looks like this

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.


Second 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.


what I wanted to do is a visual comparison of:


  1. Barplot (clustered) - region1 unit X with the same region1 from second DF unit PP. Years (2004, 2005, 2006)

  2. Line chart the same data as above

  3. 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).



If anyone can help me I would much appreciate, trying to do it for the entire day, not being able to move ahead.

Thanks !!!

Answer
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")

enter image description here

Comments