Skip to content

Instantly share code, notes, and snippets.

@amitkaps
Created September 10, 2015 05:15
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 amitkaps/8710efbfcd1c6d294a9f to your computer and use it in GitHub Desktop.
Save amitkaps/8710efbfcd1c6d294a9f to your computer and use it in GitHub Desktop.
Explore rbokeh library in R
library(dplyr)
library(ggplot2)
library(rbokeh)
########################
# Layering
########################
# Histogram & Density Plot - Price
ggplot(diamonds) + aes(price, y = ..density..) +
geom_histogram(binwidth = 100) +
geom_density(color = "red")
figure() %>%
ly_hist(price, data = diamonds, breaks = 250, freq = FALSE) %>%
ly_density(price, data = diamonds, color = "red")
########################
# Interaction
########################
# Take a sample of 1000 from diamonds dataset
dsmall <- sample_n(diamonds, 1000)
# Scatterplot - Carat vs Price, with Cut
ggplot(dsmall) + aes(carat, price, color = cut) +
geom_point(alpha = 0.5, size = 4)
# Make same scatterplot with rbokeh
figure() %>%
ly_points(carat, price, data = dsmall, color = cut)
# Make same scatterplot with rbokeh - width = 600
figure(width = 600) %>%
ly_points(carat, price, data = dsmall, color = cut)
# Add a interactive tool capability - lasso
figure(width = 600) %>%
ly_points(carat, price, data = dsmall, color = cut) %>%
tool_lasso_select()
# Add hover labels to the point
figure(width = 600) %>%
ly_points(carat, price, data = dsmall, color = cut,
hover = list(carat, price, color))
########################
# Mapping
########################
# Create a dummy data frame of points
set.seed(500)
df <- round(data.frame(
lon = jitter(rep( 77.59, 50), amount = .3),
lat = jitter(rep( 12.97, 50), amount = .3)
), digits = 2)
# Plot these points on the map using ggmap
library(ggmap)
map <- get_map("Bangalore")
ggmap(map)
ggmap(map) + geom_point(data = df, aes(lon, lat),
color = "red", size = 6)
# Plot these points on the map using rbokeh
map1 <- gmap(lat = 12.97, lng = 77.59, zoom = 9,
width = 500, height = 500)
map1 %>%
ly_points(lon, lat, data = df, alpha = 0.8, col = "red",
hover = c(lon, lat))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment