Skip to content

Instantly share code, notes, and snippets.

@jalapic
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalapic/5c14c86b2f6ad632251a to your computer and use it in GitHub Desktop.
Save jalapic/5c14c86b2f6ad632251a to your computer and use it in GitHub Desktop.
### Bar plots in ggplot
# data come from engsoccerdata package.
#### bar plot
library(ggplot2)
ggplot(df, aes(Season, totalpts, fill= country)) +
geom_bar(position="dodge", stat="identity") +
facet_grid(.~country)+
xlab("this is my x-axis") +
ylab("this is my y-axis") +
scale_fill_manual(values=c("dodgerblue", "mediumblue", "black", "orange", "green", "red")) +
ggtitle("this is my title") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(2.5)),
panel.background = element_blank(),
panel.grid.major.y = element_line(color="gray65"),
panel.grid.major.x = element_line(color="gray99"),
panel.grid.minor = element_blank(),
plot.background = element_blank(),
text = element_text(color="gray20", size=10),
axis.text = element_text(size=rel(1.0)),
axis.text.x = element_text(color="gray20",size=rel(1.5)),
axis.text.y = element_text(color="gray20", size=rel(1.5)),
axis.title.x = element_text(size=rel(1.8), vjust=0),
axis.title.y = element_text(size=rel(1.8), vjust=1),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
legend.key=element_rect(fill=NA),
legend.title=element_blank(),
strip.text.x = element_text(size=12, face="bold"),
strip.background = element_rect(colour="white", fill="white")
)
#### stacked bar plot
ggplot(df, aes(Season, totalpts, fill= country)) +
geom_bar(stat="identity") +
xlab("this is my x-axis") +
ylab("this is my y-axis") +
scale_fill_manual(values=c("dodgerblue", "mediumblue", "black", "orange", "green", "red")) +
ggtitle("this is my title") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(2.5)),
panel.background = element_blank(),
panel.grid.major.y = element_line(color="gray65"),
panel.grid.major.x = element_line(color="gray99"),
panel.grid.minor = element_blank(),
plot.background = element_blank(),
text = element_text(color="gray20", size=10),
axis.text = element_text(size=rel(1.0)),
axis.text.x = element_text(color="gray20",size=rel(1.5)),
axis.text.y = element_text(color="gray20", size=rel(1.5)),
axis.title.x = element_text(size=rel(1.8), vjust=0),
axis.title.y = element_text(size=rel(1.8), vjust=1),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
legend.key=element_rect(fill=NA),
legend.title=element_blank(),
strip.text.x = element_text(size=12, face="bold"),
strip.background = element_rect(colour="white", fill="white")
)
#### sample data
df <-
structure(list(
group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("bottom",
"top"), class = "factor"), totalwins = c(5L, 2L, 4L, 3L, 7L,
2L, 6L, 1L, 6L, 4L, 2L, 6L, 4L, 2L, 1L, 2L, 4L, 4L, 3L, 2L, 3L,
3L, 0L, 3L, 5L, 2L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 3L,
4L, 3L, 4L, 2L, 5L, 6L, 6L, 5L, 3L, 4L, 0L, 1L, 4L, 3L, 4L, 1L,
3L, 3L, 1L, 3L, 2L, 1L, 0L, 2L, 3L, 4L, 0L, 1L, 3L, 2L, 1L, 1L,
0L, 2L, 2L, 2L, 5L, 1L, 2L, 3L, 6L, 5L, 5L, 4L, 6L, 3L, 3L, 1L,
4L, 2L, 2L, 2L, 4L, 2L, 6L, 2L, 3L, 4L, 5L, 7L, 4L, 2L, 4L, 2L,
0L, 4L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 4L, 4L, 2L, 1L), totalpts = c(26,
14, 16, 19, 28, 14, 27, 11, 23, 21, 15, 28, 20, 13, 12, 17, 17,
18, 14, 9, 11, 13, 5, 12, 17, 11, 7, 11, 8, 13, 8, 9, 7, 6, 9,
9, 13, 20, 17, 15, 12, 22, 23, 26, 21, 17, 21, 9, 9, 16, 15,
17, 8, 17, 14, 10, 13, 16, 10, 6, 14, 16, 15, 10, 6, 14, 15,
8, 11, 9, 13, 13, 17, 19, 5, 10, 18, 23, 29, 21, 19, 23, 18,
17, 13, 18, 15, 14, 12, 17, 14, 21, 10, 13, 16, 19, 28, 18, 13,
17, 15, 5, 20, 12, 13, 9, 5, 9, 11, 11, 15, 15, 10, 9), country = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("England", "France", "Germany", "Holland", "Italy",
"Spain"), class = "factor"), Season = c(1995L, 1996L, 1997L,
1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 1995L, 1996L,
1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 1995L,
1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L,
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L)), .Names = c("group", "totalwins", "totalpts",
"country", "Season"), row.names = c(NA, -114L), class = c("tbl_df",
"data.frame"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment