[英]Manipulating legend text in R 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. 我有一个data.frame
我想用R
的plotly
分散绘图,其中有两个因素,我想用颜色和形状。
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"))
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? 是否可以将图例中的group
和treatment
文本用逗号分隔,而不是像现在一样用新行分隔?
This means that instead of: 这意味着代替:
1 1
A 一种
2 2
A 一种
3 3
B 乙
4 4
B 乙
I'll have: 我会:
1,A 1,A
2,A 2,A
3,B 3,B
4,B 4,B
Sounds trivial but it's one of the cases where Plotly
decides whats good for you. 听起来微不足道,但这是Plotly
决定什么对你有利的案例之一。
The legend labels are composed of the categories of color
and symbol
which are all passed in one command. 图例标签由color
和symbol
的类别组成,这些类别都在一个命令中传递。 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). 我们通过marker
和symbol
也通过marker
传递color
(不是绝对必要的)(两者都可以在add_trace
命令中传递,但然后再次Plotly
决定你该怎么做)。
The legend label is passed via name
. 图例标签通过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
) 注意:您需要明确转换您的治疗,因为符号需要命名符号或数字(除非您的治疗方法被命名为diamond
或circle
)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.