我對 R 很陌生。我想用 來按月繪制圖表ggplot2,但年份變數的最后日期在 x 軸上交織在一起。我附上了下面的圖片。關于如何調整 x 軸寬度的任何想法?我還可以在日期變數中列印每一年嗎?我的日期是在 2010-2020 年之間。
在此處輸入圖片說明
uj5u.com熱心網友回復:
更新版本。歐普似乎在要求這個。時間變數顯示完整日期(“年-月-日”)。修改 x 軸scale_x_date用于僅顯示日歷年:
# example dataset
dt <- data.table(date=as.Date(seq(1,1000,100),origin = "2010-01-01"),var=rnorm(10))
head(dt)
# display only the YEAR
ggplot(dt,aes(y=var,x=date)) geom_point()
scale_x_date(date_breaks = "1 year", date_labels = "%Y")
# display 6 months intervals
ggplot(dt,aes(y=var,x=date)) geom_point()
scale_x_date(date_breaks = "6 months", date_labels = "%b %Y")
舊版本:時間變數僅顯示年份。
為了顯示每一年的資料,這里有兩個選項。
為了增加寬度,我猜你的意思是永久保存繪圖。
說明:如果您使用 R Studio,就像螢屏截圖中顯示的那樣,您可以使用 GUI 以多種方式更改繪圖的臨時可視化。
澄清 #2:檢查?facet_wrap如何在多行和多列中顯示構面,這也有助于圖的特定可視化。
library(ggplot2)
library(data.table)
# create example dataset (no values for 2015)
dt <- data.table(var=rnorm(40),year=sample(c(seq(2010,2014,1),seq(2016,2020,1)),40,replace = T))
# clearly plot each specific year by considering it as factor (2015 not shown)
ggplot(dt,aes(y=var,x=as.factor(year))) geom_point()
xlab("Year") # nicer x-axis naming
# clearly plot each specific year by modifying breaks (shows also empty years if present)
ggplot(dt,aes(y=var,x=year)) geom_point()
scale_x_continuous(breaks = seq(min(dt[,year]),max(dt[,year]),1))
# save the file with exaggerated width (just an example)
ggsave("myfilepath/myfilename.jpg",width=20,height=4,units = "cm")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/394498.html
