Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating legend text in R plotly

Tags:

r

legend

plotly

I have a data.frame I'd like to scatter plot using R's plotly with two factors which I'd like to color and shape by.

Here's my data:

set.seed(1)
df <- data.frame(x=rnorm(12),y=rnorm(12),
                 group=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3)),
                 treatment=c(rep("A",6),rep("B",6)),
                 stringsAsFactors=F)

df$group <- factor(df$group,levels=1:4)
df$treatment <- factor(df$treatment,levels=c("A","B"))

Here's how I'm trying to plot:

require(plotly)

plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$group,symbol=~df$treatment) %>% 
  add_annotations(text="group,treatment",xref="paper",yref="paper",x=1.02, xanchor="left",y=1.02,yanchor="top",legendtitle=TRUE,showarrow=FALSE) %>%
  layout(xaxis=list(title="x"),yaxis=list(title="y"))

which gives me: enter image description here

Is it possible to get the text of group and treatment in the legend be separated by comma instead of the new line as it is now?

This means that instead of:

1

A

2

A

3

B

4

B

I'll have:

1,A

2,A

3,B

4,B

like image 900
dan Avatar asked Apr 16 '17 01:04

dan


People also ask

How do you show legends in Plotly?

Users may show or hide traces by clicking or double-clicking on their associated legend item.

How to hide legend in plotly r?

Grouped Legend Plotly legends are interactive. Click on the legend entries to hide and show traces. The legendgroup key groups legend entries so that clicking on one legend entry will hide or show all of the traces in that group.

How to hide legend in plotly?

In this example, we are hiding legend in Plotly with the help of method fig. update(layout_showlegend=False), by passing the showlegend parameter as False.


1 Answers

Sounds trivial but it's one of the cases where Plotly decides whats good for you.

The legend labels are composed of the categories of color and symbol which are all passed in one command. In order to get control over the output, let's add each trace separately.

for (grou in groups) {
  for (treat in treatments) {
    trace_data <- subset(df, group == grou & treatment == treat)
    if (nrow(trace_data) > 0) {
      p <- add_trace(p,
                     x = trace_data$x,
                     y = trace_data$y,
                     marker = list(size = 10,
                                   color = group,
                                   symbol = as.integer(charToRaw(treat)) - 65),
                     type = 'scatter',
                     mode = "markers",
                     name = paste(grou, treat, sep = ",")
                     )
    }
  }
}

We pass the color (not strictly necessary) via marker and symbol also via marker (both can be passed in the add_trace command as well but then again Plotly decides for you what do to do with it).

The legend label is passed via name.

Note: You need to convert your treatment explicitly because symbol expects either a named symbol or a number (unless your treatments are named diamond or circle)

enter image description here

Complete code

library(utils)
library(plotly)

set.seed(1)
df <- data.frame(x = rnorm(12),
                 y = rnorm(12),
                 group = c(rep(1, 3),
                           rep(2, 3),
                           rep(3, 3),
                           rep(4, 3)
                           ),
                 treatment=c(rep("A", 6),
                             rep("B", 6)
                             ),
                 stringsAsFactors = FALSE
                 )

groups <- unique(df$group)
treatments <- unique(df$treatment)

p <- plot_ly()
for (grou in groups) {
  for (treat in treatments) {
    trace_data <- subset(df, group == grou & treatment == treat)
    if (nrow(trace_data) > 0) {
      p <- add_trace(p,
                     x = trace_data$x,
                     y = trace_data$y,
                     marker = list(size = 10,
                                   color = group,
                                   symbol = as.integer(charToRaw(treat)) - 65),
                     type = 'scatter',
                     mode = "markers",
                     name = paste(grou, treat, sep = ",")
                     )
    }
  }
}
p <- add_annotations(p, 
                     text = "group,treatment",
                     xref = "paper",
                     yref = "paper",
                     x = 0.96, 
                     xanchor = "left",
                     y = 1.03,
                     yanchor = "top",
                     legendtitle = TRUE,
                     showarrow = FALSE) %>%
  layout(xaxis = list(title = "x"),
         yaxis = list(title = "y"))

p
like image 178
Maximilian Peters Avatar answered Oct 14 '22 05:10

Maximilian Peters