Lisa-Marie Bossen
Lisa-Marie Bossen

Reputation: 1

How do I plot this data as a grouped barchart?

I would like to plot this data in R

item    response    Freq
count   -99         0.000
count   -88         0.000
count   -77         0.050
count   1           0.039
count   2           0.117
count   3           0.408
count   4           0.385
read    -99         0.011
read    -88         0.000
read    -77         0.117
...      ...        ...

the x axis should be item (so count, read etc., altogether 19 items), on the y axis should be the Frequencies for each response (so 1,2,3,4,-77,-88,-99). So this would be a "grouped" barchart essentially.

I am not able to plot this grouped bar chart using R. Until now, i tried it with:

library(ggplot2)
ggplot(Data, aes(response, Freq*100, color = item)) +
  geom_point(size = 4) + geom_line(size = 1, aes(group = item)) + ggtitle("Items") + 
  ylab("Frequencies %") + xlab("responses") 


require(lattice)
barchart(Freq*100 ~ item, groups=response, Data, xlab="Items", ylab="Frequencies %")

Thanks a lot!

Upvotes: 0

Views: 200

Answers (3)

Daniel
Daniel

Reputation: 7832

You can use following code:

library(ggplot2)
ggplot(dat, aes(x=1, y=Freq, fill=factor(response))) + 
  geom_bar(stat="identity", position="dodge") +
  facet_grid(~item) +
  scale_fill_grey() +
  theme_bw()

enter image description here

The position="dodge" aligns the bars side-by-side. The facet_grid uses a new plot for each "item" (i.e. each group that is indicated by the variable named "item"). The scale_fill_grey() applies grey colours to the bars and the theme_bw() removes the grey background.

Upvotes: 1

rnso
rnso

Reputation: 24593

Try for black-white-grey graph:

barplot(with(ddf, tapply(Freq , list(response, item), mean)), beside=T, xlab="item", ylab="Freq")

enter image description here

Upvotes: 0

Andrie
Andrie

Reputation: 179488

Try this:

dat <- read.table(header=TRUE, text="item    response    Freq
count   -99         0.000
count   -88         0.000
count   -77         0.050
count   1           0.039
count   2           0.117
count   3           0.408
count   4           0.385
read    -99         0.011
read    -88         0.000
read    -77         0.117")

Using ggplot2:

library(ggplot2)
ggplot(dat, aes(x=item, y=Freq, fill=factor(response))) + 
    geom_bar(stat="identity")

enter image description here

Upvotes: 0

Related Questions