rioualen
rioualen

Reputation: 968

R ggplot + ggplotly: color scale doesn't work

Beginning with ggplotly, and I can't seem to figure out how to use the colour scale. Here's what I've got:

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

and

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

and the output is:

output

The points don't show up in the expected nuance of blue, and I get the following warning message:

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

Also, somehow the point size legend doesn't show up.

Thanks for your input!

Upvotes: 2

Views: 1263

Answers (1)

lroha
lroha

Reputation: 34406

The main issue is that the fill aesthetic for geom_point() is only applicable to certain shapes (ones that have a border). This doesn't include the default shape so you need to use the color aesthetic instead. As for why the size legend doesn't show, ggplotly doesn't yet natively support multiple legends. See 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

Upvotes: 3

Related Questions