1

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 !!!

sampak
  • 155
  • 1
  • 2
  • 12
  • You should at least share your data in a [reproducible format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also show what you tried and ask specifically about where you are getting stuck. The question kind of reads like you are assigning us homework now. You should probably start by getting your data into a more [tidy format](http://vita.had.co.nz/papers/tidy-data.pdf) to make it easier to use with ggplot. – MrFlick Aug 06 '16 at 20:23

1 Answers1

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

enter image description here

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • 1
    Thanks! Worked perfect for me! Didn't know I had to transform the df, makes much more sense now! – sampak Aug 06 '16 at 21:36