1

I have the following dataset:

year    type      count
1560    Person    2
1560    Public    1
1560    Thing     1
1578    Academic  1
1578    Public    1
1578    Thing     1
1582    Person    3
1582    Public    3
1582    Thing     3
...

My goal is to plot this dataset as three different colored histograms in one plot with respect to the group/type. The x-axis should represent the year, while there should be four bins (one for each type [Person/Public/Thing/Academic] for each year, representing the count for that group.

For now I have the following R code:

dat <- read.csv(
    file = filename
    ,header = T
    ,sep = "\t"
    ,quote = "\""
    ,row.names = NULL
    ,fileEncoding = "UTF8"
    ,stringsAsFactors = F);

melt_df <- melt(dat, id.vars = c("year","type"), measure.vars = c("count"));

ggplot(melt_df, aes(x = year, y = value, fill = variable)) +
    geom_bar(stat = 'summary', fun.y = sum) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    scale_y_continuous(limits=c(0,155),     breaks=seq(0,155,5)) +
    scale_x_continuous(limits=c(1550,2000), breaks=seq(1550,2000,10));

This results in the following plot: ggplot from dataset above

Can anyone point me in the right direction, how to get this done?

Please don't suggest to rearrange the dataset like this:

year    Person  Public  Thing  Academic
1560    2       1       1      0
...

Of course I can plot this datasaet without problems, but it's not the format I can expect, so it would be nice to work with the dataset above.

pnine
  • 83
  • 1
  • 8

1 Answers1

3

Simply do

ggplot(d, aes(factor(year), count, fill = type)) + 
   geom_col(position = "dodge")

enter image description here

data

d <- read.table(text="year    type      count
1560    Person    2
                1560    Public    1
                1560    Thing     1
                1578    Academic  1
                1578    Public    1
                1578    Thing     1
                1582    Person    3
                1582    Public    3
                1582    Thing     3", header=T)
Roman
  • 17,008
  • 3
  • 36
  • 49