在 ggplot 中删除一些 x 轴值

Dropping some x-axis values in ggplot

我名为 Finalcombined 的数据框如下所示: 我有下图:

以及以下代码:

Labourproductivity<- ggplot(Finalcombined, aes(x = quarter, y = LabourProductivity, group=1))+geom_line(colour="black", size=0.5) +
  labs(x="Time", y=("Labour Productivity"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, ))+
  geom_point(colour="black", size=2, shape=16)+
  geom_smooth(aes(group=1))+
  theme(axis.text=element_text(size=13),axis.title=element_text(size=16,face="bold"))+
  theme(panel.grid.major = element_line(colour = "white", size=0.50),panel.grid.minor = element_line(colour = "white", size=0.16))
Labourproductivity

我的问题是如何去掉 x 轴上指示的一些值但仍然具有相同的图形。我只想包括 (2004 Q1, 2005 Q1, 2006 Q1) 等等。我该怎么做?感谢您的帮助!

将以下内容添加到您的 ggplot

scale_x_discrete(breaks=Finalcombined$quarter[grepl("Q1",Finalcombined$quarter)])

示例:

require("ggplot2")

#dummy data
Q <- paste(sort(rep(2004:2013,4)),paste0("Q",1:4))
Finalcombined <-
  data.frame(quarter= Q,
             LabourProductivity=runif(length(Q)))

#plot
ggplot(Finalcombined,
       aes(x = quarter,
           y = LabourProductivity,
           group=1)) + 
  geom_line(colour="black", size=0.5) +
  labs(x="Time", y=("Labour Productivity")) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, )) +
  geom_point(colour="black", size=2, shape=16) +
  geom_smooth(aes(group=1)) +
  theme(axis.text=element_text(size=13),axis.title=element_text(size=16,face="bold")) +
  theme(panel.grid.major = element_line(colour = "white", size=0.50),panel.grid.minor = element_line(colour = "white", size=0.16)) +
  #change X axis labels
  scale_x_discrete(breaks=Finalcombined$quarter[grepl("Q1",Finalcombined$quarter)])