【问题标题】:ggplot2: coloring segments of single line with different colorsggplot2:用不同颜色为单线段着色
【发布时间】:2017-08-25 03:26:20
【问题描述】:

我想绘制两条线,然后用不同颜色对一条线的段进行着色。 这是一个简单的代码:

temp <- data.frame(x = c(2, 4, 5, 5, 7, 6, 7, 8, 10, 9),
            y = c(3, 8, 4, 8, 8, 4, 9, 12, 5, 1),
            line = c('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b'),
            color = c('c', 'c', 'd', 'd', 'd', 'c', 'c', 'c', 'd', 'd'))

ggplot() +
  geom_line(aes(x, y, linetype = line, color = ???), data = temp)

我想使用color 因子为每条线段着色。有什么帮助吗?提前谢谢!

【问题讨论】:

  • 只需使用列名color 代替???
  • 感谢您的回复。我已更新问题以使其更清楚。
  • 样本数据中的每一行包含 4 个点之间的 3 个段。有 2 种颜色,你打算如何在段之间分割颜色?
  • 好的...在编辑您的线条后,现在每条线在 5 点之间有 4 段。仍然不清楚您希望过渡段如何着色。运行ggplot(temp, aes(x, y)) + geom_line(aes(linetype = line)) + geom_point(aes(color = color)),我们可以将红点之间的线变为红色,将蓝点之间的线变为蓝色。你想要红色和蓝色点之间的线是什么颜色的??

标签: r ggplot2


【解决方案1】:

这张图能解决你的问题吗?

ggplot() + geom_line(aes(x, y, group = line, color = color), data = temp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2018-10-12
    • 2021-05-27
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多