如何在R Plotly中用颜色矢量绘制一条线 [英] How to plot a line with color vector in R Plotly

查看:772
本文介绍了如何在R Plotly中用颜色矢量绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据框:

  ret < -  rnorm(100,0,5)$ b $ (b)=数据帧(
x = seq(1,100,1),
ret = ret,
y = 100 + cumsum(ret),
col = c (ifelse(ret> 0,red,forestgreen),NA)[ - 1]

在这里,我使用名为'ret'的rnorm来模拟一些虚构金融资产的回报,并且定义了一个名为'col'的颜色向量,其中up​​ticks是绿色,downticks是红色的。



我想制作的内容如下所示:

  library(ggplot2)
ggplot(df,aes(x = x,y = y))+ geom_line(aes(color = col,group = 1))



但是我想用一个相似的图像来绘画,我可以放大情节的部分。我的第一个想法是试着简单地使用ggplotly()函数来生成所需的图像:
ggplotly(ggplot(df,aes(x = x,y = y))+ geom_line(aes(color = col,group = 1)))



但情节不再分组。此外,我尝试使用plot_ly(),但似乎无法使线段根据我指定的'col'属性获取其颜色:

  plot_ly(data = df,x =〜x)%> add_lines(y =〜y,line = list(color =〜col))



但是我的颜色参数不会影响线条的颜色。我尝试过其他各种各样的事情,但不断以两个不想要的情节之一结束。任何帮助将不胜感激!



注意:我已经用plot_ly()制作烛台和OHLC图表,但我无法使用它们,因为y轴当您放大绘图的一个小节时,它不会缩放。

解决方案

我能够从 ggplotly 通过使用 geom_segment 并使每个段链接到下一个(x,y)值,无论颜色如何:

  library(dplyr)
df = df%> %x
安排(x)%>%
mutate(x_next = lead(x),y​​_next = lead(y))
$ b $ = ggplot(df,aes(x = x,y = y))+
geom_segment(aes(xend = x_next,yend = y_next,color = col))
ggplotly(p)
pre>

也就是说,对于为什么 ggplotly 不会产生所需的输出在第一位。


Say I have the following data frame:

ret <- rnorm(100, 0, 5)
df <- data.frame(
  x = seq(1, 100, 1),
  ret = ret,
  y = 100 + cumsum(ret),
  col = c(ifelse(ret > 0, "red", "forestgreen"), NA)[-1]
)

Here I'm simulating the returns of some fictional financial asset using rnorm named 'ret', and am defining a color vector named 'col' where upticks are green and downticks are red.

What I want to produce is something like the following:

library(ggplot2)
ggplot(df, aes(x=x, y=y)) + geom_line(aes(colour=col, group=1)) 

But I want to make a similar image using plotly so that I can zoom in on sections of the plot. My first thought was to try simply using the ggplotly() function around the code that produced the desired image:

library(plotly)
ggplotly(ggplot(df, aes(x=x, y=y)) + geom_line(aes(colour=col, group=1)))

But the plot is no longer grouped. Additionally, I tried using plot_ly() but can't seem to make the line segments get their color according to the 'col' attribute that I'm specifying:

plot_ly(data=df, x = ~x) %>% add_lines(y = ~y, line = list(color=~col))

But my color argument doesn't affect the color of the line. I've tried various other things but keep ending up with one of the two undesired plots. Any help would be much appreciated!

Note: I've already made candlestick and OHLC charts with plot_ly(), but I can't work with them because the y axis doesn't scale when you zoom in to a subsection of the plot.

解决方案

I was able to get the desired behaviour from ggplotly by using geom_segment and making each segment link up to the next (x, y) value, regardless of colour:

library(dplyr)
df = df %>%
    arrange(x) %>%
    mutate(x_next = lead(x), y_next = lead(y))

p = ggplot(df, aes(x=x, y=y)) + 
    geom_segment(aes(xend = x_next, yend = y_next, colour=col))
ggplotly(p)

That said, I don't have a good answer for why ggplotly doesn't produce the desired output in the first place.

这篇关于如何在R Plotly中用颜色矢量绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆