0

I have the following data set:

Location    Type    FromDate    ToDate  1   2   3   4   5
  A          1        12-Jul    13-Jul  2   4   0   1   2
  A          2        12-Jul    13-Jul  0   0   1   4   1
  B          1        12-Jul    13-Jul  0   1   1   3   1
  B          2        12-Jul    13-Jul  1   0   0   0   1
  C          1        12-Jul    13-Jul  2   3   1   5   0
  C          2        12-Jul    13-Jul  3   3   1   0   0

How can I create a bar graph in R for each location, including both types 1 and 2, during days 1 to 5?

Jaap
  • 81,064
  • 34
  • 182
  • 193

2 Answers2

1

A slightly alternative solution which instead of using reshape2 and plyr uses dplyr and tidyr. The latter combination makes use of piping which is becoming more popular.

First read the data:

df <- read.table(header=TRUE, text="Location    Type    FromDate   ToDate 1   2   3   4   5
A          1        12-Jul    13-Jul  2   4   0   1   2
A          2        12-Jul    13-Jul  0   0   1   4   1
B          1        12-Jul    13-Jul  0   1   1   3   1
B          2        12-Jul    13-Jul  1   0   0   0   1
C          1        12-Jul    13-Jul  2   3   1   5   0
C          2        12-Jul    13-Jul  3   3   1   0   0")
# remove the X-es which are put in front of the days
names(df) <- gsub("X","",names(df))

Load the needed libraries:

library(dplyr)
library(tidyr)
library(ggplot2)

Melt the data from wide to long format:

df.m <- df %>% gather(day,value,5:9)

Create the plot:

ggplot(data=df.m, aes(x=day, y=value, fill=as.factor(Type))) + 
  geom_bar(stat="identity", position="dodge") + 
  xlab("Day of the week") +
  scale_fill_discrete("Type\nof\nsomething\n") +
  facet_grid(Location ~ ., labeller=label_both) +
  theme_bw() +
  theme(axis.title.y=element_blank())

which results in: enter image description here


However, considering your data, a line graph might be a better visualization:

ggplot(data=df.m, aes(x=day, y=value, color=as.factor(Type), group=as.factor(Type))) + 
  geom_line(size=1.5) + 
  xlab("Days") +
  scale_color_discrete("Type\nof\nsomething\n") +
  facet_grid(Location ~ ., labeller=label_both) +
  theme_bw() +
  theme(axis.title.y=element_blank())

which results in: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    The `tidyr` package and its' `gather` function was news to me, but it seems to work quite elegantly. Thanks for that input! – abel Sep 08 '14 at 20:35
  • I can not install 'tidyr' package on my R Studio. Is there any problem with my R or I should use any specific command? – user3439050 Sep 10 '14 at 16:05
  • Strange, because [tidyr is available on CRAN](http://cran.r-project.org/web/packages/tidyr/index.html). What version of R are you using? Alternatively, you can also install 'tidyr' from Github ([installation instructions](https://github.com/hadley/tidyr)) – Jaap Sep 10 '14 at 19:20
0

You should clarify your questions more precisely, so that readers know exactly what you want. It is also expected that you show an effort in solving the problem by explaining what you already tried.

I could therefore only guess what you wanted and here is my suggestion:

Load the needed packages:

require(ggplot2)
require(reshape2)
require(plyr)

Recreating your df:

location = c('A','A','B','B','C','C')
type = rep(c(1,2),3)
fdate = rep('12-Jul', 6)
tdate = rep('13-Jul', 6)
v1 = c(2,0,0,1,2,3)
v2 = c(4,0,1,0,3,3)
v3 = c(0,1,1,0,1,1)
v4 = c(1,4,3,0,5,0)
v5 = c(2,1,1,1,0,0)

dat = data.frame(location, type, fdate, tdate, v1, v2, v3, v4, v5)

Rearranging data for plotting:

melted = melt(dat, id.vars=c('location', 'type', 'fdate', 'tdate'))
sums = ddply(melted, c('fdate', 'tdate', 'location', 'type', 'variable'), 
summarise, sum=sum(value))

Plot with ggplot2:

ggplot(aes(x=variable, y=sum, fill=as.factor(type)), data=sums) + 
    geom_bar(stat="identity", position="dodge") + 
    facet_grid(location ~ .)

resulting plot edit: Working with the exact data frame you posted:

# read data
dat2 <- read.table(header=T, text="Location    Type    FromDate   ToDate 1   2   3   4   5
A          1        12-Jul    13-Jul  2   4   0   1   2
A          2        12-Jul    13-Jul  0   0   1   4   1
B          1        12-Jul    13-Jul  0   1   1   3   1
B          2        12-Jul    13-Jul  1   0   0   0   1
C          1        12-Jul    13-Jul  2   3   1   5   0
C          2        12-Jul    13-Jul  3   3   1   0   0")

# rearranging data for plotting
melted = melt(dat2, id.vars=c('Location', 'Type', 'FromDate', 'ToDate'))
sums = ddply(melted, c('FromDate', 'ToDate', 'Location', 'Type', 'variable'),
summarise, sum=sum(value))
# plot with ggplot2
ggplot(aes(x=variable, y=sum, fill=as.factor(Type)), data=sums) + 
   geom_bar(stat="identity", position="dodge") + 
   facet_grid(Location ~ .)
abel
  • 470
  • 5
  • 14
  • I ran your code before but today when I want to run again, I am getting this error: "invalid 'type' (character) of argument" after sums function. The only thing that I changed in your code is that in dat, I replaced v1, v2, ... by table [1:6,5:9] (table is the name of my file). – user3439050 Sep 15 '14 at 15:30
  • You will probably have to adjust the variable names in the `ddply` and the following `ggplot` functions. I'll edit my answer and add code which works with your original table as you posted it instead of with the one that I recreated. – abel Sep 15 '14 at 17:46