nhern121
nhern121

Reputation: 3919

reduce distance between ticks and x-y axis @ggplot

I have a data frame

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

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

which produces the following graph:

enter image description here

I want to reduce the space between the first x label and the start of the y axis, and end the right part of the graph after the last x label.

Also, I need to start my x axis at -1 (avoid white space).

Finally, how can I shorten the y-axis line? I want both x and y axis as long as their limits enable, no more than that.

I tried playing with expand inside scale_y_continuous and scale_x_discrete but it is not doing it. Without using any expand I am getting a large distance between ticks which I don't want.

In summary, I want the distance to be as shown in the graph, but solving the problems on the right and left end of the plot. Also, both x and y axis should be as long as their limits enable.

Upvotes: 0

Views: 1840

Answers (1)

lcgodoy
lcgodoy

Reputation: 793

Actually, you don't need neither latex2exp, expand_limits and expand.

Some instructions:

  1. change "uniquedd" to "u[l]" - then you don't need latex2exp;
  2. remove breaks and expand from scale_y_continous;
  3. In scale_x_discrete use just scale_x_discrete(labels = parse(text = levels(dfvi$x.names)));
  4. add theme_classic() before theme;
  5. In theme, keep just axis.text.x = element_text(angle = 90, hjust = 1) and axis.ticks.x = element_blank()

Updated plot plot link

Updated Code

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

Upvotes: 3

Related Questions