R 我想格式化我的X轴在GGPLOT是年和月,而不是天

R 我想格式化我的X轴在GGPLOT是年和月,而不是天,r,ggplot2,lubridate,R,Ggplot2,Lubridate,在我的ggplot调用中,我绘制如下,X轴仅显示标签中的每个singke日期,如20171130等,我希望我的标签如11月17日、12月17日等 # Invoke a ggplot with date vs. sessions data, segmented by device category ggplot(data = gaDataExt, mapping = aes(x = date, y = pageviews, group = 1, color = deviceCategory)

在我的ggplot调用中,我绘制如下,X轴仅显示标签中的每个singke日期,如20171130等,我希望我的标签如11月17日、12月17日等

# Invoke a ggplot with date vs. sessions data, segmented by device category
  ggplot(data = gaDataExt, mapping = aes(x = date, y = pageviews, group = 1, color = deviceCategory) ) + 
  geom_bar(stat = "identity") +
  theme_bw() + 
  labs(x = "date", y = "Pageviews by Device Category") +
  labs(title = "Andy D) Users by Device Category", x="Date", y="Total Page views") +
  ylim(0,NA) 
索姆博迪向我们建议了一种洗脱剂,它似乎和我要找的不完全一样

gaDataExt %>% 
  mutate(date = ymd(date),
         month = year(date)) %>% 
  group_by(deviceCategory) %>% 
  summarise_all(funs(mean)) %>% 
  ggplot(aes(month, pageviews)) + geom_point() + 
  facet_grid(deviceCategory~.) +
  theme_bw()
这是我的当前输出,您可以看到它都聚集在日期轴上,我只想在该轴上显示月份和年份

这是有人建议的,但似乎不起作用

这是我试图绘制的数据帧的头部

> head(gaData_User)
        date          userType deviceCategory   country            region        city users
1 30-11-2017       New Visitor        desktop Australia          Victoria   Melbourne     2
2 30-11-2017       New Visitor         mobile Australia   New South Wales      Sydney     1
3 30-11-2017       New Visitor         mobile Australia          Victoria   Melbourne     2
4 30-11-2017 Returning Visitor        desktop Australia          Victoria   Melbourne     1
5 30-11-2017 Returning Visitor        desktop Australia          Victoria Warrnambool     1
6 30-11-2017 Returning Visitor        desktop Australia Western Australia       Perth     1

您可以使用一个简单的示例
scale\u x\u date

last_month <- Sys.Date() - 0:29
df <- data.frame(
  date = last_month,
  price = runif(30)
 )

ggplot(df, aes(date, price)) +
  geom_line() + 
  scale_x_date(date_labels = "%Y-%m")

上个月你为什么不更新你原来的问题?如果有人提出了解决方案,但它不是您想要的,那么请澄清您的原始问题。请您解释一下mutate(date=ymd(date),month=year(date))的目的是什么?这真的是为了创建一个包含年份的
month
列吗?请再次回答您的问题,并提供一个,特别是一个样本数据集来重现问题。谢谢。请尝试
ggplot(data=gaDataExt,mapping=aes(x=lubridate::floor_date(date,“month”),y=pageviews,group=1,color=deviceCategory))+geom_col()+theme_bw()+labs(x=“date”,y=“pageviews by Device Category”)+labs(title=“Andy D)用户(by Device Category)”,x=“date”,y=“总页面浏览量”)+ylim(0,NA)
错误在
[.默认值
(object,name,exact=TRUE):上个月下标超出边界
last_month <- Sys.Date() - 0:29
df <- data.frame(
  date = last_month,
  price = runif(30)
 )

ggplot(df, aes(date, price)) +
  geom_line() + 
  scale_x_date(date_labels = "%Y-%m")