简体   繁体   English

我的 R plot 的 x 轴上的值是随机的,而不是数据框中的日期

[英]Values on the x-axis of my R plot are random rather than the dates from the data frame

I loaded in a dataset to R that looks like this in the header:我将数据集加载到 R 中,在 header 中看起来像这样:

        date     a     b     c
1 2017-01-01 -0.98 -1.35 -2.81
2 2017-02-01 -1.63 -2.18 -1.79
3 2017-03-01 -0.92  0.80 -3.33
4 2017-04-01  0.44  0.48 -2.11
5 2017-05-01  1.46 -3.11 -3.67
6 2017-06-01 -0.32  2.46  1.45

The full dataset includes 4 years of data with a total of 48 obs (from Jan-2017 to Dec-2020).完整的数据集包括 4 年的数据,总共 48 个 obs(从 2017 年 1 月到 2020 年 12 月)。

After loading in the dataset I change the format of the date variable to YYYYMM by using the code:加载数据集后,我使用以下代码将日期变量的格式更改为 YYYYMM:

df$date <- format(as.Date(df$date), "%Y%m")

This results in the dates looking like this:这导致日期如下所示:

    date     a     b     c
1 201701 -0.98 -1.35 -2.81
2 201702 -1.63 -2.18 -1.79
3 201703 -0.92  0.80 -3.33
4 201704  0.44  0.48 -2.11
5 201705  1.46 -3.11 -3.67
6 201706 -0.32  2.46  1.45

After doing this I plot the data with this code:完成此操作后,我使用此代码 plot 数据:

plot(df$a, type="l", col="darkgreen", lwd=1, xlab="date", ylab="$", xaxs="i")
lines(df$b, col="red", lwd=1, xaxs="i")
lines(df$c, col="blue", lwd=1, xaxs="i")
legend("bottomleft", inset= 0.04, legend=c("a", "b", "c"),
col=c("darkgreen", "red", "blue"), lwd=3, cex=0.8)

Which results in the plot below:这导致下面的 plot :

在此处输入图像描述

However, the values of the x-axis do not show me the years so that I can measure the performance of a, b and c over time.但是,x 轴的值并没有显示年份,因此我可以测量 a、b 和 c 随时间推移的性能。 How do I replace the values of the x-axis with the years in my dataset.如何将 x 轴的值替换为数据集中的年份。 And also, how do I make sure that only the years will be included on my x-axis and not my months as well?而且,我如何确保只有年份将包含在我的 x 轴上,而不是我的月份?

The answers to this question I've seen so far has been to format the date etc. This is done and seems to work fine.到目前为止,我看到的这个问题的答案是格式化日期等。这已经完成并且似乎工作正常。 Can anyone please tell me what to do about this issue?谁能告诉我如何处理这个问题?

Here are two solutions base R and ggplot2 .这里有两个基于 R 和ggplot2的解决方案。

1. Base R 1.底座R

To plot multiple lines use either matplot or matlines .到 plot 多行使用matplotmatlines

colrs <- c("darkgreen", "red", "blue")
matplot(df[[1]], df[-1], 
        type = "l", lty = "solid", lwd = 1,
        col = colrs,
        xlab = "date", ylab = "$", xaxs = "i")
legend("bottomleft", inset = 0.04, legend = c("a", "b", "c"),
       col = colrs, lwd = 3, cex = 0.8)

在此处输入图像描述

2. ggplot2 2. ggplot2

The data is in the wide format and one line per column vector is to be plotted against an x axis vector, in this case the date vector.数据采用宽格式,每列向量一行将针对x轴向量绘制,在本例中为date向量。 This sort of problem is usually a data reformating problem.这类问题通常是数据重组问题。 See reshaping data.frame from wide to long format .请参阅将 data.frame 从宽格式重塑为长格式

library(ggplot2)
library(dplyr)
library(tidyr)

df %>%
  pivot_longer(-date) %>%
  ggplot(aes(date, value, colour = name)) +
  geom_line() +
  scale_colour_manual(breaks = c("a", "b", "c"), values = c("darkgreen", "red", "blue")) +
  scale_x_date(date_labels = "%Y-%m") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust = 0.5))

在此处输入图像描述

Data数据

df <- read.table(text = "
        date     a     b     c
1 2017-01-01 -0.98 -1.35 -2.81
2 2017-02-01 -1.63 -2.18 -1.79
3 2017-03-01 -0.92  0.80 -3.33
4 2017-04-01  0.44  0.48 -2.11
5 2017-05-01  1.46 -3.11 -3.67
6 2017-06-01 -0.32  2.46  1.45
", header = TRUE)
df$date <- as.Date(df$date)

You replace the values of the x axis with the years in your dataset by adding xaxt="n" to your plot() command (which removes the current x axis) and calling通过将xaxt="n"添加到plot()命令(删除当前 x 轴)并调用

axis(1,at=1:nrow(df),labels=format(as.Date(df$date),"%Y"))

afterwards (which creates the desired x axis).之后(创建所需的 x 轴)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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