换行符当GGPLOT2没有数据(Line break when no data in ggplot2

2019-07-19 17:22发布

我使用R键绘制的一些数据。

Date <- c("07/12/2012 05:00:00", "07/12/2012 06:00:00", "07/12/2012 07:00:00",
      "07/12/2012 08:00:00","07/12/2012 10:00:00","07/12/2012 11:00:00")
Date <- strptime(Date, "%d/%m/%Y %H:%M")
Counts <- c("0","3","10","6","5","4")
Counts <- as.numeric(Counts)
df1 <- data.frame(Date,Counts,stringsAsFactors = FALSE)
library(ggplot2)
g = ggplot(df1, aes(x=Date, y=Counts)) + geom_line(aes(group = 1))
g

我怎么问R值不要绘制数据作为连续的线时,有时间休息? 我通常有每个小时一个数据点,但有时(上午8点至10:00)休息。 这些点之间,我不想线进行连接。 这是可能的R中?

编辑

非常感谢这里的响应。 我现在的数据是10秒的间隔,我希望这样做使用此数据在同一块分析。

df <- structure(list(Date = c("11/12/2012", "11/12/2012", "11/12/2012", 
                     "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
                     "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012"),
                     Time = c("20:16:00", "20:16:10", "20:16:20", "20:16:30", 
                     "20:16:40", "20:16:50", "20:43:30", "20:43:40", 
                     "20:43:50", "20:44:00", "20:44:10"),
                     Axis1 = c(181L, 14L, 65L, 79L, 137L, 104L, 7L, 0L, 0L, 
                     14L, 0L),
                     Steps = c(13L, 1L, 6L, 3L, 8L, 4L, 1L, 0L, 0L, 0L, 0L)),
                .Names = c("Date", "Time", "Axis1", "Steps"),
                row.names = c(57337L, 57338L, 57339L, 57340L, 57341L, 57342L, 
                57502L, 57503L, 57504L, 57505L, 57506L), class = "data.frame")

我想我明白的代码试图做的,当它增加了列“组”原来的数据帧,但我的问题围绕着我如何获得R键现在知道的数据是10秒的间隔? 当我申请的代码的第一行,以确定这些数字是否是连续的,或者是否有一个间隙(例如IDX < - C(1,DIFF(DF $时间)),I得到以下错误:

错误中的R [I1] - R的[-length(R):-(长度(R) - 滞后+ 1L)〕:非数字参数二进制运算符

我的“时间”变量之后,我需要添加“as.POSIXct”,以确保正确识别的时间?

Answer 1:

你必须设置group通过设置一个共同的价值,那些你想连接点。 在这里,你可以设置前4个值说1后2至2 。 并让他们为的因素。 那是,

df1$grp <- factor(rep(1:2, c(4,2)))
g <- ggplot(df1, aes(x=Date, y=Counts)) + geom_line(aes(group = grp)) + 
                     geom_point()

编辑:一旦你的data.frame加载,您可以使用此代码自动生成grp列:

idx <- c(1, diff(df$Date))
i2 <- c(1,which(idx != 1), nrow(df)+1)
df1$grp <- rep(1:length(diff(i2)), diff(i2))

注意:补充是很重要的 geom_point()以及因为如果discontinuous range恰好是在data.frame中的最后一项,它不会被绘制(因为有没有2点连接线)。 在这种情况下, geom_point()将绘制。

举个例子,我会产生更多的空白数据:

# get a test data
set.seed(1234)
df <- data.frame(Date=seq(as.POSIXct("05:00", format="%H:%M"), 
                as.POSIXct("23:00", format="%H:%M"), by="hours"))
df$Counts <- sample(19)
df <- df[-c(4,7,17,18),]

# generate the groups automatically and plot
idx <- c(1, diff(df$Date))
i2 <- c(1,which(idx != 1), nrow(df)+1)
df$grp <- rep(1:length(diff(i2)), diff(i2))
g <- ggplot(df, aes(x=Date, y=Counts)) + geom_line(aes(group = grp)) + 
            geom_point()
g

编辑:对于你的新数据(假设它是df ),

df$t <- strptime(paste(df$Date, df$Time), format="%d/%m/%Y %H:%M:%S")

idx <- c(10, diff(df$t))
i2 <- c(1,which(idx != 10), nrow(df)+1)
df$grp <- rep(1:length(diff(i2)), diff(i2))

现在绘制aes(x=t, ...)



Answer 2:

我认为这是没有办法为R或ggplot2知道是否存在丢失的数据点的地方,除了你有指定它NA 。 通过这种方式,例如:

df1 <- rbind(df1, list(strptime("07/12/2012 09:00:00", "%d/%m/%Y %H:%M"), NA))
ggplot(df1, aes(x=Date, y=Counts)) + geom_line(aes(group = 1))



Answer 3:

朱巴的回答 ,包括明确的NA的,你想休息,是最好的办法。 这里是引入这些替代方式NA的在正确的位置(而不必手动弄明白)。

every.hour <- data.frame(Date=seq(min(Date), max(Date), by="1 hour"))
df2 <- merge(df1, every.hour, all=TRUE)
g %+% df2

你可以做你以后类似df例如,更改日期和时间为正确的格式后,

df$DateTime <- as.POSIXct(strptime(paste(df$Date, df$Time), 
                                   format="%m/%d/%Y %H:%M:%S"))
every.ten.seconds <- data.frame(DateTime=seq(min(df$DateTime), 
                                             max(df$DateTime), by="10 sec"))
df.10 <- merge(df, every.ten.seconds, all=TRUE)


文章来源: Line break when no data in ggplot2
标签: r ggplot2