Create a gist now

Instantly share code, notes, and snippets.

@durtal /ggplot2.R
Last active Aug 29, 2015

What would you like to do?
library(ggplot2)
#Load Data
#Make sure months stay ordered - first time I ever wanted a factor!
#Label x-axis with every fifth label
visits_visitors <- read.csv("visits_visitors.csv")
visits_visitors$Month <- factor(visits_visitors$Month, levels = visits_visitors$Month, ordered = TRUE)
visits_visitors$Month_ <- ifelse(as.numeric(row.names(visits_visitors)) %% 5 == 0, as.character(visits_visitors$Month), "")
#Build plot as a series of elements
#create colours to use
cols <- c('Views' = "#278DBC", 'Visitors' = 'navyblue')
ggplot() +
geom_bar(data=visits_visitors, aes(x=Month, y=Views, fill='Views'), stat="identity") +
geom_bar(data=visits_visitors, aes(x=Month, y=Visitors, fill='Visitors'), stat="identity", width=.6) +
scale_y_continuous(breaks=c(0,2000, 4000, 6000, 8000, 10000)) +
scale_x_discrete(labels=visits_visitors$Month_) +
labs(x="", y="") +
geom_hline(aes(yintercept=10000), color="gray") +
scale_fill_manual(values=cols) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(size=.05, color="gray" ),
panel.background = element_rect(fill='white', colour='white'),
axis.ticks = element_blank(),
legend.position = c(.9, 1),
legend.direction = "horizontal",
legend.title=element_blank()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment