简体   繁体   English

ggplot2和ggplotly之间的行为不同

[英]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_linegeom_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.

相关问题 在R中以绘图方式在子图之间共享轴和图例(ggplot2中的构面和使用ggplotly不起作用) - Sharing axes and legends between subplots in plotly in R (faceting in ggplot2 and using ggplotly doesn't work) 使用ggplot2和ggplotly创建Sankey Diagram - Creating Sankey Diagram using ggplot2, plotly and ggplotly 使用ggplotly将ggplot2转换为plotly时,曲线混乱 - curves messed up when converting ggplot2 to plotly using ggplotly 使用 ggplotly(ggplot2 with plotly)时,你能去掉注释中的跟踪标签吗? - Can you get rid of the trace labels in the annotations when using ggplotly (ggplot2 with plotly)? R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数 - R plotly: How to keep width parameter of ggplot2 geom_bar() when using ggplotly() 如何在使用`ggplotly`将ggplot2转移到图表时更改图例位置? - How to change the legend position when transfer ggplot2 to plotly using `ggplotly`? 无法使用 ggplotly 转换 ggplot2 - Unable to convert ggplot2 using ggplotly stat_summary:qplot和ggplot2之间的行为不同/了解ggplot2的范例 - stat_summary: different behavior between qplot & ggplot2 / understanding the paradigm of ggplot2 使用ggplotly时,不会解析ggplot2图形中的标签(注释) - Labels (annotate) in ggplot2 graphics are not parsed when using ggplotly ggplotly 主题与 ggplot2 不一致 - ggplotly theme inconsistency with ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM