简体   繁体   中英

Plotting dates in r

I have written the following plot command:

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)

which gives the x-axis shown in the plot. How do I eliminate the three yearly ticks which overlap the dates? Also, is there a way to turn the date labels from horizontal to vertical so I can add more date labels?

在此输入图像描述

Using your data provided in the comments. Note the comment from mischva11 and the link below.

Rotating x axis labels in R for barplot

Only the las = 2 argument in the plot function is required.

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)

在此输入图像描述

If you just want to use your ticks, remove the labels from your plot before plotting it with xaxt='n' and rotate your plot labels with 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)

情节

explanation: your axis.POSIct() function adds additional labels. Those can overlap with your plot labels and create your shown output. So don't plot the original labels with xaxt='n'

Be aware this is just IF you only want to have your axis.POSIXct labels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM