在R中绘制日期图表

3

我已经编写了以下绘图命令:

Stripped_DATA <- structure(list(Epoch = structure(c(1110925802, 1110929408, 1110933014, 
                                                    1110936616, 1110940217), 
                                                  class = c("POSIXct", "POSIXt"), 
                                                  tzone = "Europe/Helsinki"), 
                                Timediff = c(-1.4653909261659, -1.46512243581845, 
                                             -1.46505141447328, -1.46503418192427,  -1.46464648029912)), 
                           .Names = c("Epoch", "Timediff"), 
                           row.names = c("11070", "21070", "31070", "41070", "51070"), 
                           class = "data.frame")

plot(Stripped_DATA, main = "Maser-69 hourly averages raw data, -3 < 3 microseconds", xlab = "Date", ylab = "microseconds")
ticks <- seq(as.POSIXct("2005-03-16 00:30:02", format = "%F %T"),
             as.POSIXct("2019-04-19 14:29:55", format = "%F %T"), by = "4 months")
labels <- seq(as.Date("2005-03-16"), as.Date("2019-04-19"), by = "4 months")
axis.POSIXct(1, at = ticks, format = "%Y-%m-%d %H", labels = labels)

这将在图表中显示x轴。如何消除与日期重叠的三个年度刻度?此外,是否有一种方法可以将日期标签从水平变为垂直,以便我可以添加更多的日期标签?

enter image description here


1
你能分享一下你正在使用的数据吗?例如通过粘贴 dput(head(Stripped_DATA, 20)) 的结果来展示。 - AkselA
你可以在绘图中添加 las=23 来旋转标签。 - mischva11
1
dput(head(stripped_DATA, 5)) 结果:结构(list(Epoch = structure(c(1110925802, 1110929408, 1110933014, 1110936616, 1110940217), class = c("POSIXct", "POSIXt"), tzone = "Europe/Helsinki"), Timediff = c(-1.4653909261659, -1.46512243581845, -1.46505141447328, -1.46503418192427, -1.46464648029912)), .Names = c("Epoch", "Timediff"), row.names = c("11070", "21070", "31070", "41070", "51070"), class = "data.frame") - Ismo Mehtälä
对你来说,使用ggplot2包会很有趣吗? - bbiasi
2
最好的做法是通过在问题中添加dput()的输出而不是在评论中添加来编辑您的问题。 - shea
2个回答

1

使用您在评论中提供的数据。请注意mischva11的评论和下面的链接。

旋转R中的x轴标签以进行条形图

只需要plot函数中的las = 2参数。

Stripped_DATA <- structure(list(Epoch = structure(c(1110925802, 1110929408, 1110933014, 
                                                    1110936616, 1110940217), 
                                                  class = c("POSIXct", "POSIXt"), 
                                                  tzone = "Europe/Helsinki"), 
                                Timediff = c(-1.4653909261659, -1.46512243581845, 
                                             -1.46505141447328, -1.46503418192427, 
                                             -1.46464648029912)), 
                           .Names = c("Epoch", "Timediff"), row.names = c("11070", "21070", 
                                                                          "31070", "41070",
                                                                          "51070"), 
                           class = "data.frame")


plot(Stripped_DATA, main = "Maser-69 hourly averages raw data, -3 < 3 microseconds",
     xlab = "Date", ylab = "microseconds", 
     las = 2) # see
ticks <- seq(as.POSIXct("2005-03-16 00:30:02", format = "%F %T"),
             as.POSIXct("2019-04-19 14:29:55", format = "%F %T"), by = "4 months")
labels <- seq(as.Date("2005-03-16"), as.Date("2019-04-19"), by = "4 months")
axis.POSIXct(1, at = ticks, format = "%Y-%m-%d %H", labels = labels)

enter image description here


1
如果您只想使用刻度,请在使用xaxt='n'绘制图表之前从图表中删除标签,并使用las=2旋转图表标签。
   plot(Stripped_DATA, main = "Maser-69 hourly averages raw data, -3 < 3 microseconds",
         xlab = "Date", ylab = "microseconds" , las=2, xaxt="n")
    ticks <- seq(as.POSIXct("2005-03-16 00:30:02", format = "%F %T"),
                 as.POSIXct("2019-04-19 14:29:55", format = "%F %T"), by = "4 months")
    labels <- seq(as.Date("2005-03-16"), as.Date("2019-04-19"), by = "4 months")
axis.POSIXct(1, at = ticks, format = "%Y-%m-%d %H", labels = labels, las=2)

plot

说明:您的axis.POSIXct()函数添加了额外的标签。这些标签可能会与您的图表标签重叠,从而创建您显示的输出。因此,不要使用xaxt='n'绘制原始标签。
请注意,这仅适用于您只想要axis.POSIXct标签的情况。

如何删除或隐藏x轴标题?xlab = NULL无效。 - Ismo Mehtälä

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接