简体   繁体   English

操纵R情节传奇

[英]Manipulating R plotly legend

Probably an easy question: 可能是一个简单的问题:

Trying to use plotly to produce a scatter plot and customize the legend. 尝试使用plotly生成散点图并自定义图例。

Here's my data: 这是我的数据:

require(plotly)
set.seed(1)

my.df <- data.frame(id=LETTERS[sample(26,100,replace=T)],x=rnorm(100),y=rnorm(100),sig=runif(100,0,1),stringsAsFactors = F)

In my case I'm calling this with the column indices, thus: 在我的情况下,我用列索引调用它,因此:

x.idx <- which(colnames(my.df) == "x")
y.idx <- which(colnames(my.df) == "y")
sig.idx <- which(colnames(my.df) == "sig")
sig.name <- "p-value"

And what I want to do is have the legend title be sig.name and make the legend smaller than the default size. 我想要做的是将图例标题设为sig.name并使图例小于默认大小。 So I'm trying: 所以我在尝试:

p <- plot_ly(x=~my.df[,x.idx],y=~my.df[,y.idx],color=~my.df[,sig.idx],text=~my.df$id,colors=c("darkblue","darkred")) %>% add_annotations(text=sig.name,xref="paper",yref="paper",xanchor="left",x=1,y=1,yanchor="top",legendtitle=T,showarrow=FALSE) %>% layout(legend=list(y=0.8,yanchor="top"),xaxis=list(title=colnames(my.df)[x.idx],zeroline=F),yaxis=list(title=colnames(my.df)[y.idx],zeroline=F))

Which gives me: 这给了我: 在此输入图像描述

Not exactly what I want. 不完全是我想要的。

So: 所以:

  1. How do I delete the default legend title? 如何删除默认图例标题?

  2. How do I make the legend smaller? 如何使图例变小?

You are probably looking for colorbar: len and colorbar: title . 您可能正在寻找colorbar: lencolorbar: title 在此输入图像描述

plot_ly(type = 'scatter', 
        mode = 'markers', 
        x = ~my.df[,x.idx],
        y = ~my.df[,y.idx],
        color = ~my.df[,sig.idx],
        text =~my.df$id,
        colors = c("darkblue","darkred"),
        marker = list(colorbar = list(len = 0.2, 
                                      title = sig.name)
        )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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