In my previous blog on 30 Sep 2016, I had just discovered the plotly package and was blown away by how easy it was to use plotly to make ggplot2 plots reactive to the user’s mouse.
Well I’ve just stumbled across another package to achieve this called ggiraph, and I ggiraph like even more!!
In my opinion ggiraph integrates with ggolot2 in a nicer way than plotly to achieve basic reactivity to the user’s mouse.
Below is some code to make same fake data as I used in my last blog post, but this time I use ggiraph to turn the static ggplot2 plot from my last blog post into a plot with user reactivity.
The code below uses functions from the “ggplot2”, “stringr”, “dplyr”, “knitr” and “ggiraph” packages.
#Make up some fake data
df<-data_frame(state=rep(c("New South Wales",
"Victoria",
"Queensland",
"Western Australia",
"South Australia",
"Tasmania"), 36)) %>%
group_by(state) %>%
mutate(year=c(rep(2012, 9), rep(2013,9),rep(2014, 9),rep(2015, 9))) %>%
group_by(state, year) %>%
mutate(`store ID` = str_c("shop_#",as.character(seq_along(state)))) %>%
group_by(state, year, `store ID`) %>%
mutate(`Revenue ($)` = ifelse(state=="New South Wales", sample(x=c(100000:900000), 1),
ifelse(state=="Victoria", sample(x=c(100000:700000), 1),
ifelse(state=="Queensland", sample(x=c(100000:500000), 1),
ifelse(state=="Western Australia",sample(x=c(10000:200000), 1),
ifelse(state=="South Australia",sample(x=c(10000:90000), 1),
ifelse(state=="Tasmania", sample(x=c(10000:200000), 1), NA))))))) %>%
group_by(state, year) %>%
mutate(`State Total Revenue ($)` = sum(`Revenue ($)`))
kable(head(df))
state | year | store ID | Revenue ($) | State Total Revenue ($) |
---|---|---|---|---|
New South Wales | 2012 | shop_#1 | 271022 | 3885088 |
Victoria | 2012 | shop_#1 | 207434 | 4209586 |
Queensland | 2012 | shop_#1 | 236501 | 3018283 |
Western Australia | 2012 | shop_#1 | 35100 | 1168335 |
South Australia | 2012 | shop_#1 | 15847 | 344081 |
Tasmania | 2012 | shop_#1 | 50989 | 1012868 |
#Create the text you want displayed in the tooltop for geom_point_interactive
df$tooltip_point <- paste0(
"<b>", df$`store ID`, "</b>",
"<br>(", df$state, ")",
"<br>Revenue:",
"<br>$", df$`Revenue ($)`,
"</span></div>"
)
#Create the text you want displayed in the tooltop for geom_boxplot_interactive
df$tooltip_boxplot <- paste0(
"<b>", df$state, "</b>",
"<br>State Total Revenue:",
"<br>$", df$`State Total Revenue ($)`,
"</span></div>"
)
#Make plot reactive to user's mouse with
#'geom_boxplot_interactive' and 'geom_point_interactive'
point_interactive_plot <- ggplot(df, aes(state, `Revenue ($)`)) +
geom_boxplot_interactive(aes(colour=state, tooltip=tooltip_boxplot, data_id=tooltip_boxplot)) +
geom_point_interactive(aes(colour=state, tooltip=tooltip_point, data_id=tooltip_point)) +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.title.y = element_text(face="bold", size=12),
axis.text.y = element_text(angle=0, vjust=0.5, size=11),
legend.title = element_text(size=12, face="bold"),
legend.text = element_text(size = 12, face = "bold"),
plot.title = element_text(face="bold", size=14)) +
ggtitle("Store Revenue per State from 2012 to 2015") +
facet_wrap(~year)
# htmlwidget call
ggiraph(code = {print(point_interactive_plot)}, zoom_max = 5,
tooltip_offx = 20, tooltip_offy = -10,
hover_css = "fill:red;",
tooltip_opacity = 0.7,
pointsize = 12)
The ggiraph package has a bunch of other interactive geom functions too: