【问题标题】:X-axis labels not printing on Base Plotting System On RX 轴标签未在 R 上的基础绘图系统上打印
【发布时间】:2021-05-19 02:27:58
【问题描述】:

“R 版本 4.0.3 (2020-10-10)”

我有一些时间序列数据,我试图在其中查看消费随时间的变化。这是一个代表。

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

初始plot() 中输出的图形正是我想要的,只是它没有任何 x 轴标签。

我将 Base Plotting 用于作业而不是日常使用,并且通常会使用 ggplot。 我一直在试图弄清楚为什么 x 轴没有绘图。最初我认为问题出在日期变量上,并尝试用lubridate::ymd() 清理它。然而,当我开始为这个问题制作上述代表时,X 轴标签和刻度很清楚,因为整体没有打印。在第二个图中,我将消费变量放在 x 轴上。我惊讶地发现日期在 Y 轴上整齐地打印出来。

我做错了什么?

【问题讨论】:

    标签: r plot axis-labels


    【解决方案1】:

    如果您想更好地控制轴标签和标题发生的情况,您可以手动创建它们。所以,首先制作一个没有标题和标签的情节。然后,使用axis()mtext() 手动创建它们。在此过程中,您可以使用par(mar=...) 增加地块底部的空间。微调是使用 lascex.axisline 等参数完成的。最后,您将 mar 重置为其旧值。

    您可以使用下面的代码获取更详细的 X 轴标签

    ### 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)
    

    这会产生以下图表:

    替代方案 每 7 个项目的刻度和标签

    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))
    

    它给出了以下图片:

    请告诉我这是否是您想要的。

    【讨论】:

    • 请注意,当我们包含两个图时,您的解决方案仍然会产生错误:Data$date2 &lt;-format(Data$date, "%d%b") Data$date2 &lt;- as.Date(Data$date2, format = "%d%b") par(mar = c(7,4,4,2)) 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) plot(Data$date ~ Data$Consumption, type= "l", xaxt = "n", xlab = NA)
    • 我没有收到任何错误。错误信息是什么?
    • 没有这样的错误信息。每当我们在一个块中绘制两个图表时,第二个总是会丢失标签。但是,如果您将每个图表放在不同的块中,它将正确运行以正确显示轴标签。
    • @doctshinds 1.我不认为消费必须是 100 个值 - R 从字面上抱怨这一点。 2. 我现在明白问题所在了。 par() 不会在每一个对我来说没有意义的情节中重置。我一旦我重置了一切,它就像一个魅力。 @KoenV 该图按预期工作。如果您在上面阅读,我意识到问题在于必须在每个情节中重置 par()。并且似乎将xaxt='n' 放在par() 上使其更加永久,并且需要使用xaxt='s' 重置。将其保留在“plot()”调用中会改变每个情节。
    【解决方案2】:

    我很容易看到两个问题:

    1. 更改:消耗

    2. 问题在于'par'。当一个块中有多个绘图时,与 ggplot 不同,绘图无法正确处理。删除 par 并在下面运行它应该可以工作

        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') 
    

    请注意,无论何时定义 par 以及在两个不同的块中运行每个绘图时,标签都会正确显示。你不会有任何问题。但是,当您将两个图表都绘制在一个块中时,如果您有par,您总是会遇到问题。

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 2015-07-27
      • 2022-10-05
      • 2021-12-03
      • 2021-01-04
      • 2014-06-29
      • 2021-11-05
      • 2021-06-12
      • 1970-01-01
      相关资源
      最近更新 更多