如何让geom_step在y轴上与离散因子很好地匹配?

时间:2017-05-30 19:31:23

标签: r ggplot2

我想使用ggplot2生成一个步骤图。我有沿x轴的日期时间,以及y轴上的四个离散的非数字因子级别。我想在适当的级别之间插入一个步骤,但是现在我在ggplot2中生成的代码不起作用。任何建议都非常感谢!

到目前为止,我有以下代码:

ggplot(s2, aes(x = datetime, y = activity)) + geom_step(colour = "blue")

产生:

augh!

但是,我喜欢的是这样的东西(我切换到geom_point,它工作正常,并手动添加了步骤线):

what i'd like

这是我的数据的一个子集,作为带有日期时间(POSIXct)和活动(因子)的data.frame:

s2 <- structure(list(`datetime` = structure(c(1496102400L, 
1496109600L, 1496116800L, 1496124000L, 1496131200L, 1496138400L, 
1496145600L, 1496152800L, 1496160000L, 1496167200L, 1496174400L, 
1496181600L, 1496188800L, 1496196000L, 1496203200L, 1496210400L, 
1496217600L, 1496224800L, 1496232000L, 1496239200L, 1496246400L, 
1496253600L, 1496260800L, 1496268000L, 1496275200L, 1496282400L, 
1496289600L, 1496296800L, 1496304000L, 1496311200L, 1496318400L, 
1496325600L, 1496332800L, 1496340000L, 1496347200L, 1496354400L, 
1496361600L, 1496368800L, 1496376000L, 1496383200L, 1496390400L, 
1496397600L, 1496404800L, 1496412000L, 1496419200L, 1496426400L, 
1496433600L, 1496440800L, 1496448000L, 1496455200L), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), activity = structure(c(1L, 2L, 3L, 
4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("HO", 
"TR", "FO", "SE"), class = "factor")), .Names = c("datetime", 
"activity"), row.names = c(NA, -50L), class = "data.frame")

0 个答案:

没有答案