简体   繁体   中英

X-axis labels not printing on Base Plotting System On R

"R version 4.0.3 (2020-10-10)"

I have a some time-series data where I am trying to look at the change in consumption over time. Here is a reprex.

set.seed(1)
date <-  seq(as.POSIXct('2010-01-01'),as.POSIXct('2010-04-10'),by=86400)
Consumption <- rnorm(99)


Data <- data.frame(date,Consumption)


plot(Data$Consumption~Data$date,type='l') # X-axis labels and ticks not printing
par(yaxt='n')
axis(side = 2,at=seq(-3,3,0.5),labels = seq(-3,3,0.5)) # This works on the first  plot on the y axis



plot(Data$date~Data$Consumption,type='l') # X-axis refusing to print despite assigning it.
par(xaxt='n')
axis(side = 1,at=seq(-3,3,0.5),labels = seq(-3,3,0.5)) # This works on the first  plot

The graph outputted in the initial plot() is exactly what I want except for the fact it doesn't have any labels for the x-axis.

I am using Base Plotting for an assignment rather than everyday use and would usually use ggplot. I have been trying to figure out why the x-axis is not plotting. Initially I thought the problem was with the date variable and tried cleaning it up with lubridate::ymd() . However when I started making the above reprex for the purpose of this question it is clear to the X-axis labels and ticks as whole is not printing. In the second plot I put the consumption variable on the x-axis. I was surprised to find that the date is printing neatly on its own on the Y-axis.

What am I doing wrong?

There are two issues I can see readily:

  1. change: Consumption <- rnorm(99) to Consumption <- rnorm(100) to match date column.

  2. The problem is with 'par'. When there are multiple plots within a chunk, unlike ggplot, plot does not handle properly. Remove par and run the below it should work

    set.seed(1)
    date <-  seq(as.POSIXct('2010-01-01'),as.POSIXct('2010-04-10'),by=86400)
    Consumption <- rnorm(100)
    Data <- data.frame(date,Consumption)
    plot(Data$Consumption~Data$date,type='l') 
    plot(Data$date~Data$Consumption,type='l') 

Please note whenever you define par and when you run each plot in two different chunks, the labels will display properly. You will not have any issue. But when you plot both the charts in a single chunk, you will always have problem if you have par.

When you want more control over what happens with axis labels and title, you could create them manually. So, first produce a plot without title and label. Then, create them manually with axis() and mtext() . In the process, you may increase the room at the bottom of the plot with par(mar=...) . F.netuning is done with arguments like las , cex.axis , and line . And at the end, you reset mar to its old values.

You could use the code below to get more detailed X-axis labels

### format to day (probably not the best way to do this) 
Data$date2 <-format(Data$date, "%d%b")
Data$date2 <- as.Date(Data$date2, format = "%d%b")

### increase room at bottom of the plot for X-axis title
### the labels will eat up space, if we do nothing it will be a mess
### set title with mtext later
par(mar = c(7,4,4,2))

### plot without X-axis labels (xaxt) and title (xlab)
### work with "at" and "labels" in axis-function
### rotate labels 90° (las) an reduce font (cex.axis)
### put title 5 lines below graph (line)
###
### Remark: the graph window has to be big enough
plot(Data$Consumption ~ Data$date, type= "l", xaxt = "n", xlab = NA) 
axis(side = 1, at = Data$date, labels =  Data$date2, las = 2, cex.axis=.75)
mtext(side = 1, text = "Date", line = 5)

This yields the following graph:

在此处输入图像描述

ALTERNATIVE ticks and labels for every 7th item

per7 <- seq(1, 99, 7)
plot(Data$Consumption ~ Data$date, type= "l", xaxt = "n", xlab = NA) 
axis(side = 1, at = Data$date[per7], labels =  Data$date2[per7], las = 2, cex.axis=.75)
mtext(side = 1, text = "Date", line = 5)

### reset mar
par(mar = c(5,4,4,2))

which gives the following picture:

在此处输入图像描述

Please let me know whether this is what you wanted.

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