首页 > 解决方案 > 一个 y 轴上的 2 个图 - 错误:提供给连续刻度的离散值

问题描述

我不明白为什么会出现这个错误。正常原因是数据被读取为字符串。我错过了什么?

我有两个数据框,我可以单独绘制它们,但我想在同一张图上绘制它们。第一个绘制深度与 date_time(一个 5 秒间隔的数据集)的关系。第二个根据日期绘制变量“混合层深度”,即每日间隔数据集。

gg <- ggplot()
gg <- gg + geom_line(data = Mn, aes(x = Date_time, y = 1-Depth, color = Temperature), size = 0)
gg # this works on it's own
gg <- gg + geom_line(data = MLDepth, aes(x = Date, y = 1-ML_Depth, color = "red"))
gg # this works on it's own. but when together with above, causes error.
gg <- gg + theme(legend.title = element_blank(), legend.position = 'none')
gg <- gg + xlab("Date") + ylab("Depth")
gg

当我尝试将它们都绘制在同一个图上时,我得到“错误:提供给连续比例的离散值”

Mn$Date_time 和 MLDepth$Date 都是 posixct 格式,例如“2013-10-14 12:30:00”。Depth 和 ML_Depth 都是数字。没有NA。

> str(MLDepth)
'data.frame':   88 obs. of  2 variables:
 $ Date    : POSIXct, format: "2013-10-14 08:00:00" "2013-10-15 08:00:00" "2013-10-16 08:00:00" "2013-10-17 08:00:00" ...
 $ ML_Depth: num  14.1 15.2 16.3 20.9 16.6 ...
> str(Mn)
'data.frame':   1510819 obs. of  5 variables:
 $ Date_time  : POSIXct, format: "2013-10-14 12:30:00" "2013-10-14 12:30:05" "2013-10-14 12:30:10" "2013-10-14 12:30:15" ...
 $ Depth      : num  64.4 65.9 65.9 66.4 67.9 ...
 $ Temperature: num  27.5 27.5 27.4 27.4 27.2 ...
 $ Light_Level: int  148 148 148 148 147 147 146 146 146 146 ...
 $ Date       : Date, format: "2013-10-14" "2013-10-14" "2013-10-14" "2013-10-14" ...

标签: rggplot2plot

解决方案


根据 Stefan 的评论:问题是我将数字温度映射到颜色上,而在第二个 geom_line 中,您将字符“红色”映射到颜色上。在 aes() 之外添加 color="red" 作为参数可以解决问题


推荐阅读