[英]Remove parenthesis from ggplotly legend
I found this example here which provides a perfect way of getting rid of the extra terms added by ggplotly to the legends.我在这里找到了这个例子,它提供了一种完美的方式来摆脱 ggplotly 添加到图例中的额外术语。
My problem is that the code in the solution removes everything after a comma ,
and I have an actual comma is my legends which are in the format of DIC, Spliced
我的问题是解决方案中的代码删除了逗号后的所有内容,
而我有一个实际的逗号是我的图例,其格式为DIC, Spliced
The code is:代码是:
for (i in 1:length(myplot$x$data)){
if (!is.null(myplot$x$data[[i]]$name)){
myplot$x$data[[i]]$name = gsub("\\(","",str_split(myplot$x$data[[i]]$name,",")[[1]][1])
}
}
My original plot has this kind of legend:我原来的plot有这样的传说:
After applying the code, I loose my Spliced/Uspliced
terms in my legends应用代码后,我在我的传说中失去了我的Spliced/Uspliced
术语
So plotly
always turns the new legend to be (orignal_legend,1)
(Weird).所以plotly
总是把新的图例变成(orignal_legend,1)
(Weird)。 Is it always ,1)
?总是,1)
吗? or can be ,2)
or any other number.或可以是,2)
或任何其他数字。 I don't know.我不知道。
If your original legend is (DIC, Spliced, 1)
it will become ((DIC, Spliced, 1), 1)
with plotly
.如果您的原始图例是(DIC, Spliced, 1)
它将变为((DIC, Spliced, 1), 1)
和plotly
。 So you have to remove the beginning round brackets and ending ,1)
from the legend.因此,您必须从图例中删除开头的圆括号和结尾的,1)
。 Try this regex with gsub
:用gsub
试试这个正则表达式:
for (i in 1:length(myplot$x$data)){
if (!is.null(myplot$x$data[[i]]$name)){
myplot$x$data[[i]]$name = gsub('^\\(|,\\d+\\)$', '', myplot$x$data[[i]]$name)
}
}
As mentioned earlier I am not sure if it is going to be always ,1)
or can be ,2)
as well, so to be on the safer side I have used \\d+
which will remove any number at the end of the string.如前所述,我不确定它是否总是,1)
或也可以是,2)
,所以为了更安全,我使用了\\d+
它将删除字符串末尾的任何数字.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.