[英]Timestamp is ruining my graph in ggplot2, How can I fix this?
I have a data set called X1234
and I have three columns (num, timestamp, Levels of concentration), here is a sample of what my data looks like:我有一个名为
X1234
的数据集,我有三列(数字、时间戳、浓度级别),以下是我的数据示例:
num | timestamp | Levels of Concentration
1 | 4/2/2019 10:45 | 60.80
2 | 4/2/2019 11:00 | 60.08
3 | 4/2/2019 11:15 | 60.08
4 | 4/2/2019 11:30 | 60.26
5 | 4/2/2019 11:45 | 60.26
6 | 4/2/2019 12:00 | 60.44
7 | 4/2/2019 12:15 | 60.44
8 | 4/2/2019 12:30 | 60.44
9 | 4/2/2019 12:45 | 60:62
10 | 4/2/2019 13:00 | 60.62
When I use ggplot to plot a graph of the area (the x-axis = num
, y-axis = Levels of Concentration
) I get a good graph, however, I want to do this with a timestamp
.当我使用ggplot绘制面积图(x 轴 =
num
,y 轴 = Levels of Concentration
)时,我得到了一个很好的图,但是,我想用timestamp
来做这个。 Here is the picture of what I would like my graph to look like and the code I used for it was :这是我希望我的图形看起来像什么的图片,我使用的代码是:
ggplot(X1234, aes(x = X1234$num, y=X1234$`Levels of Concentration`)) + geom_area()
Ideal Graph理想图
However I need to use the timestamp
as the x-axis, but when I do so my graph changes completely and I don't understand why.但是我需要使用
timestamp
作为 x 轴,但是当我这样做时,我的图表完全改变了,我不明白为什么。 Here is the code I used for the second graph (x = timestamp
, y = Levels of Concentration
)这是我用于第二个图的代码(x =
timestamp
,y = Levels of Concentration
)
X1234$timestamp_local <- as.Date (X1234$timestamp_local, '%m/%d ')
ggplot(data = X1234, aes (x= X1234$timestamp_local, y = X1234$`Levels of Concentration`)) + geom_area()
My actual graph using the timestamp我使用时间戳的实际图表
The problem is that num
is a number which is plotted an a continuous scale, while timestamp
is of type character which results in a discrete scale.问题是
num
是一个以连续比例绘制的数字,而timestamp
是导致离散比例的字符类型。
For plotting timestamp
on a continuous scale it needs to be coerced to the datetime class POSIXct
properly, eg, by using lubridate::mdy_hm()
:为了在连续范围内绘制
timestamp
,需要正确地将其强制转换为日期时间类POSIXct
,例如,通过使用lubridate::mdy_hm()
:
library(ggplot2)
library(lubridate)
ggplot(X1234) +
aes(x = mdy_hm(timestamp), y = `Levels of Concentration`) +
geom_area()
X1234 <- readr::read_delim("
num | timestamp | Levels of Concentration
1 | 4/2/2019 10:45 | 60.80
2 | 4/2/2019 11:00 | 60.08
3 | 4/2/2019 11:15 | 60.08
4 | 4/2/2019 11:30 | 60.26
5 | 4/2/2019 11:45 | 60.26
6 | 4/2/2019 12:00 | 60.44
7 | 4/2/2019 12:15 | 60.44
8 | 4/2/2019 12:30 | 60.44
9 | 4/2/2019 12:45 | 60.62
10 | 4/2/2019 13:00 | 60.62", delim = "|", trim_ws = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.