【问题标题】:Change distance between x-axis ticks in ggplot2更改ggplot2中x轴刻度之间的距离
【发布时间】:2013-03-14 03:09:37
【问题描述】:

现在我正在制作一个包含三个观察值的折线图。因此,存在三个 x 轴刻度。

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

我的数据:

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

我的代码:

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

【问题讨论】:

    标签: r ggplot2 axis edit-distance


    【解决方案1】:

    增加 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("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()) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多