【问题标题】:Plotting time and date in different axis in R在R中的不同轴上绘制时间和日期
【发布时间】:2014-12-06 08:07:58
【问题描述】:

我的数据看起来像

头部(data1,10)

       id           time     type      value1            value2 value3
1    1612 7/1/2014 10:15 activity        none                         
2   76308 7/1/2014 10:17  battery discharging                         
3    1613 7/1/2014 10:17 activity        none                         
4    1614 7/1/2014 10:17 activity        none                         
5    1615 7/1/2014 10:17 activity        none                         
6    1616 7/1/2014 10:17 activity        none                         
7    1617 7/1/2014 10:17 activity        none                         
8  325200 7/1/2014 10:17     wifi     linksys 00:1a:70:5b:8f:21    -86
9    1618 7/1/2014 10:19 activity        none                         
10   1619 7/1/2014 10:19 activity        none                  

可以下载this link格式的完整数据数据是csv,大小约为1.6 MB。 数据有 5 个重要列(时间、类型、值 1、值 2、值 3)。 type contains:activity表示用户活动(无、高、低)、短信、wifi等。

我想将我的数据绘制成如下所示:

X 轴是间隔一小时的时间,Y 轴是间隔一天的日期,然后每种类型都有不同的颜色,如图所示。

当多个值同时出现时,情节看起来更厚,而且对于我想要不同颜色的活动类型(无、高和低)。

【问题讨论】:

  • 我可能给你的第一个建议是准备具有正确间隔的变量并使用ggplot2(或lattice)。使用 ggplot2 你必须写类似ggplot(aes(x = time_1h, y = value, color = type, shape = type), data = yourdata) + geom_point() + facet_wrap(~ date_1day, ncol = 1)
  • 猜猜你只需要列timetype 就可以了。对吗?
  • @KFB 当多个值同时出现时,情节看起来更厚,而且对于活动类型,我想要不同的颜色(无、高和低)。
  • 您好,您没有澄清问题。是否只使用列timetype 进行绘图?
  • @KFB 我也使用列 value1 因为列 value1 中的活动值

标签: r plot ggplot2


【解决方案1】:

不确定您将如何使用数据进行绘图,在这里我假设您正在绘制不同日期的类型与时间值。看看这是不是你要找的:

# transforming your data
library(data.table); library(tidyr); library(ggplot2)
test = fread("data_test.csv")  # the data file is in working directory
test = separate(test, time, c("days","time"), sep=" ")
test$days = as.POSIXct(strptime(test$days, "%m/%d/%Y"))
test$time = as.POSIXct(strptime(test$time, "%H:%M"))

# to plot
ggplot(test, aes(x=time, y=type, colour=type, shape=type)) +
  theme_bw() + 
  geom_point() + 
  facet_grid(days ~.) + 
  scale_x_datetime(breaks=date_breaks("1 hour"), labels = date_format("%H:%M"))

【讨论】:

  • 嗨@KFB 谢谢你的回答,我找不到separate 函数,你用什么样的库来实现separate 函数?
  • 然后当我使用这个代码test$days = as.POSIXct(strptime(test$days, "%m/%d/%Y"))我得到这个错误Error in [(x, j = name, value = value) : RHS of assignment to new column 'days' is zero length but not empty list(). For new columns the RHS must either be empty list() to create an empty list column, or, have length > 0; e.g. NA_integer_, 0L, etc.
  • @user46543,加载所有library(data.table); library(tidyr); library(ggplot2)
【解决方案2】:

我可能给你的第一个建议是准备具有正确间隔的变量并使用 ggplot2(或格子)。

使用 ggplot2 你必须编写如下内容:

ggplot(aes(x = time_1h, y = value, color = type, shape = type), data = yourdata) +
 geom_point() + facet_wrap(~ date_1day, ncol = 1)

据我了解,您希望在 y 轴上为每个日期绘制的内容是该特定时间是否存在该模态,并且您希望将其绘制在不同的高度上,以便点不重合。为此,您可以准备数据,例如,如果存在 wifi,则给出 1,如果存在呼叫,则给出 2 等等......这可能是一个解决方案。等待其他可能的答案。

【讨论】:

  • 如何获取 time_1h 和 date_1day 值?你能告诉我吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2018-10-08
相关资源
最近更新 更多