unk*_*own 1 r graph ggplot2 zoo
我已经按月和年对一些数据进行了分组,使用zoo将其转换为yearmon,现在正在将其绘制在ggplot中。有谁知道为什么其中一个刻度标签缺失,而 2018-07 年却有一个,而该月没有数据?
示例数据:
df <- data.frame(dates = c("2019-01", "2019-02", "2018-08", "2018-09", "2018-10", "2018-11", "2018-12"), values= c(0,1,2,3,4,5,6))
df$dates <- as.yearmon(df$dates)
ggplot(df, aes(x = dates, y = values)) +
geom_bar(position="dodge", stat="identity") +
theme_light() +
xlab('Month') +
ylab('values')+
scale_x_yearmon(format="%Y %m")
Run Code Online (Sandbox Code Playgroud)
我认为scale_x_yearmon
是针对 xy 图的,因为它调用了,scale_x_continuous
但我们可以scale_x_continuous
这样调用自己(仅更改标记为 ## 的行):
ggplot(df, aes(x = dates, y = values)) +
geom_bar(position="dodge", stat="identity") +
theme_light() +
xlab('Month') +
ylab('values')+
scale_x_continuous(breaks=as.numeric(df$dates), labels=format(df$dates,"%Y %m")) ##
Run Code Online (Sandbox Code Playgroud)