Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@durtal
Last active August 29, 2015 14:04
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 durtal/fa0d2bcefecb399faa31 to your computer and use it in GitHub Desktop.
Save durtal/fa0d2bcefecb399faa31 to your computer and use it in GitHub Desktop.
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