当周年份为数字时,在绘制每周明智的年度数据时删除图中的间隙
Remove gaps in a plot when plotting weekwise yealy data when the weekyear is numeric
我正在以“201301”格式使用 x 轴绘制每周价格变化,其中 2013 年是年份,01 是星期。我在第 52 周后的年份之间有差距,即“201352”到“201399”,因为它是整数格式。如何消除间隙?
以下是我在 docs.google.com
使用的数据
df <- read.table(text="Year_week Price
201301 1769
201302 1764
201303 1764
201304 1762.56
201305 1775
201306 1776
201307 1775
201308 1800
201309 1827
201310 1846
201311 1848
201312 1837.5
201313 1862
201314 1862.5
201315 1862
201316 1863
201317 1862
201318 1872
201319 1900
201320 1920
201321 1914
201322 1900
201323 1890
201324 1896
201325 1898
201326 1884
201327 1872
201328 1872
201329 1869
201330 1850
201331 1836
201332 1840
201333 1848
201334 1850
201335 1863
201336 1869
201337 1860
201338 1862
201339 1869
201340 1859
201341 1850
201342 1870
201343 1875
201344 1875
201345 1881
201346 1870
201347 1887.5
201348 1870
201349 1879.5
201350 1886
201351 1872.78
201352 1914
201401 1958
201402 1962
201403 1958
201404 1955
201405 1960.98
201406 1989.5
201407 2021.882
201408 2016
201409 1999.2
201410 1987.5
201411 1992
201412 2033.5
201413 2054
201414 2050
201415 2028.6
201416 2040
201417 2028.6
201418 2024
201419 2002
201420 2000
201421 1998.81
201422 2000
201423 2002
201424 2010.96
201425 1999.2
201426 1995",header=TRUE)
将数值转换为因子。
plot(as.factor(df$Year_week),df$Price)
编辑:
另一种方法是以日期格式保存日期,这样绘图将按预期运行。我们需要使用 ISOweek
包将 YYYYWW
转换为 YYYY-MM-DD
然后绘图。
library(ISOweek)
library(ggplot2)
#convert numeric to date - YYYYWW to YYYY-MM-DD
df$Year_week_clean <- ISOweek2date(
paste0(substr(df$Year_week,1,4),"-W",
substr(df$Year_week,5,7),"-1")
)
#plot original
ggplot(data=df, aes(Year_week,Price)) +
geom_point() +
ggtitle("YYYYWW")
#plot clean
ggplot(data=df, aes(Year_week_clean,Price)) +
geom_point() +
ggtitle("YYYY-MM-DD")
我正在以“201301”格式使用 x 轴绘制每周价格变化,其中 2013 年是年份,01 是星期。我在第 52 周后的年份之间有差距,即“201352”到“201399”,因为它是整数格式。如何消除间隙?
以下是我在 docs.google.com
使用的数据df <- read.table(text="Year_week Price
201301 1769
201302 1764
201303 1764
201304 1762.56
201305 1775
201306 1776
201307 1775
201308 1800
201309 1827
201310 1846
201311 1848
201312 1837.5
201313 1862
201314 1862.5
201315 1862
201316 1863
201317 1862
201318 1872
201319 1900
201320 1920
201321 1914
201322 1900
201323 1890
201324 1896
201325 1898
201326 1884
201327 1872
201328 1872
201329 1869
201330 1850
201331 1836
201332 1840
201333 1848
201334 1850
201335 1863
201336 1869
201337 1860
201338 1862
201339 1869
201340 1859
201341 1850
201342 1870
201343 1875
201344 1875
201345 1881
201346 1870
201347 1887.5
201348 1870
201349 1879.5
201350 1886
201351 1872.78
201352 1914
201401 1958
201402 1962
201403 1958
201404 1955
201405 1960.98
201406 1989.5
201407 2021.882
201408 2016
201409 1999.2
201410 1987.5
201411 1992
201412 2033.5
201413 2054
201414 2050
201415 2028.6
201416 2040
201417 2028.6
201418 2024
201419 2002
201420 2000
201421 1998.81
201422 2000
201423 2002
201424 2010.96
201425 1999.2
201426 1995",header=TRUE)
将数值转换为因子。
plot(as.factor(df$Year_week),df$Price)
编辑:
另一种方法是以日期格式保存日期,这样绘图将按预期运行。我们需要使用 ISOweek
包将 YYYYWW
转换为 YYYY-MM-DD
然后绘图。
library(ISOweek)
library(ggplot2)
#convert numeric to date - YYYYWW to YYYY-MM-DD
df$Year_week_clean <- ISOweek2date(
paste0(substr(df$Year_week,1,4),"-W",
substr(df$Year_week,5,7),"-1")
)
#plot original
ggplot(data=df, aes(Year_week,Price)) +
geom_point() +
ggtitle("YYYYWW")
#plot clean
ggplot(data=df, aes(Year_week_clean,Price)) +
geom_point() +
ggtitle("YYYY-MM-DD")