简体   繁体   中英

Prevent ggplotly to drop unused factor levels from legend

I have this dataframe:

> dd
   x xend         y     yend group
7  1    1 -2.592981 2.531884     C
8  2    2 -3.052151 2.570183     A
9  3    3 -3.255346 2.663808     C
10 4    4 -3.456388 2.905152     A
> str(dd)
'data.frame':   4 obs. of  5 variables:
 $ x    : num  1 2 3 4
 $ xend : num  1 2 3 4
 $ y    : num  -2.59 -3.05 -3.26 -3.46
 $ yend : num  2.53 2.57 2.66 2.91
 $ group: Factor w/ 3 levels "A","B","C": 3 1 3 1

I want to plot some segments colored by the group factor, without dropping the unused factor levels from the legend. Like this:

gg <- ggplot(dd, aes(x, y, color=group)) +
   scale_colour_discrete(drop = FALSE) +
   geom_segment(aes(xend=xend, yend=yend))

This works as expected:

绘图

But now I want a plotly graphic. And ggplotly drops the unused levels:

ggplotly(gg)

在此处输入图片说明

Is there a way to prevent ggplotly to drop the unused factor levels?

The dataframe:

> dput(dd)
structure(list(x = c(1, 2, 3, 4), xend = c(1, 2, 3, 4), y = c(-2.59298110869713, 
-3.05215109954588, -3.25534630590118, -3.45638777944259), yend = c(2.53188374522142, 
2.57018254452851, 2.66380767012015, 2.90515183040407), group = structure(c(3L, 
1L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("x", 
"xend", "y", "yend", "group"), class = "data.frame", row.names = 7:10)

Packages versions:

> packageVersion("ggplot2")
[1] ‘2.2.1.9000’
> packageVersion("plotly")
[1] ‘4.6.0’

As far as I know there is no direct way of doing it but you could add a "dummy" line for each missing group value, eg

for (diff in setdiff(levels(dd$group), unique(dd$group))) {
  dummy_name = paste0('dummy_', diff) 
  dd[dummy_name,] <- dd[1,]
  for (n in names(dd[1,])) {
    dd[dummy_name,][n] = NaN
  }
  dd[dummy_name,]$group <- diff
}

which will give the following dataframe

 x xend y yend group 7 1 1 -2.592981 2.531884 C 8 2 2 -3.052151 2.570183 A 9 3 3 -3.255346 2.663808 C 10 4 4 -3.456388 2.905152 A dummy_B NaN NaN NaN NaN B

and the following plot

在此处输入图片说明


dd = structure(list(x = c(1, 2, 3, 4), 
                    xend = c(1, 2, 3, 4), 
                    y = c(-2.59298110869713, -3.05215109954588, -3.25534630590118, -3.45638777944259), 
                    yend = c(2.53188374522142, 2.57018254452851, 2.66380767012015, 2.90515183040407), 
                    group = structure(c(3L, 1L, 3L, 1L), 
                                      .Label = c("A", "B", "C"), 
                                      class = "factor")), 
               .Names = c("x", "xend", "y", "yend", "group"), 
               class = "data.frame", 
               row.names = 7:10)
for (diff in setdiff(levels(dd$group), unique(dd$group))) {
  dummy_name = paste0('dummy_', diff) 
  dd[dummy_name,] <- dd[1,]
  for (n in names(dd[1,])) {
    dd[dummy_name,][n] = NaN
  }
  dd[dummy_name,]$group <- diff
}

gg <- ggplot(dd, aes(x, y, color=group)) +
  scale_colour_discrete(drop = FALSE) +
  geom_segment(aes(xend=xend, yend=yend))

gg
ggplotly(ggplot(dd, aes(x, y, color=group)) +
           scale_colour_discrete(drop = FALSE) +
           geom_segment(aes(xend=xend, yend=yend)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM