r - 在 R 中用不同颜色绘制向量中的每 10 个数据点

标签 r plot ggplot2 plotly

我在 R 中有一个一维向量,我想将其绘制为: this 每 10 个数据点有不同的颜色。我如何在 R 中使用普通 plot 函数、ggplotplotly 执行此操作?

最佳答案

在基础 R 中你可以试试这个。

与其他答案相比,我稍微更改了数据

# The data
set.seed(2017);
df <- data.frame(x = 1:100, y = 0.001 * 1:100 + runif(100));
nCol <- 10;
df$col <- rep(1:10, each = 10);

# base R plot
plot(df[1:2]) #add `type="n"` to remove the points
sapply(1:nrow(df), function(x) lines(df[x+0:1,1:2], col=df$col[x], lwd=2))

enter image description here

对于行,col 参数将被回收,您必须在行和绘图段上使用循环(此处为 sapply)。

关于r - 在 R 中用不同颜色绘制向量中的每 10 个数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50720968/

相关文章:

r - 在 data.table 中使用列名范围,就像在 dplyrs select 中一样

r - 如何在ubuntu上安装R的i386版本?

python - 将文本文件解析为字典列表

r - ggplot在geom tile的每个tile中添加文本

r - 如何在 ggplot2 中根据聚合数据创建 fiddle 图?

r - facet_grid ggplot2 中每个方面的scale_x_date

python - 将绘图保存到图像文件而不是显示它

Python:Matplotlib:matplotlib 中的垂直对齐图

r - 更改 R 中 ggplot geom_polygon 的颜色方案

r - 添加颜色美学后整个 map 变成红色 ggplot + ggmap