简体   繁体   English

R plotly:如何从跟踪名称中删除颜色映射变量

[英]R plotly: How to remove color mapping variable from trace name

If I map 'color' to a variable which is neither x, y nor name, the color mapping variable gets added to the trace name on hover and in the legend: 如果我将'color'映射到既不是x,y也不是name的变量,则颜色映射变量会在悬停和图例中添加到跟踪名称中:

df <- data.frame(x = rnorm(5, 50, 1), 
             y = letters[1:5], 
             c = LETTERS[1:5],
             name = c("aa", "bb", "cc", "dd", "ee"),
             stringsAsFactors = FALSE)

p <- plot_ly(df, x = ~x, y = ~y,
             type = "bar", 
             color = ~c,              
             name = ~name,
             hoverinfo = "x+y+name")
p

How can I display only the name without the color mapping variable? 如何只显示没有颜色映射变量的名称?

EDIT 编辑

Below is a more complex example where the color is mapped to a variable that represents a ranking. 下面是一个更复杂的示例,其中颜色映射到表示排名的变量。 The 'name' is some arbitrary name that does not have a ranking. 'name'是一些没有排名的任意名称。 The y variable is an ordered factor that needs to be displayed in the given sequence. y变量是需要以给定顺序显示的有序因子。 I only want to display the name, not the ranking variable. 我只想显示名称,而不是排名变量。

df <- data.frame(x = rnorm(5, 50, 1), 
             y = factor(letters[1:5], 
                        levels = c("a", "e", "c", "d", "b")), 
             c = LETTERS[1:5],
             name = c("qwe", "zxc", "sdf", "bnm", "ert"),
             stringsAsFactors = FALSE)

p <- plot_ly(df, x = ~x, y = ~y,
             type = "bar", 
             color = ~c,  
             colors = brewer.pal(8, "Blues")[4:8],
             name = ~name,
             hoverinfo = "x+y+name")
p

This code sets color = ~name 此代码设置color = ~name

library(plotly)
library(RColorBrewer)

df <- data.frame(x = rnorm(5, 50, 1), 
                 y = factor(letters[1:5], 
                            levels = c("a", "e", "c", "d", "b")), 
                 c = LETTERS[1:5],
                 name = c("qwe", "zxc", "sdf", "bnm", "ert"),
                 stringsAsFactors = FALSE)

p <- plot_ly(df, x = ~x, y = ~y,
             type = "bar", 
             color = ~name,  
             colors = brewer.pal(8, "Blues")[4:8],
             name = ~name,
             hoverinfo = "x+y+name")
p

The plot looks this way: 情节看起来像这样: plotly_example_R

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

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