更改ggplot2中x轴刻度之间的距离 [英] Change distance between x-axis ticks in ggplot2

查看:3894
本文介绍了更改ggplot2中x轴刻度之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在用三个观测值生成一个线图。因此,有三个x轴刻度。

我想手动减小x轴刻度之间的距离,并基本上迫使观察值彼此更接近。换句话说,我想减少x轴蜱之间的距离。

我的数据:

  structure(list(Period = c(1997年1月 -  2003年8月,2003年9月 -  2005年6月,
2005年7月 - 2009年12月, (1L,3L,2L,
1L,3L,2L),标签= c(1997年1月 - 2003年8月,2005年7月 - 2009年12月,
2003年9月 - 2005年6月),class =factor),Variable = structure(c(2L,
2L,2L,1L,1L,1L),.Label = c(显着,Zscore),class =factor),
Score = c(8.798129,4.276128,7.280275,1.64, 1.64,1.64)),.Names = c(Period,
Time.Period,Variable,Score),class =data.frame,row.names = c(NA,
-6L))

我的代码:


$ b )$ b

  ggplot(glomor,aes(x = Time.Period,y = Score,group = Variable,shape = Variable,color = Variable,width = 0.5))+ 
geom_point()+
scale_x_d iscrete(limits = c(1997年1月 - 2003年8月,2003年9月 - 2005年6月,2005年7月 - 2009年12月),expand = c(.08,0))+
geom_line(aes linetype = Variable),size = 1.3)+
geom_point(size = 4.4)+
scale_shape_discrete(name =,label = c(Signficant Z-Score,Moran's I Z-Score ))+
scale_linetype_manual(values = c(1,3),name =,label = c(Signficant Z-Score,Moran's I Z-Score))+
scale_color_manual值= C(黑色,灰色40),名称=,标签= C(重要Z-分数,莫兰的I Z-分数))+
scale_fill_discrete(name =, label = c(Signficant Z-Score,Moran's I Z-Score))+
theme_classic()+
ylim(0,10)+
xlab(Time Periods )+
ylab(Moran's I Z-Score)+
theme(axis.text.x = element_text(size = 11))+
theme(axis.title.x = element_text (size = 14,face =bold))+
theme(axis.title.y = element_text(size = 14,face =bold))+
theme(legend.position = c (.80,.90))+
theme(legend.text = element_text(size = 12))+
theme(legend.background = element_rec t(fill =white,linetype =dashed))+
theme(legend.key = element_blank())


解决方案

增加scale_x_discrete的expand =参数的第一个参数:

  ggplot(glomor,aes(x = Time.Period,y = Score,group = Variable,shape = Variable,
color = Variable,width = 0.5))+
geom_point() +
scale_x_discrete(limits = c(1997年1月 - 2003年8月,
2003年9月 - 2005年6月,2005年7月 - 2009年12月),expand = c(0.2,0)
geom_line(aes(linetype = Variable),size = 1.3)+
geom_point(size = 4.4)+
scale_shape_discrete(name =,labels = c(显着Z分数 ,Moran's I Z-Score))+
scale_linetype_manual(values = c(1,3),name =,
labels = c(显着Z分数,Moran's I Z - Score))+
scale_color_manual(values = c(black,grey40),name =,
labels = c(显着Z分数,Moran's I Z- Score))+
scal e_fill_discrete(name =,labels = c(显着Z分数,Moran's I Z分数))+
theme_classic()+
ylim(0,10)+
blab(Time Periods)+
ylab(Moran's I Z-Score)+
主题(axis.text.x = element_text(size = 11))+
theme( axis.title.x = element_text(size = 14,face =bold))+
theme(axis.title.y = element_text(size = 14,face =bold))+
主题(legend.position = c(.80,.90))+
主题(legend.text = element_text(size = 12))+
主题(legend.background = element_rect(fill =white ,linetype =dashed))+
theme(legend.key = element_blank())


Right now I am producing a line graph with three observations. Hence, there are three x-axis ticks.

I want to manually reduce the distance between the x-axis ticks and basically force the observations to be closer to each other. In other words, I want to reduce the distance between the x-axis ticks.

My data:

structure(list(Period = c("January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009", "January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009"), Time.Period = structure(c(1L, 3L, 2L, 
1L, 3L, 2L), .Label = c("Jan 1997 - Aug 2003", "Jul 2005 - Dec 2009", 
"Sep 2003 - Jun 2005"), class = "factor"), Variable = structure(c(2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("Significant", "Zscore"), class = "factor"), 
Score = c(8.798129, 4.267268, 7.280275, 1.64, 1.64, 1.64)), .Names = c("Period", 
"Time.Period", "Variable", "Score"), class = "data.frame", row.names = c(NA, 
-6L))

My code:

ggplot(glomor, aes(x=Time.Period, y=Score, group=Variable, shape=Variable, color=Variable,        width=0.5)) +
geom_point() +
scale_x_discrete(limits=c("Jan 1997 - Aug 2003","Sep 2003 - Jun 2005","Jul 2005 - Dec 2009"),   expand=c(.08,0)) +
geom_line(aes(linetype=Variable), size=1.3) +
geom_point(size=4.4) +
scale_shape_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +
scale_linetype_manual(values=c(1,3), name="", label=c("Signficant Z-Score", "Moran's I Z-Score"))   +
scale_color_manual(values=c("black", "grey40"), name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +
scale_fill_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +
theme_classic()+
ylim(0,10) +
xlab("Time Periods") +
ylab("Moran's I Z-Score") +
theme(axis.text.x=element_text(size=11)) +
theme(axis.title.x=element_text(size=14, face="bold")) +
theme(axis.title.y=element_text(size=14, face="bold")) +
theme(legend.position=c(.80, .90)) +
theme(legend.text=element_text(size=12)) +
theme(legend.background = element_rect(fill="white", linetype="dashed")) +
theme(legend.key = element_blank()) 

解决方案

Increase the first argument of the expand = argument of scale_x_discrete:

ggplot(glomor, aes(x=Time.Period, y=Score, group=Variable, shape=Variable,     
                   color=Variable, width=0.5)) +
geom_point() +
scale_x_discrete(limits = c("Jan 1997 - Aug 2003",
     "Sep 2003 - Jun 2005","Jul 2005 - Dec 2009"), expand=c(0.2, 0)) +
geom_line(aes(linetype=Variable), size=1.3) +
geom_point(size=4.4) +
scale_shape_discrete(name="", labels=c("Significant Z-Score", "Moran's I Z-Score")) +
scale_linetype_manual(values=c(1, 3), name="", 
                      labels=c("Significant Z-Score", "Moran's I Z-Score"))   +
scale_color_manual(values=c("black", "grey40"), name="",
                   labels=c("Significant Z-Score", "Moran's I Z-Score")) +
scale_fill_discrete(name="", labels=c("Significant Z-Score", "Moran's I Z-Score")) +
theme_classic()+
ylim(0,10) +
xlab("Time Periods") +
ylab("Moran's I Z-Score") +
theme(axis.text.x=element_text(size=11)) +
theme(axis.title.x=element_text(size=14, face="bold")) +
theme(axis.title.y=element_text(size=14, face="bold")) +
theme(legend.position=c(.80, .90)) +
theme(legend.text=element_text(size=12)) +
theme(legend.background = element_rect(fill="white", linetype="dashed")) +
theme(legend.key = element_blank()) 

这篇关于更改ggplot2中x轴刻度之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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