在ggplot中删除一些x轴值 [英] Dropping some x-axis values in ggplot

查看:343
本文介绍了在ggplot中删除一些x轴值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框称为Finalcombined看起来像这样:

I有以下图表:





以下代码:

  Labourproductivity<  -  ggplot(Finalcombined ,aes(x = quarter,y = LabourProductivity,group = 1))+ geom_line(color =black,size = 0.5)+ 
labs(x =Time,y =(Labor Productivity)) )+
主题(axis.text.x = element_text(angle = 90,hjust = 1,))+
geom_point(color =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(color =white,size = 0.50),panel.grid.minor = element_line(color =white,size = 0.16))
Labourproductivity

我的问题是如何摆脱在x轴上显示的某些值仍然具有相同的图形。我只想列入(2004年第1季度,2005年第1季度,2006年第1季度)等。我会怎么做呢?感谢您的帮助!

解决方案

将以下内容添加到您的 ggplot / p>

  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))
FinalCompined < -
data.frame(quarter = Q,
LabourProductivity = runif(length(Q)))

#plot
ggplot(Finalcombined,
aes(x = quarter,
y = LabourProductivity,
group = 1))+
geom_line(color =black,size = 0.5)+
labs( x =Time,y =(Labor Productivity))+
theme(axis.text.x = element_text(angle = 90,hjust = 1,))+
geom_point(color =黑色,size = 2,shape = 16)+
geom_smooth(aes(group = 1))+
主题(axis.text = element_text(size = 13),axis.title = element_text(size = 16,face =bold))+
主题(panel.grid.major = element_line(color =white,size = 0.50),panel.grid.minor = element_line(color =white,size = 0.16))+
#改变X轴标签
scale_x_discrete(break = Finalcombined $ quarter [grepl(Q1,Finalcombined $ quarter)])


My dataframe called Finalcombined looks like this: I have the following graph:

And the following code:

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

My question is how can get rid of some of the values indicated on the x-axis but still have the same graph. I would only like to include (2004 Q1, 2005 Q1, 2006 Q1) and so on. How would I go about this? Thank you for your help!

解决方案

Add below to your ggplot

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

Example:

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

这篇关于在ggplot中删除一些x轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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