问题描述
我有一堆来自同一主题研究的配对"观察结果,我正在尝试构建一个意大利面条图来可视化这些观察结果,如下所示:
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
,我们在列中添加包含 NA
的行提供 x
和 y
坐标).
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 中的分组线图:如何控制线条颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!