首页 > 解决方案 > 当我尝试将两个变量绘制到同一个图上时,为什么我的图看起来像这样?

问题描述

使用 ggplot2 的结果

您可以使用以下代码创建数据框:

structure(list(year = c(1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969), DAI = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), Severity = c(0, 0, 0, 0, 3, 20, 50, 80, 90, 100, 100, 0, 0, 0, 0, 0, 0, 0, 3, 6, 30, 70)), class = "data.frame", row.names = c(NA, -22L))

我想在一个区域创建两个图,然后我将使用 pracma 包估计每条曲线下的面积。我用来创建情节的代码是:

  p2 <- ggplot(df4, aes(x=DAI, y=Severity, color=year)) + geom_line()
  p2

标签: rggplot2

解决方案


你需要year输入factor

library(ggplot2)

df <- structure(list(year = c(1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1968, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969, 1969), DAI = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), Severity = c(0, 0, 0, 0, 3, 20, 50, 80, 90, 100, 100, 0, 0, 0, 0, 0, 0, 0, 3, 6, 30, 70)), class = "data.frame", row.names = c(NA, -22L))

ggplot(df,aes(x=DAI,y=Severity,color = factor(year)))+geom_line()

推荐阅读