简体   繁体   中英

Plotting time and date in different axis in R

I have data looks like

head(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                  

The complete data can be download in this link format data is csv with size around 1.6 MB. The data has 5 important columns (time, type, value1, value2, value3). type contain : activity means user activity (none, high, low), sms, wifi, etc.

I want to plot my data look like this:在此处输入图片说明

X axis is time with interval one hour and Y axis is date with interval one day, and then for each type have different colour like in that figure.

When many value in same time so the plot look more thick, and also for the activity types I want different colour for (none, high, and low).

Unsure how you are going to use the data to plot, here I assume you are plotting type against time value across different days. See if this is what you are looking for:

# 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"))

在此处输入图片说明

A first indication I may give you is to prepare variables with the right intervals and use ggplot2 (or also lattice).

With ggplot2 you have to write something like:

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

As I understand, what you want to plot on the y-axis for each date, is the presence or absence of that modality in that specific hour and you want to plot it on different heights so that points do not coincide. To do that, you could prepare data, giving for example 1 if wifi is present, 2 if call is present etc... This could be a solution. Wait for other possible answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM