In [1]:
library("plotly")
In [2]:
set.seed(123)
x <- rnorm(1000)
y <- rchisq(1000, df = 1, ncp = 0)
group <- sample(LETTERS[1:5], size = 1000, replace = T)
size <- sample(1:5, size = 1000, replace = T)
In [3]:
ds <- data.frame(x, y, group, size)
In [4]:
p <- plot_ly(ds, x = x, y = y, mode = "markers", split = group, size = size) %>% layout(title = "Scatter Plot")
In [5]:
embed_notebook(p)
In [6]:
library(plotly)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p <- qplot(carat, price, data=dsamp, colour=clarity)
embed_notebook(p)
In [7]:
# Filled Line Chart
library(plotly)
library(PerformanceAnalytics)
#Load data
data(managers)
# Convert to data.frame
managers.df <- as.data.frame(managers)
managers.df$Dates <- index(managers)
# See first few rows
head(managers.df)
# Plot
p <- plot_ly(managers.df, x = managers.df$Dates, y = managers.df$HAM1, type = "scatter", mode = "lines", name = "Manager 1", fill = "tonexty") %>%
layout(title = "Time Series plot")
embed_notebook(p)
In [8]:
# Heatmap
library(plotly)
library(mlbench)
# Get Sonar data
data(Sonar)
# Use only numeric data
rock <- as.matrix(subset(Sonar, Class == "R")[,1:59])
mine <- as.matrix(subset(Sonar, Class == "M")[,1:59])
# For rocks
p1 <- plot_ly(z = rock, type = "heatmap", showscale = F)
# For mines
p2 <- plot_ly(z = mine, type = "heatmap", name = "test") %>%
layout(title = "Mine vs Rock")
# Plot together
p3 <- subplot(p1, p2)
embed_notebook(p3)
In [9]:
library(plotly)
# initiate a 100 x 3 matrix filled with zeros
m <- matrix(numeric(300), ncol = 3)
# simulate a 3D random-walk
for (i in 2:100) m[i, ] <- m[i-1, ] + rnorm(3)
# collect everything in a data-frame
df <- setNames(
data.frame(m, seq(1, 100)),
c("x", "y", "z", "time")
)
# create the plotly
p <- plot_ly(df, x = df$x, y = df$y, z = df$z, color = time, type = "scatter3d")
embed_notebook(p)
In [10]:
library(plotly)
# Note that volcano is a numeric matrix that ships with R
plot_ly(z = volcano, type = "surface")
# 2D kernel density estimation
kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50))
p <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()
In [11]:
library(ggplot2)
library(ggmap)
d <- data.frame(lat=c(50.659631, 50.607213, 50.608129),
lon=c(3.09319, 3.011473, 3.031529))
Lille <- get_map("Lille,France", zoom=12)
p <- ggmap(Lille)
p + geom_point(data=d, aes(x=lon, y=lat), color="red", size=25, alpha=0.25)
In [12]:
library(rgeos)
library(sp)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
d <- SpatialPointsDataFrame(coords = data[, -1], data = data, proj4string = CRS("+init=epsg:4326"))
d_mrc <- spTransform(d, CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"))
In [13]:
d_mrc_bff_mrc <- gBuffer(d_mrc, byid = TRUE, width = 250, quadsegs = 10)
In [14]:
d_mrc_bff <- spTransform(d_mrc_bff_mrc, CRS("+init=epsg:4326"))
d_mrc_bff_fort <- fortify(d_mrc_bff)
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap +
RL +
geom_path(data=d_mrc_bff_fort, aes(long, lat, group=group), color="green") +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))
In [ ]: