使用ggplot scale_x_yearmon缺少yearmon标签 [英] missing yearmon labels using ggplot scale_x_yearmon

查看:133
本文介绍了使用ggplot scale_x_yearmon缺少yearmon标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按月和年对一些数据进行了分组,使用Zoo转换为Yearmon,现在将其绘制在ggplot中.有谁知道为什么缺少一个勾号标签,而当月没有数据的情况下,为什么在2018-07年度没有一个勾号标签?

I have grouped some data by month and year, converted to yearmon using zoo and am now plotting it in ggplot. Does anyone know why one of the ticklabels are missing and there is one for 2018-07, when there is no data for that month?

示例数据:

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")

推荐答案

我认为scale_x_yearmon是用于xy图的,因为它调用scale_x_continuous,但是我们可以像这样自己调用scale_x_continuous(仅标有#的行) #已更改):

I think scale_x_yearmon was meant for xy plots as it calls scale_x_continuous but we can just call scale_x_continuous ourselves like this (only the line marked ## is changed):

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")) ##

这篇关于使用ggplot scale_x_yearmon缺少yearmon标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆