2

I have a dataset for TV watching on Weekdays and Weekend for 7 students (named "TVwatch") as follows:

TV watching in hours:

Names   Wkdays Wkend        
Dawn     5.0    4.5
Steve    9.8    2.0
Lena     3.5    5.5
Patty    9.6    4.3
Ania     8.3    3.2
Beth     9.3    2.5
Roby     2.5    7.4

How can I create a comparison plot where vertical bars will show the hours of TVwatching on wkdays and wkends(on Y-axis) for each students (on X-axis) in R (similar to Clustered columns in Excel)?

Thanks,

Justin
  • 42,475
  • 9
  • 93
  • 111
  • It's not clear what your after exactly. Do you want to make a staked bar plot ? http://stackoverflow.com/questions/7583432/plot-stacked-bar-plot-in-r – Chargaff Jun 20 '13 at 18:34
  • 3
    What all have you tried? When asking questions here please show that you have done some research on your own before you arrived here and asked for code. Using `melt` from the `reshape2` package and the `ggplot2` package this is very straight forward and has been asked and answered many times. – Justin Jun 20 '13 at 18:44

2 Answers2

2

Lattice is a perfectly good way to go (+1 to @agstudy). If you want a base R approach, you could try this:

d = read.table(text="Names   Wkdays Wkend        
Dawn     5.0    4.5
Steve    9.8    2.0
Lena     3.5    5.5
Patty    9.6    4.3
Ania     8.3    3.2
Beth     9.3    2.5
Roby     2.5    7.4", header=T)

d = t(as.matrix(d[,2:3]))
d = as.table(d)
colnames(d) = c("Dawn", "Steve", "Lena", "Patty", "Ania", "Beth", "Roby")

windows()
  barplot(d, beside=T)

enter image description here

The key function is ?barplot, and the key argument is beside=TRUE. You can learn more about barplots in R at the Quick-R website.

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
1

For example:

library(lattice)
barchart(Wkdays + Wkend~Names,data=dat,auto.key=TRUE)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261