简体   繁体   English

如何在 R 中使用 ggplot plot 在 x 轴上一周的第一天

[英]How in R using ggplot plot first days of the week on x axis

I have the following data:我有以下数据:

structure(list(Date = structure(c(1594166400, 1594080000, 1593993600, 
1593734400, 1593648000, 1593561600, 1593475200, 1593388800, 1593129600, 
1593043200, 1592956800, 1592870400, 1592784000, 1592524800, 1592438400, 
1592352000, 1592265600, 1592179200, 1591920000, 1591833600, 1591747200, 
1591660800, 1591574400, 1591315200, 1591228800, 1591142400, 1591056000, 
1590969600, 1590710400, 1590624000, 1590537600, 1590451200, 1590364800, 
1590105600, 1590019200, 1589932800, 1589846400, 1589760000, 1589500800, 
1589414400, 1589328000, 1589241600, 1589155200, 1588896000, 1588809600, 
1588723200, 1588636800, 1588550400, 1588291200, 1588204800, 1588118400, 
1588032000, 1587945600, 1587686400, 1587600000, 1587513600, 1587427200, 
1587340800, 1587081600, 1586995200, 1586908800, 1586822400, 1586736000, 
1586390400, 1586304000, 1586217600, 1586131200, 1585872000, 1585785600, 
1585699200, 1585612800, 1585526400, 1585267200, 1585180800, 1585094400, 
1585008000, 1584921600, 1584662400, 1584576000, 1584489600, 1584403200, 
1584316800, 1584057600, 1583971200, 1583884800, 1583798400, 1583712000, 
1583452800, 1583366400, 1583280000, 1583193600, 1583107200, 1582848000, 
1582761600, 1582675200, 1582588800, 1582502400, 1582243200, 1582156800, 
1582070400, 1581984000, 1581897600, 1581638400, 1581552000, 1581465600, 
1581379200, 1581292800, 1581033600, 1580947200, 1580860800, 1580774400, 
1580688000, 1580428800, 1580342400, 1580256000, 1580169600, 1580083200, 
1579824000, 1579737600, 1579651200, 1579564800, 1579478400, 1579219200, 
1579132800, 1579046400, 1578960000, 1578873600, 1578614400, 1578528000, 
1578441600, 1578355200, 1578268800), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Price = c(43.24, 43.08, 43.1, 42.8, 43.14, 42.03, 
41.15, 41.71, 41.02, 41.05, 40.31, 42.63, 43.08, 42.19, 41.51, 
40.71, 40.96, 39.72, 38.73, 38.55, 41.73, 41.18, 40.8, 42.3, 
39.99, 39.79, 39.57, 38.32, 35.33, 35.29, 34.74, 36.17, 35.53, 
35.13, 36.06, 35.75, 34.65, 34.81, 32.5, 31.13, 29.19, 29.98, 
29.63, 30.97, 29.46, 29.72, 30.97, 27.2, 26.44, 25.27, 22.54, 
20.46, 19.99, 21.44, 21.33, 20.37, 19.33, 25.57, 28.08, 27.82, 
27.69, 29.6, 31.74, 31.48, 32.84, 31.87, 33.05, 34.11, 29.94, 
24.74, 22.74, 22.76, 24.93, 26.34, 27.39, 27.15, 27.03, 26.98, 
28.47, 24.88, 28.73, 30.05, 33.85, 33.22, 35.79, 37.22, 34.36, 
45.27, 49.99, 51.13, 51.86, 51.9, 50.52, 52.18, 53.43, 54.95, 
56.3, 58.5, 59.31, 59.12, 57.75, 57.67, 57.32, 56.34, 55.79, 
54.01, 53.27, 54.47, 54.93, 55.28, 53.96, 54.45, 58.16, 58.29, 
59.81, 59.51, 59.32, 60.69, 62.04, 63.21, 64.59, 65.2, 64.85, 
64.62, 64, 64.49, 64.2, 64.98, 65.37, 65.44, 68.27, 68.91)), row.names = c(NA, 
-132L), class = c("tbl_df", "tbl", "data.frame"))

When I use code below to plot a graph y-axis has only names of the months (in total 3 names).当我使用下面的代码 plot 时,图表 y 轴只有月份的名称(总共 3 个名称)。 However, I want that on the y-axis only the first days of the week are depicted like "06-01-2020", "13-01-2020"... How can I fix it?但是,我希望在 y 轴上仅将一周的第一天描绘成“06-01-2020”、“13-01-2020”……我该如何解决?

ggplot(Crude_oil, aes(x=Date, group=Group, color=Group)) +
  geom_line(aes(y = Price), size = 2)

You need to specify the labels.您需要指定标签。 It is straightforward to create a vector of labels with some help from package lubridate :在 package lubridate的帮助下创建标签向量很简单:

all_dates <- seq(as.POSIXct("2020-01-01"), lubridate::now(), "1 day")
Mondays <- all_dates[lubridate::wday(all_dates) == 2]

Now we can plot using the labels parameter in scale_x_datetime :现在我们可以使用 scale_x_datetime 中的标签参数scale_x_datetime

ggplot(Crude_oil, aes(x=Date)) +
  geom_line(aes(y = Price), size = 2) +
  scale_x_datetime(breaks = Mondays, labels = as.Date(Mondays)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此处输入图像描述

Doesn't need to be so complicated.没必要这么复杂。 No need to define the labels first.无需先定义标签。 Just set the limits and use date_breaks .只需设置限制并使用date_breaks

# mydat <- your structure

ggplot(mydat, aes(x=lubridate::ymd(Date))) + # x needs to be date
  geom_line(aes(y = Price), size = 2)  +
  scale_x_date(limits = c(as.Date("2020-01-06"),NA), date_breaks = "weeks") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

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

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