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

时间:2017-06-11 06:35:19

标签: r plotly

如果我映射颜色'对于既不是x,y也不是名称的变量,颜色映射变量会在悬停时和图例中添加到跟踪名称中:

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

如何只显示没有颜色映射变量的名称?

修改

下面是一个更复杂的示例,其中颜色映射到表示排名的变量。这个名字&#39;是一些没有排名的任意名称。 y变量是需要以给定顺序显示的有序因子。我只想显示名称,而不是排名变量。

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

1 个答案:

答案 0 :(得分:0)

此代码设置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

情节看起来像这样: plotly_example_R