plot - How to reshape data for a stacked barchart using R lattice -


this question has answer here:

i have bunch of data in table (imported csv) in following format:

date        classes         score 9/1/11       french          34 9/1/11       english         34 9/1/11       french          34 9/1/11       spanish         34 9/2/11       french          34 9/2/11       english         34 9/3/11       spanish         34 9/3/11       spanish         34 9/5/11       spanish         34 9/5/11       english         34 9/5/11       french          34 9/5/11       english         34 

ignore score column, it's not important.

i need tally of total number of students taking english or spanish or french class based on date, ie. need first group date , divide each day further blocks based on language , plot stacked bar chart looks following. each bar represents date , each cross section of bar represents single language.

i've figured out how once data in matrix form each row represents date , every column attribute (or language). assuming data in form in csv:

ie           french      english       spanish 9/1/11       2           1             1 9/2/11       1           1             0           9/3/11       0           0             2 9/5/11       1           2             1 

then can do:

directory<-"c:\\test\\language.csv" ourdata6<-read.csv(directory)  language<-as.matrix(ourdata6)  barchart(prop.table(language), horizontal=false, auto.key = list(space='right',cex=.5,border=t,points=f, lines=f,lwd=5,text=c('french','spanish','enligsh'),cex=.6), main = list(label="distribution of classes 10",cex=2.5),  ylab = list(", cex=1.7),xlab.top=list("testing",cex=1.2)) 

the challenge data original format format need.

i tried

a<-count(language, c("date", "classes")) 

where gives me counts sorted both in vertical form

ie 9/1/11       french           2              9/1/11       english          1                        9/1/11       spanish          1             etc... 

i need pivot becomes single row per date. if of these might 0 need placeholders them ie. first column must correspond french, second must correspond english current setup work.

any ideas on how or if approach matrix + prop.table correct? there simpler ways of doing this?

supposing data in dataframe called df, can of dplyr , tidyr packages:

library(dplyr) library(tidyr)  wide <- df %>% select(date,classes) %>%   group_by(date,classes) %>%   summarise(n=n()) %>%            # @akrun said, can use tally()   spread(classes, n, fill=0) 

using example data provided, results in following dataframe:

  date english french spanish 9/1/11       1      2       1 9/2/11       1      1       0 9/3/11       0      0       2 9/5/11       2      1       1 

now can make lattice plot with:

barchart(date ~ english + french + spanish, data=wide, stack = true,          main = list(label="distribution of language classes",cex=1.6),          xlab = list("number of classes", cex=1.1),          ylab = list("date", cex=1.1),          auto.key = list(space='right',cex=1.2,text=c('enligsh','french','spanish'))) 

which gives following plot: enter image description here


edit: instead of using lattice-plots, can use ggplot2, (at least in opinion) easier understand. example:

# convert wide dataframe long 1 long <- wide %>% gather(class, n, -date)  # load ggplot2 library(ggplot2)  # create plot ggplot(long, aes(date, n, fill=class)) +   geom_bar(stat="identity", position="stack") +   coord_flip() +   theme_bw() +   theme(axis.title=element_blank(), axis.text=element_text(size=12)) 

which gives: enter image description here


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -