[英]Different behavior between ggplot2 and plotly using ggplotly
I want to make a line chart in plotly
so that it does not have the same color on its whole length. 我想打一个折线图中
plotly
,使其不会对整个长度相同的颜色。 The color is given continuous scale. 颜色以连续刻度给出。 It is easy in
ggplot2
but when I translate it to plotly
using ggplotly
function the variable determining color behaves like categorical variable. 这是很容易在
ggplot2
但是当我把它翻译成plotly
使用ggplotly
函数变量决定像分类变量色彩的行为。
require(dplyr)
require(ggplot2)
require(plotly)
df <- data_frame(
x = 1:15,
group = rep(c(1,2,1), each = 5),
y = 1:15 + group
)
gg <- ggplot(df) +
aes(x, y, col = group) +
geom_line()
gg # ggplot2
ggplotly(gg) # plotly
ggplot2 (desired): ggplot2 (所需):
plotly :
密谋 :
I found one work-around that, on the other hand, behaves oddly in ggplot2
. 另一方面,我发现一个变通办法在
ggplot2
表现ggplot2
。
df2 <- df %>%
tidyr::crossing(col = unique(.$group)) %>%
mutate(y = ifelse(group == col, y, NA)) %>%
arrange(col)
gg2 <- ggplot(df2) +
aes(x, y, col = col) +
geom_line()
gg2
ggplotly(gg2)
I also did not find a way how to do this in plotly directly. 我也没有找到一种方法直接在密谋。 Maybe there is no solution at all.
也许根本没有解决方案。 Any ideas?
有任何想法吗?
It looks like ggplotly is treating group
as a factor, even though it's numeric. 看起来ggplotly将
group
视为一个因素,即使它是数字。 You could use geom_segment
as a workaround to ensure that segments are drawn between each pair of points: 您可以使用
geom_segment
作为解决方法,以确保在每对点之间绘制线段:
gg2 = ggplot(df, aes(x,y,colour=group)) +
geom_segment(aes(x=x, xend=lead(x), y=y, yend=lead(y)))
gg2
ggplotly(gg2)
Regarding @rawr's (now deleted) comment, I think it would make sense to have group
be continuous if you want to map line color to a continuous variable. 关于@rawr(已删除)的注释,如果您要将线色映射到连续变量,我认为让
group
连续是有意义的。 Below is an extension of the OP's example to a group
column that's continuous, rather than having just two discrete categories. 以下是OP示例的扩展,它是一个连续的
group
列,而不是只有两个离散的类别。
set.seed(49)
df3 <- data_frame(
x = 1:50,
group = cumsum(rnorm(50)),
y = 1:50 + group
)
Plot gg3
below uses geom_line
, but I've also included geom_point
. 下面的图
gg3
使用geom_line
,但我也包含了geom_point
。 You can see that ggplotly
is plotting the points. 您可以看到
ggplotly
正在绘制点。 However, there are no lines, because no two points have the same value of group
. 但是,没有行,因为没有两个点的
group
值相同。 If we hadn't included geom_point
, the graph would be blank. 如果我们未包含
geom_point
,则该图将为空白。
gg3 <- ggplot(df3, aes(x, y, colour = group)) +
geom_point() + geom_line() +
scale_colour_gradient2(low="red",mid="yellow",high="blue")
gg3
ggplotly(gg3)
Switching to geom_segment
gives us the lines we want with ggplotly
. 切换到
geom_segment
给我们,我们想用线ggplotly
。 Note, however, that line color will be based on the value of group
at the first point in the segment (whether using geom_line
or geom_segment
), so there might be cases where you want to interpolate the value of group
between each (x,y) pair in order to get smoother color gradations: 但是请注意,该行颜色将立足于价值
group
在该段的第一个点(无论使用geom_line
或geom_segment
),所以要插值的值有可能是案件group
的每个(x之间,y在)以获取更平滑的颜色渐变:
gg4 <- ggplot(df3, aes(x, y, colour = group)) +
geom_segment(aes(x=x, xend=lead(x), y=y, yend=lead(y))) +
scale_colour_gradient2(low="red",mid="yellow",high="blue")
ggplotly(gg4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.