ggplotly 无法显示 geom_line [英] ggplotly fails to show geom_line
问题描述
我正在尝试使用 plotly 构建交互式图表,但未能显示 geom_line()
,我不知道为什么.这不是我的确切示例,但即使是 plotly 网页上提供的示例也失败了.这是:
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()
在 ggplot
之后看起来像这样:
Which looks like this after the ggplot
:
但是 ggplotly
看起来像这样:
But the ggplotly
looks like this:
什么给?尝试过:ggplot() 中 geom_line() 的应用 和 ggplotly 未正确显示 geom_line 无济于事.
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()
所以我总是可以在 geom_point()
本身分配 geom_point()
属性,但为什么呢?
So I could always just assign the geom_point()
properties at geom_point()
per se, but why?
推荐答案
这可能是一个功能,而不是一个错误:一条线只能有一种颜色.当按剂量分组时,两个分组的点具有不同的 sup 值,例如点剂量 0.5 与 supp 值 OJ 和 VC 有关.所以绘图函数必须处理有两种颜色分配给一条线的情况.
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 通过使用它在数据表中找到的第一种颜色来解决这个问题.如果您对表格重新排序,ggplot 将为线条使用另一种颜色:
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()
相比之下,ggplotly 更喜欢通过不绘制任何线条来解决歧义.如果没有歧义,绘图也会由 ggplotly 完成:
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()
这篇关于ggplotly 无法显示 geom_line的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!