R ggplot + ggplotly:色标不起作用

R ggplot + ggplotly: color scale doesn't work

从 ggplotly 开始,我似乎无法弄清楚如何使用色标。这是我得到的:

> dput(set.df)
structure(list(Set.name = structure(1:6, .Label = c("set_3_1", 
"set_3_2", "set_3_3", "set_3_4", "set_3_5", "set_3_6"), class = "factor"), 
    Set.size = c(36202L, 31389L, 74322L, 181981L, 204571L, 347844L
    ), TF.number = c(91, 16, 38, 91, 91, 91), Ref.set.size = c(3830L, 
    155L, 725L, 3830L, 3830L, 3830L), False.negatives = c(107L, 
    7L, 100L, 1159L, 1744L, 2310L), Sensitivity = c(0.972062663185379, 
    0.954838709677419, 0.862068965517241, 0.697389033942559, 
    0.544647519582245, 0.39686684073107), Specificity = c(0.0790835553530113, 
    0.296607846658412, 0.296159447758514, 0.300796981749767, 
    0.325487685108514, 0.451174177879625), Precision = c(0.00959662223642798, 
    0.00342695718619029, 0.00608000311296159, 0.0110294421274311, 
    0.0094999544585117, 0.0199195355603025)), row.names = c(NA, 
-6L), class = "data.frame")

plot <- ggplot(set.df, aes(key=Set.name, size = Set.size, fill = TF.number, x = Specificity, y = Sensitivity)) + 
  geom_point(colour="#ffffff00") +
  expand_limits(x = 0, y = 0) +
  geom_hline(yintercept = 0.8, linetype = "dotted", colour= 'blue') +
  scale_fill_continuous(low='skyblue', high='midnightblue')

ggplotly(plot, tooltip =  c("Set.name", "Set.size", "TF.number", "Ref.set.size", "False.negatives", "Sensitivity", "Specificity", "Precision"))

输出为:

这些点没有出现在预期的蓝色细微差别中,我收到以下警告消息:

Warning message:
In L$marker$color[idx] <- aes2plotly(data, params, "fill")[idx] :
  number of items to replace is not a multiple of replacement length

此外,不知何故点大小图例没有显示。

感谢您的意见!

主要问题是 fill 美学 geom_point() 仅适用于某些形状(有边框的形状)。这不包括默认形状,因此您需要改用 color 美学。至于为什么没有显示尺寸图例,ggplotly 还没有原生支持多个图例。参见 here

myplot <- ggplot(set.df, aes(key = Set.name, size = Set.size, color = TF.number, x = Specificity, y = Sensitivity)) + 
  geom_point() +
  expand_limits(x = 0, y = 0) +
  geom_hline(yintercept = 0.8, linetype = "dotted", colour= 'blue') +
  scale_color_continuous(low='skyblue', high='midnightblue')

ggplotly(myplot, tooltip =  c("Set.name", "Set.size", "TF.number", "Ref.set.size", "False.negatives", "Sensitivity", "Specificity", "Precision"))