【问题标题】:Using a different color for a section of a line in ggplot在ggplot中为线条的一部分使用不同的颜色
【发布时间】:2020-03-25 18:33:23
【问题描述】:

我正在使用 ggplot 为我的绘图绘制各种线条,每条线条都有指定的颜色。我将估计值绘制为年份的函数(因此,这些是时间序列数据)。
但是,我绘制的两条线在前 138 年中重叠。我想做的是将这两条曲线的前 138 年(对应于前 22 个值)着色为黑色,但我没有遇到任何有用的东西来显示如何隔离这样的曲线部分。这可能吗?

这是我到目前为止所做的(效果很好):

library(ggplot2)

gg5 <- ggplot(data = xcel, aes(x = Year)) + 
          geom_line(aes(y = GLocation45), color = "blue", linetype = "twodash") + 
          geom_line(aes(y = GLocation85), color = "red", linetype = "twodash") + 
          geom_line(aes(y = Glocationco), color = "green", linetype = "twodash")

> dput(xcel[1:10,])

structure(list(Year = c(7L, 14L, 21L, 28L, 35L, 42L, 49L, 56L, 
63L, 70L), GLocation45 = c(180.06, 187.96, 181.03, 173.14, 184.26, 
183.7, 188.59, 178.21, 189.23, 184.63), GScale45 = c(56.83, 63.86, 
62.21, 51.87, 60.28, 60.74, 62.11, 58.22, 60.94, 60.73), GShape45 = c(0.004, 
-0.17, -0.16, -0.03, -0.1, -0.19, -0.17, 0.07, -0.15, -0.05), 
GLocation85 = c(180.06, 187.96, 181.03, 173.14, 184.26, 183.7, 
188.59, 178.21, 189.23, 184.63), GScale85 = c(5.68, 63.86, 
62.21, 51.87, 60.28, 60.74, 62.11, 58.22, 60.94, 60.73), 
GShape85 = c(0.004, -0.16, -0.16, -0.03, -0.1, -0.19, -0.17, 
0.07, -0.15, -0.05), Glocationco = c(174.58, 167.46, 178.54, 
183.37, 181.54, 187.89, 197.58, 194.36, 212.71, 198.84), 
GScaleco = c(59.2, 51.42, 57.82, 62.86, 61.84, 65.84, 72.01, 
64.4, 75.7, 62.69), GShapeco = c(-0.06, 0.27, 0.002, -0.02, 
-0.005, -0.11, -0.26, 3e-04, -0.29, 0.05)), row.names = c(NA, 
10L), class = "data.frame")

对象“GLocation45”和“GLocation85”是与前 138 年的相同值重叠的对象(本质上,前 22 个值对应于前 138 年,因此这些对象的前 22 个值是相同的),所以我希望曲线的那些部分是黑色的,然后让它们在 138 年后分别继续为蓝色和红色。

这可能吗?

【问题讨论】:

  • 您能否提供xceldput() 的样本?
  • 伊恩坎贝尔感谢您的回复。好的 - 我刚刚从 dput 在上面的帖子中添加了一个示例(它显示了对象“Year”和“GLocation45”)
  • 很遗憾,dput 的输出格式不正确。你能输出一些能让你的代码运行的东西吗?也许dput(xcel[1:10,])
  • @IanCampbell 好的 - 我在上面的帖子中添加了从“dput(xcel[1:10,])”收到的输出

标签: r ggplot2


【解决方案1】:

这是一个解决方案,您只需对数据进行子集化即可绘制每条线。

library(ggplot2)

#Number of rows, from 1 to n, where the lines overlap.
#According to your question this should be 138 using the complete dataset
n<-5

ggplot(data = xcel, aes(x = Year)) + 
  #Subset data for black line, i.e., shared line
  geom_line(aes(y = GLocation45), data = xcel[1:n,],
            color = "black", linetype = "twodash") + 
  #Subset for the red line. i.e., rest of the data 
  geom_line(aes(y = GLocation85), data = xcel[n:nrow(xcel),],
            color = "red", linetype = "twodash") + 
  #Draw green line normally, without subsets
  geom_line(aes(y = Glocationco),
            color = "green", linetype = "twodash")

【讨论】:

  • 啊,是的,这就是我要找的!非常感谢!我在你的情节中注意到蓝色曲线不见了,但是? GLocation45 将是蓝色,但在黑色部分之后(就像您在黑色部分之后的红色曲线一样 - 两者都将具有相同的黑色部分)。此外,为了更正一个细节,对象“GLocation45”和“GLocation85”的前 22 个值将相当于 138 年,所以实际上应该是黑色的前 22 个值。在这种情况下,在子集时,GLocation45 和 GLocation85 是否应该是“xcel[1:22,]”?谢谢,
  • 我想通了 - 我刚刚在包含“GLocation45”或“GLocation85”的第一个 geom_line 中子集 1:22 之后添加了一个新的 geom_line()。因此,这样,值 1 到 22 将在这两个对象的黑色下方(都在单独的 geom_line 中),然后创建两个新的单独 geom_line() 来执行 22:34 作为蓝色或红色,有效地连接到黑块。 :) 再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2018-08-25
  • 1970-01-01
  • 2020-03-17
  • 2013-05-06
  • 2016-09-29
  • 2018-06-11
相关资源
最近更新 更多