在R plotly中操作传奇文本

时间:2017-04-16 01:39:56

标签: r legend plotly

我有一个R我希望使用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=F) df$group <- factor(df$group,levels=1:4) df$treatment <- factor(df$treatment,levels=c("A","B")) 分散情节,其中包含两个我喜欢颜色和形状的因素由

这是我的数据:

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"))

这是我试图策划的方式:

group

给了我: Gulp - jQuery is not defined

是否可以将图例中的treatmentscanf文本用逗号分隔,而不是像现在这样用新行分隔?

这意味着代替:

1

A

2

A

3

4

我会:

1,A

2,A

3,B

4,B

1 个答案:

答案 0 :(得分:5)

听起来微不足道,但这是Plotly为你决定的案例之一。

图例标签由colorsymbol的类别组成,这些类别都在一个命令中传递。为了控制输出,让我们分别添加每个跟踪。

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 = ",")
                     )
    }
  }
}

我们通过colormarker通过symbol传递marker(非绝对必要)(两者都可以通过add_trace命令传递但是再次Plotly决定你该怎么做。)

图例标签通过name传递。

注意:您需要明确转换您的治疗,因为符号需要命名符号或数字(除非您的治疗方法被命名为diamondcircle

enter image description here

完整代码

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