Last active
August 29, 2015 14:03
-
-
Save mkcor/d54cc5eb4d3b826d093b to your computer and use it in GitHub Desktop.
Source code for http://rpubs.com/plotly/user2014-demographics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Conference Demographics" | |
output: html_document | |
--- | |
The R User Conference 2014 Demographics | |
======================================================== | |
(View source here: https://gist.github.com/mkcor/d54cc5eb4d3b826d093b) | |
Some informal data and graphs created for David McArthur by Tom Filloon and Ari | |
Lamstein and Marianne Corvellec. | |
Made with R, RStudio, knitr, ggplot2, Hmisc and plotly. Thanks R Community for | |
making this so easy! | |
```{r, cache=TRUE} | |
# create some random data that matches with our experiences during the conference | |
sex <- sample(c("M", "F"), 700, prob=c(0.75, .25), T) | |
age <- rnorm(700, 35, 5) | |
continent <- sample(c("North America", "Europe", "Asia", "South America"), 700, | |
prob=c(.5, .3, .15, .05), T) | |
year_first_contact <- age | |
year_first_contact <- year_first_contact + 1949 | |
year_first_contact <- year_first_contact + sample(-3:3, 700, T) # to add some jitter | |
affiliation <- sample(c("A", "G", "I", "S"), 700, prob=c(0.3, 0.03, 0.49, 0.18), | |
replace=T) | |
df <- data.frame(sex=sex, age=age, continent=continent, | |
year_first_contact=year_first_contact, affiliation=affiliation) | |
# force first row to be David McArthur | |
df[1, "sex"] <- "M" | |
df[1, "age"] <- 70.0 | |
df[1, "continent"] <- "North America" | |
df[1, "year_first_contact"] <- 1958 | |
df[1, "affiliation"] <- "A" | |
``` | |
# Now, let's view the data | |
```{r, echo=FALSE, message=FALSE} | |
library(ggplot2) | |
library(Hmisc) | |
library(devtools) | |
install_github("ropensci/plotly") | |
library(plotly) | |
``` | |
```{r, eval=FALSE} | |
library(ggplot2) | |
library(Hmisc) | |
library(devtools) | |
install_github("ropensci/plotly") | |
library(plotly) | |
``` | |
```{r} | |
continent <- ggplot(df, aes(x=continent)) + geom_bar() + ggtitle("Continent of Origin") | |
py <- plotly() | |
``` | |
You can view an interactive version of this plot at https://plot.ly/~mkcor/414 | |
and get R code for it at https://plot.ly/~mkcor/414.r | |
```{r, plotly=TRUE, width=600, height=600} | |
py$ggplotly(continent, session="knitr") | |
``` | |
```{r} | |
affiliation <- ggplot(df, aes(x=affiliation)) + geom_bar() + | |
ggtitle("Affiliation (Academic, Government, Industry, Student)") | |
``` | |
Again, view an interactive version of this plot at https://plot.ly/~mkcor/395 | |
```{r, plotly=TRUE, width=600, height=600} | |
py$ggplotly(affiliation, session="knitr") | |
``` | |
```{r} | |
sex <- ggplot(df, aes(x=sex)) + geom_bar() + ggtitle("Sex of Attendees") | |
``` | |
You can use the link in the iframe above and append file extensions to it, like | |
.r, .json, .png, etc. | |
```{r, plotly=TRUE, width=600, height=600} | |
py$ggplotly(sex, session="knitr") | |
``` | |
```{r} | |
bpplot(df$age, main="Age of Attendees") | |
bpplot(df$year_first_contact, main="First Computer Contact") | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment