dataset
library(dplyr)
package ‘dplyr’ was built under R version 3.2.5
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
head(mtcars)
ggiraph
Github: https://github.com/davidgohel/ggiraph
hover
Fill the point with orange color when mouse over a point:
gg2 <- gg + geom_point_interactive(aes(data_id = gear,
tooltip = gear), size = 5)
ggiraph(code = print(gg2))
Change the fill color of hover:
gg3 <- gg + geom_point_interactive(aes(data_id = gear,
tooltip = gear), size = 5)
ggiraph(code = {print(gg3)}, hover_css = "fill:black;" )
click actions
# Add a new variable: vehicle model
mtcars2 <- data.frame(model = rownames(mtcars), mtcars)
# create an 'onclick' column
mtcars2$onclick <- sprintf("window.open(\"%s%s\")",
"http://www.google.com/images?q=", as.character(mtcars2$model) )
gg4 <- ggplot(data = mtcars2,
aes(x = mpg, y = wt, color = factor(cyl))) +
geom_point_interactive(
aes( data_id = model, tooltip = model, onclick = onclick ), size = 5 ) +
theme_bw()
ggiraph(code = print(gg4),
hover_css = "fill-opacity:.3;cursor:pointer;")
zoom
ggiraph(code = print(gg4), zoom_max = 7)
plotly
Webpage: https://plot.ly/ggplot2/
Just mouse over the points to see tooltips. You can also zoom, save and lasso select by default:
suppressPackageStartupMessages(library(plotly))
package ‘plotly’ was built under R version 3.2.5
ggplotly(gg + geom_point())
multiple plots
You can also combine multiple plots:
df <- mtcars
df$lab1 <- 'mpg vs cyl'
df$lab2 <- 'disp vs cyl'
mt_mpg <- ggplot(df)+
geom_boxplot(aes(x = as.factor(cyl), y = mpg))+
facet_wrap(~lab1)
mt_disp <- ggplot(df)+
geom_boxplot(aes(x = as.factor(cyl), y = disp))+
facet_wrap(~lab2)
subplot(mt_mpg, mt_disp)
is.na() applied to non-(list or vector) of type 'NULL'is.na() applied to non-(list or vector) of type 'NULL'
rbokeh
Webpage: http://hafen.github.io/rbokeh/#plot_attributes
You can add tooltips, select and zoom, and also save the plot using buttons:
suppressPackageStartupMessages(library(rbokeh))
package ‘rbokeh’ was built under R version 3.2.5
bb1 <- figure() %>%
ly_points(mpg, wt, data = mtcars, color = factor(cyl), size = 25, glyph = factor(cyl),
hover = "This car has @cyl cylinders") %>%
tool_box_select() %>%
tool_lasso_select()
bb1
LS0tDQp0aXRsZTogIkludGVyYWN0aXZlIFBsb3R0aW5nIExpYnJhcmllcyBpbiBSIg0Kb3V0cHV0OiANCiAgaHRtbF9ub3RlYm9vazogDQogICAgdG9jOiB5ZXMNCiAgICB0b2NfZmxvYXQ6IFRSVUUNCi0tLQ0KDQpkYXRhc2V0DQo9PT09PT09PQ0KDQpgYGB7ciBkYXRhfQ0KbGlicmFyeShkcGx5cikNCmhlYWQobXRjYXJzKQ0KYGBgDQoNCg0KZ2dpcmFwaA0KPT09PT09PQ0KR2l0aHViOiBodHRwczovL2dpdGh1Yi5jb20vZGF2aWRnb2hlbC9nZ2lyYXBoDQoNCiMjIHRvb2x0aXANCkRpc3BsYXkgdGhlIHZhbHVlIG9mIGBnZWFyYCB2YXJpYWJsZSB3aGVuIG1vdXNlIG92ZXIgYSBwb2ludDoNCg0KYGBge3IgZ2dpcmFwaDF9DQpzdXBwcmVzc1BhY2thZ2VTdGFydHVwTWVzc2FnZXMobGlicmFyeShnZ3Bsb3QyKSkNCnN1cHByZXNzUGFja2FnZVN0YXJ0dXBNZXNzYWdlcyhsaWJyYXJ5KGdnaXJhcGgpKQ0KDQpnZyA8LSBnZ3Bsb3QoZGF0YSA9IG10Y2FycywNCiAgICAgICAgICAgICBhZXMoeCA9IG1wZywgeSA9IHd0LCBjb2xvciA9IGZhY3RvcihjeWwpKSkgDQoNCmdnMSA8LSAgZ2cgKyBnZW9tX3BvaW50X2ludGVyYWN0aXZlKGFlcyh0b29sdGlwID0gZ2VhciksIHNpemUgPSA1KSANCg0KZ2dpcmFwaChjb2RlID0gcHJpbnQoZ2cxKSkNCmBgYA0KDQojIyBob3Zlcg0KRmlsbCB0aGUgcG9pbnQgd2l0aCBvcmFuZ2UgY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