r - 当我尝试将两个变量绘制到同一个图上时,为什么我的图看起来像这样?
问题描述
您可以使用以下代码创建数据框:
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
解决方案
你需要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()
推荐阅读
- r - 为什么来自 forcats 的 fct_lump 无法处理我的数据?
- google-apps-script - 使用脚本在 Google 电子表格中自动增加 ID
- java - JPA - 具有嵌入 ID 的映射列表属性
- google-cloud-platform - Debian 9 上的 OpenCl 安装程序未检测到已安装的 lsb_core
- node.js - 将数据附加到 .json 文件
- python - 使用scrapy,python从DIV中的第二个孩子获取文本
- kubernetes - 从 kubelet 批准证书后,kubenetes 无法获取节点信息
- javascript - 事件处理程序被多次触发
- c - 我在创建一个函数来搜索我的链接列表中的字符串时遇到问题
- json - 将 JSON 解析为 Dart 中的列表