I would like to create a line plot - on x-axis i need times and on y-axis corresponding values.
我想创建一个线图 - 在x轴上我需要时间和y轴对应的值。
Here is my head of table:
这是我的表头:
3:10:45 PM 22.65916 22.85276 22.45874
3:11:17 PM 22.67199 22.84572 22.4558
3:11:49 PM 22.65227 22.8443 22.44791
3:12:21 PM 22.64929 22.84993 22.4558
3:12:53 PM 22.65622 22.84572 22.45284
3:13:25 PM 22.65027 22.84572 22.45385
3:13:58 PM 22.64139 22.85699 22.46366
3:14:30 PM 22.66408 22.8443 22.45678
3:15:02 PM 22.64734 22.85558 22.46366
3:15:34 PM 22.6552 22.86403 22.46073
My part of code is:
我的部分代码是:
temp=read.table("data.txt", header = FALSE, sep="\t")
plot(temp$V2, type="o", col="red")
I can add lines for another column, but how to put time to x-axis?
我可以为另一列添加行,但是如何将时间放到x轴上?
Result should look like this:
结果应如下所示:
1 个解决方案
#1
1
Try
尝试
library(ggplot2)
library(scales)
library(reshape2)
p <- ggplot(mlt, aes(x=V1, y=value, col=variable))+
geom_line()
p+
scale_x_datetime(labels=date_format('%I:%M:%S %p')) +
theme_bw() +
theme(axis.text.x=element_text(angle=45, hjust=1)) +
xlab(NULL) +
ylab(NULL)
You can also specify the limits
and breaks
in scale_x_datetime
您还可以在scale_x_datetime中指定限制和中断
lo <- with(mlt,as.POSIXct(strftime(min(V1),"%Y-%m-%d")))
hi <- with(mlt,as.POSIXct(strftime(as.Date(max(V1))+1,"%Y-%m-%d"))-1)
limits <- c(lo,hi)
p +
scale_x_datetime(breaks='1 hour',labels=date_format('%I:%M:%S %p'),
limits=limits) +
theme_bw() +
theme(axis.text.x=element_text(angle=45, hjust=1)) +
xlab(NULL) +
ylab(NULL)
data
df <- structure(list(V1 = c("3:10:45 PM", "3:11:17 PM", "3:11:49 PM",
"3:12:21 PM", "3:12:53 PM", "3:13:25 PM", "3:13:58 PM", "3:14:30 PM",
"3:15:02 PM", "3:15:34 PM"), V2 = c(22.65916, 22.67199, 22.65227,
22.64929, 22.65622, 22.65027, 22.64139, 22.66408, 22.64734, 22.6552
), V3 = c(22.85276, 22.84572, 22.8443, 22.84993, 22.84572, 22.84572,
22.85699, 22.8443, 22.85558, 22.86403), V4 = c(22.45874, 22.4558,
22.44791, 22.4558, 22.45284, 22.45385, 22.46366, 22.45678, 22.46366,
22.46073)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names =
c(NA, -10L))
mlt <- melt(df, id.var='V1')
mlt$V1 <- as.POSIXct(mlt$V1, format='%I:%M:%S %p')
#1
1
Try
尝试
library(ggplot2)
library(scales)
library(reshape2)
p <- ggplot(mlt, aes(x=V1, y=value, col=variable))+
geom_line()
p+
scale_x_datetime(labels=date_format('%I:%M:%S %p')) +
theme_bw() +
theme(axis.text.x=element_text(angle=45, hjust=1)) +
xlab(NULL) +
ylab(NULL)
You can also specify the limits
and breaks
in scale_x_datetime
您还可以在scale_x_datetime中指定限制和中断
lo <- with(mlt,as.POSIXct(strftime(min(V1),"%Y-%m-%d")))
hi <- with(mlt,as.POSIXct(strftime(as.Date(max(V1))+1,"%Y-%m-%d"))-1)
limits <- c(lo,hi)
p +
scale_x_datetime(breaks='1 hour',labels=date_format('%I:%M:%S %p'),
limits=limits) +
theme_bw() +
theme(axis.text.x=element_text(angle=45, hjust=1)) +
xlab(NULL) +
ylab(NULL)
data
df <- structure(list(V1 = c("3:10:45 PM", "3:11:17 PM", "3:11:49 PM",
"3:12:21 PM", "3:12:53 PM", "3:13:25 PM", "3:13:58 PM", "3:14:30 PM",
"3:15:02 PM", "3:15:34 PM"), V2 = c(22.65916, 22.67199, 22.65227,
22.64929, 22.65622, 22.65027, 22.64139, 22.66408, 22.64734, 22.6552
), V3 = c(22.85276, 22.84572, 22.8443, 22.84993, 22.84572, 22.84572,
22.85699, 22.8443, 22.85558, 22.86403), V4 = c(22.45874, 22.4558,
22.44791, 22.4558, 22.45284, 22.45385, 22.46366, 22.45678, 22.46366,
22.46073)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names =
c(NA, -10L))
mlt <- melt(df, id.var='V1')
mlt$V1 <- as.POSIXct(mlt$V1, format='%I:%M:%S %p')