我正在努力創建兩個圖形并使用拼湊將它們合并為一個圖形。x 軸是日期,頂部圖是 geom_line/geom_point 圖,底部圖是使用 geom_col 創建的。合并后,我希望頂部圖形中的點與底部圖形中的列中心對齊。
我希望 b/c x 軸跨越相同的日期,點將與列的中心對齊。在幾次嘗試中,我無法將點置于列上方的中心。生成的圖形(和代碼)如下。

library(patchwork)
nn<-15
p1data = tibble(date=ymd('2021-01-01') days(0:(nn-1)), values=floor(runif(nn, 5.0, 75)))
p2data <- tibble(initdate = ymd('2021-01-01') days(0:(nn-1)), data=runif(nn,4,10))
# set up a tibble to shade in every other day
shade <- tibble(min=ymd('2021-01-01 00:00') days(0:20),max=lead(min))
# Remove every other row
toDelete <- seq(1,dim(shade)[1],by=2)
shade <- shade[-toDelete,]
plot_limits = c(ymd('2021-01-01'),ymd('2021-01-16'))
# Orginal Attempt
p1 <- p1data %>%
ggplot()
geom_rect(data=shade, aes(xmin=min,xmax=max,ymin=-Inf,ymax=Inf),fill='grey80' )
geom_line(aes(x=date,y=values))
geom_point(aes(x=date,y=values),color='red',size=3)
scale_x_date(expand=c(0,0),
date_breaks = '1 days',
date_labels = '%b-%d',
limits=plot_limits)
labs(title='two plots combined')
theme_bw()
theme(plot.title=element_text(hjust=0.5),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.x=element_blank())
p2 <- p2data %>%
ggplot()
geom_col(aes(x=initdate,y=data, fill=data))
scale_x_date(expand=c(0,0),date_breaks = '1 days',date_labels = '%b-%d')
theme_bw()
theme(plot.title=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_text(angle=0),
legend.direction = "horizontal",
legend.position = "bottom",
legend.key.width=unit(1.25,"cm"),
legend.key.height=unit(.25,"cm"),
legend.box = "vertical",
legend.box.background = element_blank(),
legend.title = element_text(face = "bold"),
legend.spacing = unit(1,"cm"),
legend.justification = "center",
legend.margin = margin(0.,0.,0.,0., unit="cm"),
legend.background = element_blank(),
plot.margin = margin(0, 0, 0, 0, "cm")
)
p1/p2
是否有其他方法可以將頂部圖表中每一天的點與底部圖表中的列居中?
uj5u.com熱心網友回復:
在準備這個問題的程序中,我能夠開發出一個解決方案。解決方案是對頂部圖形使用日期時間格式,而不僅僅是日期。然后可以將與資料關聯的日期時間設定為 YYYY-MM-DD 12:00。然后在一天中間而不是在每天開始時繪制資料。
生成的圖形是:

實作它的代碼是:
# The solution that works
# Specify the dates in datetime format and use the hours to center the data in the day.
# The shade data for daily shades must be in datetime asd well.
# set up a tibble to shade in every other day
shade_dtm <- tibble(min=ymd_hm('2021-01-01 00:00') days(0:20),max=as_datetime(lead(min)))
# Remove every other row
toDelete <- seq(1,dim(shade_dtm)[1],by=2)
shade_dtm <- shade_dtm[-toDelete,]
# create data
p1data_dtm = tibble(date=ymd_hm('2021-01-01 12:00') days(0:(nn-1)), values=floor(runif(nn, 5.0, 75)))
plot_limits_dtm = c(ymd_hm('2021-01-01 00:00'),ymd_hm('2021-01-16 00:00'))
p1 <- p1data_dtm %>%
ggplot()
geom_rect(data=shade_dtm, aes(xmin=min,xmax=max,ymin=-Inf,ymax=Inf),fill='grey80' )
geom_line(aes(x=date,y=values))
geom_point(aes(x=date,y=values),color='red',size=3)
scale_x_datetime(expand=c(0,0),
date_breaks = '1 days',
date_labels = '%b-%d',
limits=plot_limits_dtm)
labs(title='two plots combined: data aligned')
theme_bw()
theme(plot.title=element_text(hjust=0.5),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.x=element_blank())
p2 <- p2data %>%
ggplot()
geom_col(aes(x=initdate,y=data, fill=data))
scale_x_date(expand=c(0,0),date_breaks = '1 days',date_labels = '%b-%d')
theme_bw()
theme(plot.title=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_text(angle=0),
legend.direction = "horizontal",
legend.position = "bottom",
legend.key.width=unit(1.25,"cm"),
legend.key.height=unit(.25,"cm"),
legend.box = "vertical",
legend.box.background = element_blank(),
legend.title = element_text(face = "bold"),
legend.spacing = unit(1,"cm"),
legend.justification = "center",
legend.margin = margin(0.,0.,0.,0., unit="cm"),
legend.background = element_blank(),
plot.margin = margin(0, 0, 0, 0, "cm")
)
p1/p2
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328494.html
下一篇:使用plotnine進行軸縮放
