library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.5
library(gcookbook)
## Warning: package 'gcookbook' was built under R version 3.2.5

Section 3.1. Making a Basic Bar Graph

library(gcookbook) 
ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity")

ggplot(BOD, aes(x=Time, y=demand)) + geom_bar(stat="identity")

ggplot(BOD, aes(x=factor(Time), y=demand)) + geom_bar(stat="identity")

ggplot(pg_mean, aes(x=group, y=weight)) +
    geom_bar(stat="identity", fill="lightblue", colour="black")

Section 3.2. Grouping Bars Together

library(ggplot2)
library(gcookbook) 
cabbage_exp
##   Cultivar Date Weight        sd  n         se
## 1      c39  d16   3.18 0.9566144 10 0.30250803
## 2      c39  d20   2.80 0.2788867 10 0.08819171
## 3      c39  d21   2.74 0.9834181 10 0.31098410
## 4      c52  d16   2.26 0.4452215 10 0.14079141
## 5      c52  d20   3.11 0.7908505 10 0.25008887
## 6      c52  d21   1.47 0.2110819 10 0.06674995
#y=weight kept returning "Error: stat_count() must not be used with a y aesthetic."

ggplot(cabbage_exp,aes(x=Date, fill=Cultivar))+ geom_bar(position="dodge")

ce <- cabbage_exp[1:5, ]   

ggplot(ce, aes(x=Date, fill=Cultivar)) + geom_bar(position="dodge", colour="black") + scale_fill_brewer(palette="Pastel1")

Section 3.3. Making a Bar Graph of Counts

ggplot(diamonds, aes(x=cut)) + geom_bar()

ggplot(diamonds, aes(x=carat)) + geom_bar()

Section 3.4. Using Colors in a Bar Graph

 library(gcookbook) 
upc <- subset(uspopchange, rank(Change)>40)

ggplot(upc, aes(x=Abb, y=Change, fill=Region)) + geom_bar(stat="identity")

ggplot(upc, aes(x=reorder(Abb, Change), y=Change, fill=Region)) +
    geom_bar(stat="identity", colour="black") +
    scale_fill_manual(values=c("#669933", "#FFCC66")) +
    xlab("State")

Section 3.5. Coloring Negative and Positive Bars Differently

library(gcookbook) 
csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
csub$pos <- csub$Anomaly10y >= 0

ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
  geom_bar(stat="identity", position="identity")

ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
    geom_bar(stat="identity", position="identity", colour="black", size=0.25) +
    scale_fill_manual(values=c("#CCEEFF", "#FFDDDD"), guide=FALSE)

Section 3.6. Adjusting Bar Width and Spacing

library(gcookbook) 
ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity")

ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity", width=0.5)

ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity", width=1)

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity", width=0.5, position="dodge")

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity", width=0.5, position=position_dodge(0.7))

Section 3.7. Making a Stacked Bar Graph

library(gcookbook)
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity",width=.9,position=position_dodge())

library(gcookbook) 
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity",width=.2,position=position_dodge(.7))

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity")+guides(fill=guide_legend(reverse=TRUE))

library(plyr) 
## Warning: package 'plyr' was built under R version 3.2.5
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar, order=desc(Cultivar)))+geom_bar(stat="identity")

Section 3.8. Making a Proportional Stacked Bar Graph

library(gcookbook) 
library(plyr)
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar,order=desc(Weight))) +geom_bar(stat="identity",colour="black")+guides(fill=guide_legend(reverse=TRUE))+scale_fill_brewer(palette="Pastel1")

ce <- ddply(cabbage_exp, "Date", transform,percent_weight = Weight / sum(Weight) * 100)
ggplot(ce, aes(x=Date, y=percent_weight, fill=Cultivar)) +geom_bar(stat="identity")

ggplot(ce,aes(x=Date,y=percent_weight,fill=Cultivar))+geom_bar(stat="identity",colour="black")+guides(fill=guide_legend(reverse=TRUE))+scale_fill_brewer(palette="Pastel1")

Section 3.9. Adding Labels to a Bar Graph

library(gcookbook) 
ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight)) +
    geom_bar(stat="identity") +
    geom_text(aes(label=Weight), vjust=1.5, colour="white")

ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight)) +
    geom_bar(stat="identity") +
    geom_text(aes(label=Weight), vjust=-0.2)

ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight))+geom_bar(stat="identity")+geom_text(aes(label=Weight), vjust=-0.2)+ylim(0, max(cabbage_exp$Weight) * 1.05)

ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight)) + geom_bar(stat="identity")+geom_text(aes(y=Weight+0.1, label=Weight))

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity", position="dodge") +geom_text(aes(label=Weight), vjust=1.5, colour="white",
position=position_dodge(.9), size=3)

library(plyr)
ce <- arrange(cabbage_exp, Date, Cultivar)
ce <- ddply(ce, "Date", transform, label_y=cumsum(Weight))

ggplot(ce, aes(x=Date, y=Weight, fill=Cultivar)) +geom_bar(stat="identity") +geom_text(aes(y=label_y, label=Weight), vjust=1.5, colour="white")

ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",colour="black")+geom_text(aes(y=label_y,label=paste(format(Weight,nsmall=2),"kg")),size=4,vjust=6)+guides(fill=guide_legend(reverse=TRUE))+scale_fill_brewer(palette="Pastel1")

Section 3.10. Making a Cleveland Dot Plot

library(gcookbook) 
tophit <- tophitters2001[1:25, ] 
ggplot(tophit, aes(x=avg, y=name)) + geom_point()

ggplot(tophit, aes(x=avg, y=reorder(name, avg))) +
    geom_point(size=6) +
    theme_bw() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.major.y = element_line(colour="grey60", linetype="dashed"))

ggplot(tophit, aes(x=reorder(name, avg), y=avg)) +
    geom_point(size=6) +
    theme_bw() +
    theme(axis.text.x = element_text(angle=60, hjust=1),
          panel.grid.major.y = element_blank(),
          panel.grid.minor.y = element_blank(),
          panel.grid.major.x = element_line(colour="grey60", linetype="dashed"))

nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
tophit$name <- factor(tophit$name, levels=nameorder)

ggplot(tophit, aes(x=avg,y=name))+geom_segment(aes(yend=name), xend=0, colour="grey50")+geom_point(size=3, aes(colour=lg)) +scale_colour_brewer(palette="Set1", limits=c("NL","AL")) +theme_bw()+theme(panel.grid.major.y = element_blank(),legend.position=c(1,0.55),legend.justification=c(1, 0.5))

ggplot(tophit, aes(x=avg,y=name))+geom_segment(aes(yend=name), xend=0, colour="grey50")+geom_point(size=3,aes(colour=lg))+scale_colour_brewer(palette="Set1", limits=c("NL","AL"),guide=FALSE)+theme_bw()+theme(panel.grid.major.y = element_blank())+facet_grid(lg~.,scales="free_y", space="free_y")

Additional Problems

Create three informative graphs that demonstrate the skills you have learned in this chapter. Minimally, at least one graph should have each of the following properties. Use one or more of the listed datasets to create your graphs.

Properties

Datasets

library(ggplot2)
library(plyr)

#Basic Bar Plot, Bar Plot Using Counts, Labels for Bar Plots
Executions <- read.csv("C:\\Users\\mondude\\AppData\\Local\\Temp\\RtmpkT0Uef\\data11705f1b160f")
ggplot(Executions,aes(x=Region,y=Death.Row.Prisoners))+geom_bar(stat="identity")+geom_text(aes(label=Death.Row.Prisoners,vjust=-.2))
## Warning: Removed 18 rows containing missing values (position_stack).
## Warning: Removed 18 rows containing missing values (geom_text).

#Proportional Stacked Bar Plot, Legend, Colored Bars
Executions <- read.csv("C:\\Users\\mondude\\AppData\\Local\\Temp\\RtmpkT0Uef\\data11705f1b160f")
Ex<-ddply(Executions, "Region", transform,percent_executed = Ex.Since.1976 / sum(Ex.Since.1976) * 100)
ggplot(Ex, aes(x=Region, y=percent_executed, fill=State)) +geom_bar(stat="identity",colour="black")

#Cleveland Dot Plot
Executions[, c("State", "Region", "Ex.Pre.1976")]
##                   State    Region Ex.Pre.1976
## 1               Alabama     South         708
## 2                Alaska      West          12
## 3               Arizona      West         104
## 4              Arkansas     South         478
## 5            California      West         709
## 6              Colorado      West         101
## 7           Connecticut Northeast         126
## 8              Delaware Northeast          62
## 9  District of Columbia     South         118
## 10              Florida     South         314
## 11              Georgia     South         950
## 12               Hawaii      West          49
## 13                Idaho      West          26
## 14             Illinois   Midwest         348
## 15              Indiana   Midwest         131
## 16                 Iowa   Midwest          45
## 17               Kansas   Midwest          57
## 18             Kentucky     South         424
## 19            Louisiana     South         632
## 20                Maine Northeast          21
## 21             Maryland     South         309
## 22        Massachusetts Northeast         345
## 23             Michigan   Midwest          13
## 24            Minnesota   Midwest          66
## 25          Mississippi     South         351
## 26             Missouri   Midwest         285
## 27              Montana      West          71
## 28             Nebraska   Midwest          34
## 29               Nevada      West          61
## 30        New Hampshire Northeast          24
## 31           New Jersey Northeast         361
## 32           New Mexico      West          73
## 33             New York Northeast        1130
## 34       North Carolina     South         784
## 35         North Dakota   Midwest           8
## 36                 Ohio   Midwest         438
## 37             Oklahoma     South         132
## 38               Oregon      West         122
## 39         Pennsylvania Northeast        1040
## 40         Rhode Island Northeast          52
## 41       South Carolina     South         641
## 42         South Dakota   Midwest          15
## 43            Tennessee     South         335
## 44                Texas     South         755
## 45                 Utah      West          43
## 46              Vermont Northeast          26
## 47             Virginia     South        1277
## 48           Washington      West         105
## 49        West Virginia     South         155
## 50            Wisconsin   Midwest           1
## 51              Wyoming      West          22
ggplot(Executions,aes(x=Ex.Pre.1976,y=reorder(State,Ex.Pre.1976)))+geom_point(size=3)+theme_bw() +theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed"))


END!