Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions skittles.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ library(ggplot2)
skipalette<-c("#c0043f","#e64808","#f1be02","#048207","#441349")

# Rename data headers, and grab the overall mean
names(skittles)<-c("Pak","Strawberry","Orange","Lemon","Apple","Grape")
names(skittles)<-c("Pack","Strawberry","Orange","Lemon","Apple","Grape")
netmean<-mean(c(skittles$Strawberry,skittles$Orange,skittles$Lemon,skittles$Apple,skittles$Grape))

# Summarize data into frame "skitsum" for the final plot.
Expand All @@ -21,8 +21,8 @@ skitsum$flavor <- factor(skitsum$flavor,
c("Strawberry","Orange","Lemon","Apple","Grape"))

# Generate "skitbin". "skitbin" will allow us to plot a neat "Raw Results" diagram in the first plot
skitmelt<-melt(skittles,id="Pak")
skitbin<-data.frame("Pak"=NA,"variable"=NA,"value"=NA)
skitmelt<-melt(skittles,id="Pack")
skitbin<-data.frame("Pack"=NA,"variable"=NA,"value"=NA)
for(n in 1:nrow(skitmelt)){
skitbin<-rbind(skitbin,skitmelt[rep(n,skitmelt$value[n]),]) }
skitbin<-skitbin[2:nrow(skitbin),]
Expand All @@ -31,13 +31,13 @@ skitbin$variable <- factor(skitbin$variable,
rm(n);rm(skitmelt)

# Convert raw data into a "Raw results" pictogram via "skitbin"
ggplot(skitbin,aes(x=Pak,y=variable))+
ggplot(skitbin,aes(x=Pack,y=variable))+
geom_jitter(aes(fill=variable),height=5/16,width=5/16,shape=21,size=3)+
geom_tile(color="grey75",alpha=0)+
scale_fill_manual("Flavor",values=skipalette)+
guides(fill="none")+
scale_x_continuous(breaks=1:36,minor_breaks=seq(.5,36.5,1))+
scale_y_discrete(limits=rev(levels(melt(skittles,id="Pak")$variable)))+
scale_y_discrete(limits=rev(levels(melt(skittles,id="Pack")$variable)))+
labs(title="Skittles Counts",
subtitle="Raw Results",
x="Pack Number",y="Flavor",
Expand All @@ -46,21 +46,21 @@ ggplot(skitbin,aes(x=Pak,y=variable))+
theme(panel.grid=element_blank())

# Convert raw data into heatmap, mildly more pleasing than "skitbin"
ggplot(melt(skittles,id="Pak"),aes(x=Pak,y=variable))+
ggplot(melt(skittles,id="Pack"),aes(x=Pack,y=variable))+
geom_tile(aes(fill=variable,alpha=value),color="white")+
geom_text(aes(label=value),size=3)+
scale_fill_manual("Flavor",values=skipalette)+
guides(alpha="none",fill="none")+
scale_x_continuous(breaks=1:36,minor_breaks=NULL)+
scale_y_discrete(limits=rev(levels(melt(skittles,id="Pak")$variable)))+
scale_y_discrete(limits=rev(levels(melt(skittles,id="Pack")$variable)))+
labs(title="Skittles Counts",
subtitle="Raw Results Converted into Heatmap",
x="Pack Number",y="Flavor",
caption="created by /u/zonination")+
theme_bw()

# Convert data into stacked bar chart. Yet even more pleasing.
ggplot(melt(skittles,id="Pak"),aes(Pak,value))+
ggplot(melt(skittles,id="Pack"),aes(Pack,value))+
geom_bar(stat="identity",aes(fill=variable),alpha=.8)+
scale_fill_manual("Flavor",values=skipalette)+
labs(title="Skittles Counts",
Expand All @@ -71,7 +71,7 @@ ggplot(melt(skittles,id="Pak"),aes(Pak,value))+
theme_bw()

# Plot data in a violinplot, with added dotplot for effect.
ggplot(melt(skittles,id="Pak"),aes(variable,value))+
ggplot(melt(skittles,id="Pack"),aes(variable,value))+
geom_violin(aes(fill=variable),alpha=.8)+
geom_dotplot(binaxis="y",stackdir="center",aes(fill=variable))+
scale_fill_manual("Flavor",values=skipalette)+
Expand Down
4 changes: 2 additions & 2 deletions skittles.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Pak,Red,Orange,Yellow,Green,Purple
Pack,Red,Orange,Yellow,Green,Purple
1,10,16,8,12,14
2,11,15,13,15,7
3,8,15,14,8,19
Expand Down Expand Up @@ -34,4 +34,4 @@ Pak,Red,Orange,Yellow,Green,Purple
33,12,16,19,6,8
34,11,14,13,11,12
35,15,13,15,10,10
36,13,11,12,11,14
36,13,11,12,11,14