emehex
emehex

Reputation: 10538

Creating Stagger Charts in R

I have a df that looks like this:

df <- data.frame(
  CM = c("Jan", "Jan", "Jan", "Feb", "Feb", "Mar", "Mar", "Mar", "Apr", "Apr", "Apr"),
  PM = c("Jan", "Feb", "Mar", "Feb", "Mar", "Mar", "Apr", "May", "Apr", "May", "Jun"),
  Value = c(8, 5, 6, 8, 3, 4, 7, 6, 7, 1, 3))

(Where CM = Current Month, PM = Predicted Month)

df

CM   PM   Value
Jan  Jan  8*
Jan  Feb  5
Jan  Mar  6
Feb  Feb  8*
Feb  Mar  3
Mar  Mar  4*
Mar  Apr  7
Mar  May  6
Apr  Apr  7*
Apr  May  1
Apr  Jun  3

I'd like to transform this tidy data to a stagger chart.

       Prediction
       Jan   Feb   Mar   Apr   May   Jun  ...
M|Jan  8*    5     6
O|Feb        8*    3
N|Mar              4*    7     6
T|Apr                    7*    1     3
H|...

*s denote actual values.

Upvotes: 4

Views: 330

Answers (3)

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

According to your numerical value in df, you can try this:

>df$CM<-factor(df$CM,levels=month.abb)
>df$PM<-factor(df$PM,levels=month.abb) 
>xtabs(Value ~ CM + PM, df)

     PM
CM    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  Jan   8   5   6   0   0   0   0   0   0   0   0   0
  Feb   0   8   3   0   0   0   0   0   0   0   0   0
  Mar   0   0   4   7   6   0   0   0   0   0   0   0
  Apr   0   0   0   7   1   3   0   0   0   0   0   0
  May   0   0   0   0   0   0   0   0   0   0   0   0
  Jun   0   0   0   0   0   0   0   0   0   0   0   0
  Jul   0   0   0   0   0   0   0   0   0   0   0   0
  Aug   0   0   0   0   0   0   0   0   0   0   0   0
  Sep   0   0   0   0   0   0   0   0   0   0   0   0
  Oct   0   0   0   0   0   0   0   0   0   0   0   0
  Nov   0   0   0   0   0   0   0   0   0   0   0   0
  Dec   0   0   0   0   0   0   0   0   0   0   0   0

Upvotes: 1

NicE
NicE

Reputation: 21425

You can use dcast from the reshape2 package:

library(reshape2)
df <- data.frame(
  CM = c("Jan", "Jan", "Jan", "Feb", "Feb", "Mar", "Mar", "Mar", "Apr", "Apr", "Apr"),
  PM = c("Jan", "Feb", "Mar", "Feb", "Mar", "Mar", "Apr", "May", "Apr", "May", "Jun"),
  Value = c(8, 5, 6, 8, 3, 4, 7, 6, 7, 1, 3))
df$CM<-factor(df$CM,levels=month.abb)
df$PM<-factor(df$PM,levels=month.abb)
dcast(df,CM~PM,value.var="Value",fill="")

Give this with your example:

   CM Jan Feb Mar Apr May Jun
1 Jan   8   5   6            
2 Feb       8   3            
3 Mar           4   7   6    
4 Apr               7   1   3  

To add the stars:

res<-dcast(df,CM~PM,value.var="Value",fill="")
row.names(res)<-res[,1]
res<-res[,-1]

for(i in 1:nrow(res)){
  res[i,i]<-paste0(res[i,i],"*")
}
res

Gives:

    Jan Feb Mar Apr May Jun
Jan  8*   5   6            
Feb      8*   3            
Mar          4*   7   6    
Apr              7*   1   3

Upvotes: 3

user1267127
user1267127

Reputation:

You can use the following too

data<- structure(list(C.M = structure(c(3L, 3L, 3L, 2L, 2L, 4L, 4L, 
4L, 1L, 1L, 1L), .Label = c("Apr", "Feb", "Jan", "Mar"), class = "factor"), 
    P.M = structure(c(3L, 2L, 5L, 2L, 5L, 5L, 1L, 6L, 1L, 6L, 
    4L), .Label = c("Apr", "Feb", "Jan", "Jun", "Mar", "May"), class = "factor"), 
    Value = structure(c(8L, 4L, 5L, 8L, 2L, 3L, 6L, 5L, 7L, 1L, 
    2L), .Label = c("1", "3", "4*", "5", "6", "7", "7*", "8*"
    ), class = "factor")), .Names = c("C.M", "P.M", "Value"), class = "data.frame", row.names = c(NA, 
-11L))

df <- as.data.frame(reshape(data,idvar="C.M",timevar="P.M",direction="wide"))
    print (df)

Upvotes: 0

Related Questions