Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Suggests:
processx,
plotlyGeoAssets,
forcats,
thematic
thematic,
palmerpenguins
LazyData: true
RoxygenNote: 7.1.1
Encoding: UTF-8
Expand Down
7 changes: 4 additions & 3 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
#' @seealso [plot_ly()]
#' @examples \dontrun{
#' # simple example
#' ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
#' ggplotly(ggiris)
#' ggpenguins <- qplot(bill_length_mm , body_mass_g,
#' data = palmerpenguins::penguins, color = species)
#' ggplotly(ggpenguins)
#'
#' data(canada.cities, package = "maps")
#' viz <- ggplot(canada.cities, aes(long, lat)) +
Expand All @@ -58,7 +59,7 @@
#' demo("crosstalk-highlight-ggplotly", package = "plotly")
#'
#' # client-side linked brushing in a scatterplot matrix
#' highlight_key(iris) %>%
#' highlight_key(palmerpenguins::penguins) %>%
#' GGally::ggpairs(aes(colour = Species), columns = 1:4) %>%
#' ggplotly(tooltip = c("x", "y", "colour")) %>%
#' highlight("plotly_selected")
Expand Down
12 changes: 6 additions & 6 deletions R/plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@
#' # You might notice plot_ly() has named arguments that aren't in this figure
#' # reference. These arguments make it easier to map abstract data values to
#' # visual attributes.
#' p <- plot_ly(iris, x = ~Sepal.Width, y = ~Sepal.Length)
#' add_markers(p, color = ~Petal.Length, size = ~Petal.Length)
#' add_markers(p, color = ~Species)
#' add_markers(p, color = ~Species, colors = "Set1")
#' add_markers(p, symbol = ~Species)
#' add_paths(p, linetype = ~Species)
#' p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~body_mass_g)
#' add_markers(p, color = ~bill_depth_mm, size = ~bill_depth_mm)
#' add_markers(p, color = ~species)
#' add_markers(p, color = ~species, colors = "Set1")
#' add_markers(p, symbol = ~species)
#' add_paths(p, linetype = ~species)
#'
#' }
#'
Expand Down
7 changes: 4 additions & 3 deletions man/ggplotly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/plot_ly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test_that("Can overwrite a grid", {

id <- new_id()
m <- api_create(mtcars, id)
m2 <- api_create(iris, id)
m2 <- api_create(palmerpenguins::penguins, id)
expect_true(identical(m$embed_url, m2$embed_url))
expect_false(identical(m$cols, m2$cols))
})
Expand Down
37 changes: 19 additions & 18 deletions tests/testthat/test-ggplot-labels.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
context("labels")

test_that("ggtitle is translated correctly", {
ggiris <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width)) +
ggpenguin <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_depth_mm)) +
ggtitle("My amazing plot!")
info <- expect_doppelganger_built(ggiris, "labels-ggtitle")
info <- expect_doppelganger_built(ggpenguin, "labels-ggtitle")
expect_identical(info$layout$title$text, "My amazing plot!")
})

test_that("ylab is translated correctly", {
ggiris <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width)) +
ylab("sepal width")
info <- expect_doppelganger_built(ggiris, "labels-ylab")
ggpenguin <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_depth_mm)) +
ylab("bill depth")
info <- expect_doppelganger_built(ggpenguin, "labels-ylab")
labs <- c(info$layout$xaxis$title$text, info$layout$yaxis$title$text)
expect_identical(labs, c("Petal.Width", "sepal width"))
expect_identical(labs, c("bill_length_mm", "bill depth"))
})

test_that("scale_x_continuous(name) is translated correctly", {
ggiris <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width)) +
scale_x_continuous("petal width")
info <- expect_doppelganger_built(ggiris, "labels-scale_x_continuous_name")
ggpenguin <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_depth_mm)) +
scale_x_continuous("bill length")
info <- expect_doppelganger_built(ggpenguin, "labels-scale_x_continuous_name")
labs <- c(info$layout$xaxis$title$text, info$layout$yaxis$title$text)
expect_identical(labs, c("petal width", "Sepal.Width"))
expect_identical(labs, c("bill length", "bill_depth_mm"))
})

test_that("angled ticks are translated correctly", {
ggiris <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width)) +
ggpenguin <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_depth_mm)) +
theme(axis.text.x = element_text(angle = 45))
info <- expect_doppelganger_built(ggiris, "labels-angles")
info <- expect_doppelganger_built(ggpenguin, "labels-angles")
expect_identical(info$layout$xaxis$tickangle, -45)
})

test_that("xaxis/yaxis automargin defaults to TRUE", {
p <- ggplot(iris, aes(Species)) + geom_bar() + coord_flip()
p <- ggplot(palmerpenguins::penguins, aes(species)) + geom_bar() + coord_flip()
l <- plotly_build(p)$x
expect_true(l$layout$xaxis$automargin)
expect_true(l$layout$yaxis$automargin)
Expand All @@ -49,7 +49,8 @@ test_that("factor labels work", {
})

test_that("empty labels work", {
p <- ggplot(iris, aes(Petal.Length, Sepal.Width, color = Species)) +
p <- ggplot(palmerpenguins::penguins,
aes(bill_length_mm, bill_depth_mm, color = species)) +
geom_point() +
labs(x = element_blank(), y = element_blank())
b <- expect_doppelganger_built(p, "labs-element-blank")
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-ggplot-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ test_that("very long legend items", {
info <- expect_traces(p_long_items, 3, "very-long-legend-items")
})

iris$All <- "All species"
p <- qplot(data = iris, x = Sepal.Length, y = Sepal.Width, color = All)
penguins <- palmerpenguins::penguins
penguins$All <- "All species"
p <- qplot(data = penguins, x = bill_length_mm, y = bill_depth_mm, color = All)

test_that("legend is created with discrete mapping regardless of unique values", {
info <- expect_traces(p, 1, "one-entry")
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-ggplot-size.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
context("size")

test_that("size is a vector if it is specified", {
iplot <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width, size=Petal.Length))
L <- expect_doppelganger_built(iplot, "size-is-a-vector")
pplot <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_depth_mm, size=flipper_length_mm))
L <- expect_doppelganger_built(pplot, "size-is-a-vector")
m <- L$data[[1]]$marker
expect_that(m, is_a("list"))
expect_true(length(m$size) > 1)
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/test-ggplot-theme.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
context("ggplot themes")

iris.base <- ggplot(iris) +
geom_point(aes(Petal.Width, Sepal.Width)) +
penguin.base <- ggplot(palmerpenguins::penguins) +
geom_point(aes(bill_length_mm, bill_length_mm)) +
theme_grey()

test_that("background translated correctly",{
ggiris <- iris.base +
ggpenguin <- penguin.base +
theme(panel.background = element_rect(fill = "blue"),
plot.background = element_rect(fill = "green"))
info <- expect_doppelganger_built(ggiris, "theme-background")
info <- expect_doppelganger_built(ggpenguin, "theme-background")
L <- info$layout
expect_true(L$plot_bgcolor == toRGB("blue"))
expect_true(L$paper_bgcolor == toRGB("green"))
})

test_that("grid/ticks translated correctly",{
ggiris <- iris.base +
ggpenguin <- penguin.base +
theme(axis.ticks = element_line(colour = "red"),
panel.grid.major = element_line(colour = "violet"))
info <- expect_doppelganger_built(ggiris, "theme-ticks-and-grids")
info <- expect_doppelganger_built(ggpenguin, "theme-ticks-and-grids")
for (xy in c("x", "y")) {
ax.list <- info$layout[[paste0(xy, "axis")]]
expect_true(ax.list$tickcolor == toRGB("red"))
Expand All @@ -27,17 +27,17 @@ test_that("grid/ticks translated correctly",{
})

test_that("show ticks as 'outside' by default", {
ggiris <- iris.base
info <- expect_doppelganger_built(ggiris, "theme-ticks-default")
ggpenguin <- penguin.base
info <- expect_doppelganger_built(ggpenguin, "theme-ticks-default")
for (xy in c("x", "y")) {
ax.list <- info$layout[[paste0(xy, "axis")]]
expect_identical(ax.list$ticks, "outside")
}
})

test_that("do not show zeroline by default", {
ggiris <- iris.base
info <- expect_doppelganger_built(ggiris, "theme-zeroline-default")
ggpenguin <- penguin.base
info <- expect_doppelganger_built(ggpenguin, "theme-zeroline-default")
for (xy in c("x", "y")) {
ax.list <- info$layout[[paste0(xy, "axis")]]
expect_identical(ax.list$zeroline, FALSE)
Expand All @@ -63,12 +63,12 @@ test_that("marker default shape is a circle", {
})

test_that("plot panel border is translated correctly", {
ggiris <- iris.base + theme_grey() # has no panel.border
info <- expect_doppelganger_built(ggiris, "theme-panel-border-1")
ggpenguin <- penguin.base + theme_grey() # has no panel.border
info <- expect_doppelganger_built(ggpenguin, "theme-panel-border-1")

red <- ggplot(iris) +
red <- ggplot(palmerpenguins::penguins) +
theme_grey() +
geom_point(aes(Petal.Width, Sepal.Width)) +
geom_point(aes(bill_length_mm, bill_depth_mm)) +
theme(panel.border = element_rect(colour = "red", fill = NA))

info <- expect_doppelganger_built(red, "theme-panel-border-2")
Expand Down
30 changes: 15 additions & 15 deletions tests/testthat/test-plotly-color.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ expect_traces <- function(p, n.traces, name){
}

test_that("plot_ly() handles a simple scatterplot", {
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Petal.Width)
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm, color = ~bill_depth_mm)
})

test_that("Mapping a factor variable to color works", {
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species)
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm, color = ~species)
l <- expect_traces(p, 3, "scatterplot-color-factor")
markers <- lapply(l$data, "[[", "marker")
cols <- unlist(lapply(markers, "[[", "color"))
Expand All @@ -24,15 +24,15 @@ test_that("Custom RColorBrewer pallette works for factor variable", {
# convert hex to rgba spec for comparison's sake
colsToCompare <- toRGB(cols)
# specifying a pallette set should "span the gamut"
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm, color = ~species,
colors = "Set1")
l <- expect_traces(p, 3, "scatterplot-color-factor-custom")
markers <- lapply(l$data, "[[", "marker")
colz <- unlist(lapply(markers, "[[", "color"))
idx <- if (packageVersion("scales") > '1.0.0') c(1, 2, 3) else c(1, 5, 9)
expect_identical(sort(colsToCompare[idx]), sort(colz))
# providing vector of RGB codes should also work
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm, color = ~species,
colors = cols[1:3])
l <- expect_traces(p, 3, "scatterplot-color-factor-custom2")
markers <- lapply(l$data, "[[", "marker")
Expand All @@ -51,16 +51,16 @@ test_that("Passing hex codes to colors argument works", {
})

test_that("Mapping a numeric variable to color works", {
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Petal.Width)
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm, color = ~bill_depth_mm)
# one trace is for the colorbar
l <- expect_traces(p, 2, "scatterplot-color-numeric")
idx <- vapply(l$data, is.colorbar, logical(1))
markerScale <- l$data[[which(idx)]]$marker
markerDat <- l$data[[which(!idx)]]$marker
expect_true(all(markerDat$color == iris$Petal.Width))
expect_true(markerScale$colorbar$title == "Petal.Width")
expect_true(min(iris$Petal.Width) == markerScale$cmin)
expect_true(max(iris$Petal.Width) == markerScale$cmax)
expect_true(all(markerDat$color == na.omit(palmerpenguins::penguins$bill_depth_mm)))
expect_true(markerScale$colorbar$title == "bill_depth_mm")
expect_true(min(na.omit(palmerpenguins::penguins$bill_depth_mm)) == markerScale$cmin)
expect_true(max(na.omit(palmerpenguins::penguins$bill_depth_mm)) == markerScale$cmax)
expect_true(all(0 <= markerScale$colorscale[,1] & markerScale$colorscale[,1] <= 1))
})

Expand All @@ -76,20 +76,20 @@ test_that("color/stroke mapping with box translates correctly", {
})

test_that("Custom RColorBrewer pallette works for numeric variable", {
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Petal.Width, colors = "Greens")
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_length_mm,
color = ~bill_depth_mm, colors = "Greens")
# one trace is for the colorbar
l <- expect_traces(p, 2, "scatterplot-color-numeric-custom")
})

test_that("axis titles get attached to scene object for 3D plots", {
p <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, z = ~Sepal.Width)
p <- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_depth_mm, z = ~flipper_length_mm)
l <- expect_traces(p, 1, "scatterplot-scatter3d-axes")
expect_identical(l$data[[1]]$type, "scatter3d")
scene <- l$layout$scene
expect_true(scene$xaxis$title == "Petal.Length")
expect_true(scene$yaxis$title == "Petal.Width")
expect_true(scene$zaxis$title == "Sepal.Width")
expect_true(scene$xaxis$title == "bill_length_mm")
expect_true(scene$yaxis$title == "bill_depth_mm")
expect_true(scene$zaxis$title == "flipper_length_mm")
})

test_that("Can specify a scale manually", {
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-plotly-subplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ test_that("nrows argument works", {
})

test_that("group + [x/y]axis works", {
iris$id <- as.integer(iris$Species)
p <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, color = ~Species,
xaxis = ~paste0("x", id), mode = "markers")
penguins <- palmerpenguins::penguins %>% tidyr::drop_na() %>% arrange(species)
p <- plot_ly(penguins, x = ~bill_length_mm, y = ~bill_depth_mm, color = ~species,
xaxis = ~paste0("x", as.integer(species)), mode = "markers")
s <- expect_traces(subplot(p, margin = 0.05), 3, "group")
ax <- s$layout[grepl("^[x-y]axis", names(s$layout))]
doms <- lapply(ax, "[[", "domain")
Expand Down Expand Up @@ -136,7 +136,10 @@ test_that("subplot accepts a list of plots", {

test_that("ggplotly understands ggmatrix", {
skip_if_not_installed("GGally")
L <- expect_doppelganger_built(GGally::ggpairs(iris), "plotly-subplot-ggmatrix")
penguins <- palmerpenguins::penguins %>% tidyr::drop_na() %>%
select(species, bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g)
L <- expect_doppelganger_built(GGally::ggpairs(penguins),
"plotly-subplot-ggmatrix")
})

test_that("annotation paper repositioning", {
Expand Down
Loading