4

I'm trying to build an interactive chart using plotly, but it's failing to show the geom_line() and I'm not sure why. This isn't my exact example, but even the example provided on plotly's webpage fails. Here it is:

library(plotly)
datn <- read.table(header=TRUE, text='
supp dose length
                   OJ  0.5  13.23
                   OJ  1.0  22.70
                   OJ  2.0  26.06
                   VC  0.5   7.98
                   VC  1.0  16.77
                   VC  2.0  26.14
                   ')

## This one works fine (original example):
ggplot(data=datn, aes(x=dose, y=length, group=supp, colour=supp)) +  geom_line() +  geom_point()
ggplotly()

## This one doesn't (modified the group):
ggplot(data=datn, aes(x=dose, y=length, group=dose, colour=supp)) +  geom_line() +  geom_point()
ggplotly()

Which looks like this after the ggplot:

enter image description here

But the ggplotly looks like this:

enter image description here

What gives? Tried: The appliance of geom_line() in ggplot() and ggplotly not displaying geom_line correctly to no avail.

UPDATE: If I omit the color, the lines plot correctly:

ggplot(data=datn, aes(x=dose, y=length, group=dose)) +          
geom_line(aes(group=dose)) +  geom_point()
ggplotly()

So I could always just assign the geom_point() properties at geom_point() per se, but why?

Community
  • 1
  • 1
Amit Kohli
  • 2,860
  • 2
  • 24
  • 44

2 Answers2

0

It's doing exactly what you are coding it to. For example, take the first one and take out the group parameter entirely in the aes() function:

ggplot(data=datn, aes(x=dose, y=length, colour=supp)) +  geom_line()

You get the exact same plot as before.

In your second example, you are coloring by the supp but grouping by the dose. For example, when dose is 0.5, the lengths are 13.23 and 7.98. There is a line in the plot which connects points (0.5, 7.98) and (0.5, 13.23). Since you are grouping by dose, the lines will be plotted based on the dose groups which are 0.5, 1, and 2.

  • Right, that's what I want, but ggplotly() is omitting those lines. That's my question. I'm uploading a picture. – Amit Kohli Sep 29 '16 at 17:14
0

It might be a feature, not a bug: A single line can only have a single color. When grouping for dose, the two grouped points have different values of sup, e.g. point dose 0.5 is related to supp values OJ and VC. So the plotting function has to deal with the situation of having two colors to assign to a single line.

ggplot solves this problem by using the first color it finds in the datatable. If your reorder the table, ggplot will use the other color for the lines:

datn = datn[order(datn$supp, decreasing = T),]

ggplot(data=datn, aes(x=dose, y=length, group=dose, colour=supp)) +  geom_line() +  geom_point()

In contrast, ggplotly prefers to solve the ambiguity by not plotting any line. If no ambiguity is there, plotting will be also done by ggplotly:

datn$supp2 = c("a", "b", "b", "a", "b", "b")
ggplot(data=datn, aes(x=dose, y=length, group=dose, colour=supp2)) +  geom_line() +  geom_point()
ggplotly()