plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Add support for scale_*(drop = FALSE)

Open ronmexico2718 opened this issue 9 years ago • 5 comments

I am having trouble getting plotly to respect a drop=FALSE argument for a geom_point() plot where I color by factor and only 1 of the factor levels is represented in the dataset. The code below generates 3 simple plots to demonstrate this

  1. Simple iris plot, color by species, everything works properly
  2. Same plot as 1, but only include data corresponding to a single species. The legend does not display any of the levels.
  3. Same plot as 2, but adding the drop=FALSE argument to make the legend display all factor levels, even if they are not displayed in the dataset. The ggplot plot displays all levels in the legend, but the ggplotly version doesnt display any levels.
library(ggplot2)
library(plotly)

# plot all iris data
p <- ggplot(data=iris, aes(x=Petal.Width, y=Petal.Length, color=Species)) +
      geom_point()

print(p)
layout(ggplotly(p))

# just plot setosa species
setosa <- subset(iris, Species=="setosa")
p_setosa <- ggplot(data=setosa, aes(x=Petal.Width, y=Petal.Length, color=Species)) +
    geom_point()

print(p_setosa)
layout(ggplotly(p_setosa)
       , margin = list(r=100))

# setosa only, add drop=FALSE argument
p_setosa_nodrop <- ggplot(data=setosa, aes(x=Petal.Width, y=Petal.Length, color=Species)) +
  geom_point() +
  scale_color_discrete(drop=FALSE)

print(p_setosa_nodrop)
layout(ggplotly(p_setosa_nodrop)
       , margin = list(r=100))

Session info ------------------------------------------------------------- setting value
version R version 3.2.5 (2016-04-14) system x86_64, mingw32
ui RStudio (0.99.486)
language (EN)
collate English_United States.1252
tz America/New_York
date 2016-04-19

Packages ----------------------------------------------------------------- package * version date source
assertthat 0.1 2013-12-06 CRAN (R 3.2.2)
base64enc 0.1-3 2015-07-28 CRAN (R 3.2.2)
colorspace 1.2-6 2015-03-11 CRAN (R 3.2.2)
DBI 0.3.1 2014-09-24 CRAN (R 3.2.2)
devtools 1.9.1 2015-09-11 CRAN (R 3.2.2)
digest 0.6.9 2016-01-08 CRAN (R 3.2.3)
dplyr 0.4.3 2015-09-01 CRAN (R 3.2.4)
ggplot2 * 2.1.0 2016-03-01 CRAN (R 3.2.4)
gridExtra 2.2.1 2016-02-29 CRAN (R 3.2.5)
gtable 0.2.0 2016-02-26 CRAN (R 3.2.3)
htmltools 0.3.5 2016-03-21 CRAN (R 3.2.5)
htmlwidgets 0.6 2016-02-25 CRAN (R 3.2.5)
httr 1.1.0 2016-01-28 CRAN (R 3.2.5)
jsonlite 0.9.19 2015-11-28 CRAN (R 3.2.5)
labeling 0.3 2014-08-23 CRAN (R 3.2.2)
magrittr 1.5 2014-11-22 CRAN (R 3.2.2)
memoise 0.2.1 2014-04-22 CRAN (R 3.2.2)
munsell 0.4.3 2016-02-13 CRAN (R 3.2.3)
plotly * 3.5.0 2016-04-19 Github (ropensci/plotly@43d882d) plyr 1.8.3 2015-06-12 CRAN (R 3.2.2)
R6 2.1.2 2016-01-26 CRAN (R 3.2.3)
Rcpp 0.12.4 2016-03-26 CRAN (R 3.2.4)
scales 0.4.0 2016-02-26 CRAN (R 3.2.3)
tidyr 0.4.1 2016-02-05 CRAN (R 3.2.5)
viridis 0.3.4 2016-03-12 CRAN (R 3.2.4)
yaml 2.1.13 2014-06-12 CRAN (R 3.2.2)

ronmexico2718 avatar Apr 19 '16 16:04 ronmexico2718

Is there an option in base plot_ly call to specify drop = FALSE type option? I am not using ggplotly. But, can't find a way to preserve missing categories on the plot.

ghost avatar Nov 06 '16 20:11 ghost

What would be the proposed workaround for now?

I've tried adding extra rows, with the (dropped) factor levels and NA values in the other columns. But these rows have been ignored by plotly also.

henningsway avatar Dec 17 '18 12:12 henningsway

It would be great, if ggplotly could support this.

As a workaround, I add dummy data in to the plot data to make the legend work @henningsway

davidhodge931 avatar May 13 '20 19:05 davidhodge931

Is there already a workaround for this? Would love to have this option.

QuintenSand avatar Mar 24 '23 15:03 QuintenSand

I know this thread is old but the issue is still active (as far as I can tell), so hopefully the below helps anyone else who stumbles upon it...

Is there already a workaround for this? Would love to have this option.

As @davidhodge931 noted, the workaround is to ensure there's a dummy value for each of your factor levels. How you do this will depend on what format your data is in. As an example, for a simple bar chart:

# Original data
df <- data.frame(
  category = factor(c("A", "B"), levels = c("A", "B", "C")),
  value = c(10, 20)
)

# Add dummy rows for missing categories 
all_levels <- levels(df$category)
present_levels <- unique(df$category)
missing_levels <- setdiff(all_levels, present_levels)

dummy_data <- data.frame(
  category = factor(missing_levels, levels = all_levels),
  value = NA  # NA ensures it doesn't show in the plot
)

df_full <- bind_rows(df, dummy_data)

# Plot
p <- ggplot(df_full, aes(x = category, y = value, fill = category)) +
  geom_bar(stat = "identity", na.rm = TRUE) +
  scale_fill_manual(
    values = c("A" = "red", "B" = "blue", "C" = "green")
  )

# Convert to interactive plot
ggplotly(p)

It could be a bit more complex than this - for example, I wanted to use drop=FALSE with a choropleth. For this, I had to redraw new areas, setting the geom_sf fill value to read from a different variable to the scale_fill_manual value.

nc011 avatar Jun 23 '25 12:06 nc011