减少刻度和x-y轴之间的距离@ggplot

时间:2018-05-03 22:46:30

标签: r ggplot2 bar-chart

我有一个数据框

library(ggplot2)
library(latex2exp)
dfvi<-structure(list(rel.imp = c(3.68859054679465, 7.97309042736285, 
0.461768686793877, -0.672404901177933, 0.257999910761084, -0.56914400358685, 
0.331273538653149, -0.226891321033094, 0.179124365066449, -0.393707520847751
), x.names = structure(c(9L, 10L, 8L, 1L, 6L, 2L, 7L, 4L, 5L, 
3L), .Label = c("meandd", "uniquedd", "meanrd", "maxrd", "minrd", 
"sdd", "modedd", "mindd", "co1", "co2"), class = "factor"), logrelimp = c(1.54513201422058, 2.19423014598306, 0.379647131842709, -0.514262651121387, 0.229523087341111, -0.450530250028075, 0.286136031936669, -0.204483588890798, 0.164772099509159, 
-0.331967477446303)), .Names = c("rel.imp", "x.names", "logrelimp"), row.names = c(NA, -10L), class = "data.frame")

使用ggplot2

ggplot(dfvi, aes(x=x.names, y=logrelimp)) +
 geom_segment( aes(x=x.names, xend=x.names, y=0, yend=logrelimp),color="black",size=2) +
 geom_point( color="darkred", size=3) +    scale_y_continuous(breaks=c(-1,seq(0,2,1)),limits=c(-1,2.5),expand=c(0.5,0.0))+scale_x_discrete(labels=c('co2'='co2','co1'='co1',
                        'mindd'=parse(text=TeX('mindd')),
                        'meandd'=parse(text=TeX('$meandd$')),
                        'sdd'=parse(text=TeX('$sdd$')),
                        'uniquedd'=parse(text=TeX('$u_l$')),
                        'modedd'=parse(text=TeX('$modedd$')),
                        'maxrd'=parse(text=TeX('$maxrd$')),
                        'minrd'=parse(text=TeX('$minrd$')),
                        'meanrd'=parse(text=TeX('$meanrd$'))),expand=c(0.5,0.0))+
geom_hline(yintercept=0, linetype="dashed", color = "black")+
expand_limits(x=0,y=0)+
theme(
axis.text.x = element_text(size=20,angle=90,hjust=1),
#panel.grid.major.x = element_blank(),
#panel.grid.major.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
axis.ticks.x = element_blank(),
axis.text = element_text(colour = "black"),
#axis.text.x = element_text(size = 20),
axis.text.y = element_text(size = 20),
axis.title = element_text(size = 20),
strip.text.x = element_text(size = 20),
axis.line.y = element_line(color="black"),
axis.line.x = element_line(color="black")) + 
xlab("x labels") +
ylab("y label")

生成以下图表:

enter image description here

我希望缩小第一个x标签和y轴的开头之间的空间,并在最后一个x标签后结束图表的右侧部分。

另外,我需要以-1开始x轴(避免空格)。

最后,如何缩短y - 轴线?我想要xy轴,只要它们的限制启用,不超过它。

我尝试在expandscale_y_continuous内玩scale_x_discrete,但它没有这样做。没有使用任何expand我在嘀嗒声之间得到了很大的距离,这是我不想要的。

总之,我希望距离如图所示,但是解决了图的右端和左端的问题。此外,xy轴应该只要其限制启用即可。

1 个答案:

答案 0 :(得分:3)

实际上,您既不需要latex2expexpand_limitsexpand

一些说明:

  1. "uniquedd"更改为"u[l]" - 然后您不需要latex2exp;
  2. breaks;
  3. 中删除expandscale_y_continous
  4. scale_x_discrete中仅使用scale_x_discrete(labels = parse(text = levels(dfvi$x.names)));
  5. theme_classic();
  6. 之前添加theme
  7. theme中,只需axis.text.x = element_text(angle = 90, hjust = 1)axis.ticks.x = element_blank()
  8. 更新情节 plot link

    更新代码

    library(ggplot2)
    
    dfvi <- structure(
      list(
        x.names = structure(c(9L, 10L, 8L, 1L, 6L, 2L, 7L, 4L, 5L, 3L),
                            .Label = c("meandd", "u[l]", "meanrd",
                                       "maxrd", "minrd", "sdd", "modedd",
                                       "mindd", "co1", "co2"),
                            class = "factor"),
        rel.imp = c(3.68859054679465, 7.97309042736285, 0.461768686793877,
                    -0.672404901177933, 0.257999910761084, -0.56914400358685,
                    0.331273538653149, -0.226891321033094, 0.179124365066449,
                    -0.393707520847751),
        logrelimp = c(1.54513201422058, 2.19423014598306, 0.379647131842709,
                      -0.514262651121387, 0.229523087341111, -0.450530250028075,
                      0.286136031936669, -0.204483588890798, 0.164772099509159,
                      -0.331967477446303)
      ),
      .Names = c("x.names", "rel.imp", "logrelimp"),
      row.names = c(NA, -10L), class = "data.frame"
    )
    
    ggplot(dfvi, aes(x = x.names, y = logrelimp)) +
      geom_segment( aes(x=x.names, xend = x.names, y = 0, yend = logrelimp), 
                    color = "black", size = 2) +
      geom_point( color = "darkred", size = 3) +
      scale_y_continuous(limits = c(-1, 2.5)) +
      scale_x_discrete(labels = parse(text = levels(dfvi$x.names))) +
      geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
      theme_classic() +
      theme(
      axis.text.x = element_text(angle = 90, hjust = 1),
      axis.ticks.x = element_blank()) + 
      xlab("x labels") +
      ylab("y label")
    
相关问题