用ggplot在x轴上显示有限的时间范围 [英] Show limited time range on x-axis with ggplot

查看:193
本文介绍了用ggplot在x轴上显示有限的时间范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望下图中的x轴从06:00开始到22:00结束,每4小时休息一次。但是,我无法弄清以下情况。



a)如何在06:00之前在06:00之前开始x轴,而06:00之前没有空的空间。 / p>

b)如何在22:00之后使x轴结束,而在22:00之后没有任何空白空间。现在它甚至不显示22:00



c)如何每4小时休息一次。



< d)如何为y轴指定一个标签(目前它只是X4,列名)。



我尝试了几件事,但没有成功。一些示例数据:

 范围< -  seq(as.POSIXct(2015/4/18 06:00), as.POSIXct(2015/4/18 22:00),mins)

df< - data.frame(matrix(nrow = length(range),ncol = 4))
df [,1] < - c(1:长度(范围))
df [,2] < - 2 * c(1:长度(范围))
df [ ,3] < - 3 * c(1:长度(范围))
df [,4] < - 范围

重塑:

  library(reshape2)
df2 < - melt(df ,



图表:

  library(ggplot2)
ggplot(data = df2,aes(x = X4,y = value,color = variable))+ geom_line()+
scale_y_continuous(expand = c(0,0))+
coord_cartesian(xlim = c(as.POSIXct(2015/4/18 06:00:00),as.POSIXct(2015/4/18 22:00:00)))

这使图表看起来像这样:



有什么想法?

解决方案

这里有一些代码可以帮助你。这可以通过使用 scale_x_datetime

  ##轻松完成。终点
st < - as.POSIXct(2015/4/18 06:00:00)
nd< - as.POSIXct(2015/4/18 22:00:00 )

##显示给定时间范围的数据
ggplot(data = df2,aes(x = X4,y = value,color = variable))+
geom_line )+
scale_y_continuous(Some name,expand = c(0,0))+
scale_x_datetime(Some name,expand = c(0,0),limits = c(st,nd ),
breaks = seq(st,nd,4 hours),
labels = strftime(seq(st,nd,4 hours),%H:%S))


I want the x-axis in the following graph to start at 06:00 and end at 22:00, with breaks at every 4 hours. I can't figure out the following, however.

a) How to make the x-axis start at 06:00 without any empty space before 06:00.

b) How to make the x-axis end at 22:00 without any empty space after 22:00. Right now it doesn't even show 22:00

c) How to have breaks at every 4 hours.

d) How to assign a label to the y-axis (currently it's simply X4, the column name).

I've tried several things, but without success. Some example data:

range <- seq(as.POSIXct("2015/4/18 06:00"),as.POSIXct("2015/4/18 22:00"),"mins")

df <- data.frame(matrix(nrow=length(range),ncol=4))
df[,1] <- c(1:length(range))
df[,2] <- 2*c(1:length(range))
df[,3] <- 3*c(1:length(range))
df[,4] <- range

Reshape:

library(reshape2)
df2 <- melt(df,id="X4")

Graph:

library(ggplot2)
ggplot(data=df2,aes(x=X4,y=value,color=variable)) + geom_line()+
  scale_y_continuous(expand=c(0,0)) +
  coord_cartesian(xlim=c(as.POSIXct("2015/4/18 06:00:00"),as.POSIXct("2015/4/18 22:00:00")))

Which makes the graph look like this:

Any ideas?

解决方案

Here is some code that should help you. This can easily be done using scale_x_datetime.

## desired start and end points
st <- as.POSIXct("2015/4/18 06:00:00")
nd <- as.POSIXct("2015/4/18 22:00:00")

## display data for given time range
ggplot(data = df2, aes(x = X4, y = value, color = variable)) + 
  geom_line() +
  scale_y_continuous("Some name", expand = c(0, 0)) +
  scale_x_datetime("Some name", expand = c(0, 0), limits = c(st, nd), 
                   breaks = seq(st, nd, "4 hours"), 
                   labels = strftime(seq(st, nd, "4 hours"), "%H:%S"))

这篇关于用ggplot在x轴上显示有限的时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆