【问题标题】:How to stop all time labels showing in ggplot2?如何停止 ggplot2 中显示的所有时间标签?
【发布时间】:2019-05-30 21:27:32
【问题描述】:

我在尝试让我的 ggplot 上的 x 轴仅显示特定值时遇到问题。 X 轴是一个时间范围,但目前有很多时间无法读取(见图),理想情况下我希望每小时左右打一个勾,但我就是不知道怎么做。

任何建议都将不胜感激。

我已经尝试过针对类似问题的建议,但没有任何运气(请参阅下面我尝试过的大部分代码和错误)

这是我的图形代码;

ggplot(data, aes(x=time, y=binary)) +
geom_bar(stat='identity') +
facet_wrap(~id,scales="free_x",  ncol=1,strip.position = "right")

这是我尝试解决问题的大部分内容;

scale_x_continuous(breaks=c(seq(16:00,17:00,18:00)))
>Error in seq.default(16:0, 17:0, 18:0,  : 
      'from' must be of length 1
In addition: Warning message:
In seq.default(16:0, 17:0, 18:0,) :
extra arguments  will be disregarded
scale_x_continuous(breaks=c(seq("16:00","17:00","18:00")))
> Error in seq.default("16:00", "17:00", "18:00") : 
      'from' must be a finite number
    In addition: Warning message:
    In seq.default("16:00", "17:00", "18:00") : NAs

强制引入

data$time <- gsub('\"', "", as.character(data$time), fixed=TRUE)
data$time <- as.Date(data$time, "%H-%M-%S")
>    Warning messages:
    1: In min(x) : no non-missing arguments to min; returning Inf
    2: In max(x) : no non-missing arguments to max; returning -Inf
    3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
    4: Removed 7290 rows containing missing values (position_stack).
x=data$time
as.numeric(gsub(",","",x,fixed=TRUE))
>    Warning message:
    NAs introduced by coercion
scale_x_datetime(breaks = date_breaks("1 hour"), labels = date_format("%H:%M:%S"))

<ScaleContinuousDatetime>
 Range:  
 Limits:    0 --    1

这里是原始数据集的一个样本(因为它很大),很抱歉最初没有发布这个。 非常感谢您的帮助!

> data
   time  id binary
1 15:49 267      2
2 13:58 269      0
3 15:51 231      0
4 16:00 263      1
5 15:51 237      2
6 15:53 236      2
7 16:00 235      2

> dput(data)

structure(list(time = structure(c(2L, 1L, 3L, 5L, 3L, 4L, 5L), .Label = c("13:58", 
"15:49", "15:51", "15:53", "16:00"), class = "factor"), id = c(267L, 
269L, 231L, 263L, 237L, 236L, 235L), binary = c(2L, 0L, 0L, 1L, 
2L, 2L, 2L)), .Names = c("time", "id", "binary"), class = "data.frame", row.names = c(NA, 
-7L))

> str(data)

'data.frame':   7 obs. of  3 variables:
 $ time  : Factor w/ 5 levels "13:58","15:49",..: 2 1 3 5 3 4 5
 $ id    : int  267 269 231 263 237 236 235
 $ binary: int  2 0 0 1 2 2 2

【问题讨论】:

  • 您可以在问题中分享的最有帮助的是实际数据,通过dput(data) 或至少通过str(data) 提供数据的实际结构。 (或一小部分数据的等价物)
  • 在 joran 的评论之后,您可以找到一些关于问题需要什么的见解,以便我们可以重现您的问题并为您提供答案,可以在这里找到how-to-make-a-great-r-reproducible-example
  • 你想用breaks=c(seq(16:00,17:00,18:00))做什么?这些将被读取为格式不正确的数字。通常seq 接受 from、to 和 by 参数。

标签: r ggplot2 time


【解决方案1】:

好吧,在没有数据运行的情况下,我可以看到一些希望能有所帮助的问题:

scale_x_continuous(breaks=c(seq(16:00,17:00,18:00)))

两次都抛出同样的错误seq需要三个参数seq(initial value, final value, step),而你目前有它作为seq(time 1, time 2, time 3)

查看另一个 ggplot 调用:

scale_x_datetime(breaks = date_breaks("1 hour"), labels = date_format("%H:%M:%S"))

应该读

scale_x_datetime(date_breaks="1 hour", date_labels("%H:%M:%S"))

鉴于您正在处理时间/日期变量,后一种选项更可取。

那么关键是确保变量的格式正确。这是我们需要查看该列的示例以提供建议的地方!可能已经没问题了 - ggplot 使用我找到的日期格式执行此操作,因此这可能表明它的格式已经正确。

如果您在列中添加数据示例(在 @joran 的评论之后),那么我们可以继续。

【讨论】:

  • 感谢大家的建议,很抱歉这是我第一次在这里发帖,所以我真的忘记了实际发布数据!现在已将其编辑为原始问题。 @JMilner,非常感谢您的建议,会试一试!
猜你喜欢
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
相关资源
最近更新 更多