r - 为多条轨迹绘制颜色条和色阶
问题描述
我正在尝试绘制不同月份的数据,不同的轨迹代表时间序列中的数据的一年,例如dataframe
. 我想要的输出是下图的交互式版本,给定一个 long data.frame
。(下面给出的数据和代码)
数据的视觉检查 -
y year time
1 15.70525 2005 0.41666667
2 15.82955 2005 0.50000000
3 17.55470 2005 0.58333333
4 18.10086 2005 0.66666667
5 17.49667 2005 0.75000000
6 19.34727 2005 0.83333333
7 20.03129 2005 0.91666667
8 23.48669 2006 0.00000000
9 12.53699 2006 0.08333333
10 15.46702 2006 0.16666667
存在ggplotly
解决方案,但我想避免它。因此,在 native 中plotly
,如何 -
data.frame
用分组变量定义的每条迹线的数据绘制多条迹线。- 在.
colorscale
_colorbar
我有一个适用于 1 的解决方案,但如果它是惯用的方式,我不会起诉plotly
;但是,我完全停留在 2 上。
我通过将数据从长转换为宽并为每列添加跟踪来实现第 1 点。对于一些预定义的颜色,我的方法如下 -
library(plotly)
yr.names <- as.character(unique(data[['year']]))
colours <- c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")
data <- stats::reshape(
data = data, direction = 'wide', idvar = c('time'),
timevar = 'year', v.names = 'y'
)
colnames(data)[2:ncol(data)] <- yr.names
data <- data[order(data[['time']]), ]
p <- plot_ly(data = data)
for (i in seq_along(yr.names)) {
p <- add_trace(
p = p, y = data[[yr.names[i]]], x = ~time, name = yr.names[i],
type = 'scatter', mode = 'lines', line = list(color = colours[i])
)
}
p
接下来,我如何判断plotly
在year
我的情况下是一个连续变量?换句话说,我如何手动定义一个colorbar
/colorscale
来做我想做的事情?
这是我的尝试不起作用 -
nCol <- length(colours)
colourscale <- data.frame(
y = seq(0, 1, length.out = nCol),
col = as.character(colours)
)
p <- plot_ly(
x = seq(0, 1, length.out = nCol), type = 'scatter', mode = 'markers',
y = mean(data[[yr.names[1]]], na.rm = TRUE), hoverinfo = 'none', marker = list(
size = rep(0, nCol), color = as.numeric(yr.names), colorscale = colourscale,
colors = colours,
colorbar = list(
title = 'Year', nticks = nCol
)
)
)
for (i in seq_along(yr.names)) {
p <- add_trace(
p = p, y = data[[yr.names[i]]], x = data[['time']], name = yr.names[i],
type = 'scatter', mode = 'lines+markers', line = list(color = colours[i]),
showlegend = FALSE
)
}
p
这产生 -
这显然是不正确的。任何指针?
问题的数据
data <- data.frame(
y = c(
15.705248, 15.82955, 17.554701, 18.100864, 17.496668, 19.347265,
20.031291, 23.486694, 12.536987, 15.467018, 14.233539, 17.783058,
16.291602, 16.980282, 18.612189, 16.623343, 21.430241, 23.575517,
23.334206, 28.038383, 16.763869, 19.792754, 16.427305, 21.000742,
20.681002, 21.83489, 23.93020353, 22.93035694, 23.26333992,
25.25003022, 25.80609, 29.665356, 21.654285, 18.264945, 23.107677,
22.91251, 19.43174
), year = c(
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006,
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007,
2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2008, 2008,
2008, 2008, 2008, 2008
), time = c(
0.416666666666667, 0.5, 0.583333333333333,
0.666666666666667, 0.75, 0.833333333333333, 0.916666666666667,
0, 0.0833333333333333, 0.166666666666667, 0.25, 0.333333333333333,
0.416666666666667, 0.5, 0.583333333333333, 0.666666666666667,
0.75, 0.833333333333333, 0.916666666666667, 0, 0.0833333333333333,
0.166666666666667, 0.25, 0.333333333333333, 0.416666666666667,
0.5, 0.583333333333333, 0.666666666666667, 0.75, 0.833333333333333,
0.916666666666667, 0, 0.0833333333333333, 0.166666666666667,
0.25, 0.333333333333333, 0.416666666666667
)
)
解决方案
这是一个解决方案,可以解决,但仍然有效。
library(plotly)
p1 <- data %>% plot_ly(x = ~time,
y = ~y,
type = 'scatter',
mode = 'lines',
color = ~year,
marker = list(size = 1),
transforms = list(
list(
type = 'groupby',
groups = ~year,
styles = list(
list(target = 2005, value = list(marker =list(color = "#440154FF"))),
list(target = 2006, value = list(marker =list(color = "#3B528BFF"))),
list(target = 2007, value = list(marker =list(color = "#1FA187FF"))),
list(target = 2008, value = list(marker =list(color = "#9FDA3AFF")))
)
)
))
p1
虽然你说你想避免ggplotly
,但我觉得它更直接
library(ggplot2)
p2 <- data %>%
ggplot(aes(time, y, group=year)) +
geom_line(aes(color = year)) +
scale_color_viridis() +
theme_classic()
ggplotly(p2)
推荐阅读
- git - 使用 Jenkins 构建多个分支
- javascript - 什么是和谐,什么是和谐出口?
- c++ - 显示模态窗口 1 秒
- perl - Docker [for mac] 文件系统变为只读,这破坏了 docker 的几乎所有功能
- php - 通过 Ajax 包含 php 文件并显示在页面上
- python - 使用套接字不断运行 server.accept() 是否安全?
- spring - 具有内存列表的 Spring 服务
- javascript - 移相器黑屏
- windows - 使用数据版本控制解析 mingw 中的路径失败
- c# - 时间:2019-01-10 标签:c#添加contextmenu到datagridview