我相当肯定我已经在某处找到了解决方案,但由于我无法找到它,这是我的问题.
我有一些由多个变量识别的时间序列数据,我希望能够使用多个变量来绘制和区分颜色ggplot2.
样本数据:
date <- c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC",
"2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC",
"2016-06-01 UTC", "2016-04-01 UTC")
temp <- c(80.24018, 85.88911, 104.23125, 85.13571, 91.21129, 104.88333, 97.81116,
107.40484, 121.03958, 87.91830)
id <- c("A","A","A","A","A","B","B","B","B","B")
location <- c("N","S","S","N","N","S","N","S","N","S")
df <- data.frame(date,temp,id,location)
Run Code Online (Sandbox Code Playgroud)
我试图绘图
library(ggplot2)
ggplot(df) +
geom_line(aes(x=date,y=temp,colour=factor(location), group=interaction(location,id)))
Run Code Online (Sandbox Code Playgroud)
使用此代码只能按位置着色.我希望线条按位置和id着色.
我想让图表上的每个点与线条颜色不同.这是样本数据.
df <- structure(list(yrmonth = structure(c(17167, 17167, 17167, 17198,
17198, 17198, 17226, 17226, 17226, 17257, 17257, 17257), class = "Date"),
index = structure(c(2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L), .Label = c("E-W", "N-S", "OS"), class = "factor"),
N = c(2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1), data = c(129,
141, 27, 150.5, 209, 87, 247.5, 243, 188, 223, 226.5, 170
)), .Names = c("yrmonth", "index", "N", "data"), row.names = 31:42, …Run Code Online (Sandbox Code Playgroud) 如果我有一个日期和小时的矢量,如...
c("2016-03-15 13","2016-03-16 23","2016-03-17 06","2016-03-18 15","2016-03-19 08","2016-03-20 21")
Run Code Online (Sandbox Code Playgroud)
我可以找到每个时间戳之间经过的小时数吗?我调查difftime但它需要2个向量.
我有一些年月形式的数据,我想将其格式化以在ggplot.
date <- c("2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08",
"2016-09", "2016-10", "2016-11", "2016-12")
Run Code Online (Sandbox Code Playgroud)
我正在使用parsedate::parse_date,但自从更新 R 后它不再起作用。
我看过
as.yearmon工作正常,但它没有格式化为我需要的 POSIXct ggplot。其他格式(例如as.POSIXct和 )strptime会给出 NA 或错误。
注意:我不介意是否将每月的第一天添加到“年-月”格式中。
如何删除每个新变量的第一行?例如,这里有一些数据:
m <- c("a","a","a","a","a","b","b","b","b","b")
n <- c('x','y','x','y','x','y',"x","y",'x',"y")
o <- c(1:10)
z <- data.frame(m,n,o)
Run Code Online (Sandbox Code Playgroud)
我想删除列m中a和b的第一个条目.我有一个非常大的数据框,所以我想根据从a到b的变化等来做到这一点.
这就是我想要的数据框架.
m n o
1 a y 2
2 a x 3
3 a y 4
4 a x 5
5 b x 7
6 b y 8
7 b x 9
8 b y 10
Run Code Online (Sandbox Code Playgroud)
谢谢.