I've been trying to figure out how to use the solutions here and here when the x axis of the bar chart are POSIXt elements.
I want to plot a mean wind speed daily forecast and display the mean speed in each bar. I'm creating two plots, one with the daily means and another one separating night and day. The problem happens in the night and day plot, it seems geom_text() can't use the position_dodge() argument properly when the x axis is POSIXt.
require(lubridate)
data = seq.POSIXt(from=ymd_hm("2017-06-20 00:00"),to=ymd_hm("2017-06-29 00:00"),by=86400)
d_value=c(6.375, 5.875, 6.525, 6.200, 5.475, 4.075, 4.000, 6.200, 5.775, 6.425)
n_value=c(5.725, 5.700, 5.600, 5.750, 4.850, 3.750, 4.025, 7.225, 6.450, 6.100)
t_value=c(6.0500, 5.7875, 6.0625, 5.9750, 5.1625, 3.9125, 4.0125, 6.7125, 6.1125, 6.2625)
d_df = data.frame(data=data,value=d_value,key1="d",key2=1)
n_df = data.frame(data=data,value=n_value,key1="n",key2=1)
t_df = data.frame(data=data,value=t_value,key1="t",key2=2)
df = rbind(d_df,n_df,t_df)
ggplot(data=df)+
geom_col(aes(x=data,y=value,fill=key1), position="dodge",color="black")+
geom_text(aes(x=data,y=value+0.2,label=sprintf("%.1f",value)), vjust=2,col="black",position = position_dodge(width =0.9))+
facet_wrap(~key2,nrow=2)
The result:
EDIT: from: How to set Bin Width With geom_bar stat="identity" in a time Series plot?
Actually, width = 0.9 means 0.9 seconds in the POSIXt axis. Setting position_dodge(width =86400) gives: