R ggplot + ggplotly : color scale doesn't work

标签 r ggplot2 ggplotly

从 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"))

输出是:

output

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

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

此外,不知何故,磅值图例没有显示。

感谢您的意见!

最佳答案

主要问题是 geom_point()fill 美学仅适用于某些形状(有边框的形状)。这不包括默认形状,因此您需要使用 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"))

enter image description here

关于R ggplot + ggplotly : color scale doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921898/

相关文章:

r - ggplot 标题不起作用?

r - ggplot : rescale axis (log) and cut axis

r - ggplotly() 不将日期放在 x 轴上

r - 使用 R 中的 ggplotly() 绘制图中点旁边的标签

r - 在ggplotly散点图中添加自定义数据标签

r - 如何使用/维护与另一个 R 包中的 S4 类同名的 S4 类?

r - 需要帮助从该网站抓取表格数据

mysql - 如何避免将大型文件并行加载到mysql中的超时错误

r - ggplot 错误 : similar data graphs, 为什么不再?

r - ggplot geom_smooth 排除负值