Plotly R中的分组线图:如何控制线色? [英] Grouped line plots in Plotly R: how to control line color?

查看:455
本文介绍了Plotly R中的分组线图:如何控制线色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从同一主题的研究中获得了一堆成对"的观察结果,并且我试图构建一个意粉图以将这些观察结果可视化,如下所示:

I have a bunch of 'paired' observations from a study for the same subject, and I am trying to build a spaghetti plot to visualize these observations as follows:

library(plotly)
df <- data.frame(id = rep(1:10, 2),
                 type = c(rep('a', 10), rep('b', 10)),
                 state = rep(c(0, 1), 10),
                 values = c(rnorm(10, 2, 0.5), rnorm(10, -2, 0.5)))
df <- df[order(df$id), ]
plot_ly(df, x = type, y = values, group = id, type = 'line') %>%
  layout(showlegend = FALSE)

它会产生我正在寻找的正确情节.但是,代码以自己的颜色显示每个分组的行,这确实很烦人并且分散了注意力.我似乎找不到消除颜色的方法.

It produces the correct plot I am seeking. But, the code shows each grouped line in own color, which is really annoying and distracting. I can't seem to find a way to get rid of colors.

奖金问题:我实际上想使用color = state并实际上使用该变量为倾斜的线条着色.

Bonus question: I actually want to use color = state and actually color the sloped lines by that variable instead.

有什么方法/想法吗?

推荐答案

您可以将线条设置为相同的颜色

You can set the lines to the same colour like this

plot_ly(df, x = type, y = values, group = id, type = 'scatter', mode = 'lines+markers', 
        line=list(color='#000000'), showlegend = FALSE)

对于一对一"的价格问题有两个奖励:如何使用与用于分组的变量不同的颜色进行着色":

For the 'bonus' two-for-the-price-of-one question 'how to color by a different variable to the one used for grouping':

如果您仅绘制标记而没有线条,这将很简单,因为您可以简单地为marker.color提供颜色矢量.但是,不幸的是,line.color只接受一个值,而不是一个向量,因此我们需要解决此限制.

If you were only plotting markers, and no lines, this would be simple, as you can simply provide a vector of colours to marker.color. Unfortunately, however, line.color only takes a single value, not a vector, so we need to work around this limitation.

如果数据不是太多(在这种情况下此方法变慢,并且下面给出了一种更快的方法),则可以通过将它们分别作为单独的迹线循环添加(循环)来分别设置每行的颜色超过ID)

Provided the data are not too numerous (in which case this method becomes slow, and a faster method is given below), you can set colours of each line individually by adding them as separate traces one by one in a loop (looping over id)

p <- plot_ly()
for (id in df$id) {
  col <- c('#AA0000','#0000AA')[df[which(df$id==id),3][1]+1] # calculate color for this line based on the 3rd column of df (df$state).
  p <- add_trace(data=df[which(df$id==id),], x=type, y=values, type='scatter', mode='markers+lines',
                 marker=list(color=col),
                 line=list(color=col), 
                 showlegend = FALSE,
                 evaluate=T)
  }
p

尽管从概念上讲,这种每行一线的方法可能是最简单的方法,但是如果将其应用于数百或数千个线段,它的确会变得(非常不切实际)缓慢.在这种情况下,有一种更快的方法,每种颜色只绘制一条线,但是通过在单独的线段之间插入NA并使用connectgaps=FALSE选项将线断开,可以将该线分成多个线段分成缺少数据的段.

Although this one-trace-per-line approach is probably the simplest way conceptually, it does become very (impractically) slow if applied to hundreds or thousands of line segments. In this case there is a faster method, which is to plot only one line per colour, but to split this line up into multiple segments by inserting NA's between the separate segments and using the connectgaps=FALSE option to break the line into segments where there are missing data.

首先使用dplyr在行段之间插入缺失值(即,对于每个唯一的id,我们在提供xy坐标的列中添加一行包含NA的行).

Begin by using dplyr to insert missing values between line segements (i.e. for each unique id we add a row containing NA in the columns that provide x and y coordinates).

library(dplyr)
df %<>% distinct(id) %>%
  `[<-`(,c(2,4),NA) %>%
  rbind(df) %>%
  arrange (id)

并使用connectgaps=FALSE绘制:

plot_ly(df, x = type, y = values, group = state, type = 'scatter', mode = 'lines+markers', 
        showlegend = FALSE,
        connectgaps=FALSE)

这篇关于Plotly R中的分组线图:如何控制线色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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