Gurkenhals
Gurkenhals

Reputation: 235

Spacing between y-axis and box and between boxes

I have some trouble creating a box plot.

1) How do I reducing the spacing between the y-axis and the first box and between the two boxes?

2) How get I the y-axis-title bold? There seems to be a problem with the superscript.

3) How can I create simple squares indicating the group using the specific color for my legend?

I am looking forward to your replies!

library(ggplot2)
d <- data.frame(IVCe.bin=factor(c(1,1,1,1,1,1,1, 0,0,0,0,0,0,0)),
                eGFR.MDRD=c(90,95,98,94,88,85,100, 80, 60,66,62,69,72,78))

g <- ggplot(data=d, aes(x=IVCe.bin, y=eGFR.MDRD, group=IVCe.bin, fill=IVCe.bin)) +
     stat_boxplot(geom ='errorbar', width=0.1, lwd=1) +
     geom_boxplot(lwd=1, width=0.25) +
     theme_bw() +

     theme(legend.position="top") + 
     theme( panel.grid.major = element_blank() ) +
     theme( panel.grid.minor = element_blank() ) +
     theme( panel.border = element_blank()) +
     theme(axis.text.x=element_blank(), axis.ticks.x = element_blank(), axis.line.x = element_blank(), axis.title.x=element_blank()) +

     ylab(~"eGFR [ml/min/1.73m"^2) +
     theme(axis.ticks.y = element_line(color='black', size=1)) +
     theme(axis.line.y = element_line(color='black', size=1)) +
     theme(axis.text.y=element_text(size=15)) +
     theme(axis.title.y=element_text(size=15, hjust=0.5, vjust=10, face="bold")) +

     theme(axis.ticks.length = unit(.25, "cm")) # Länge der Ticks
print(g)

(g <- g + scale_fill_discrete(breaks=c(0, 1), labels=c("Control", "Intervention")))
(g <- g + labs(fill="")) # Keine Legendentitel
(g <- g + theme(legend.text = element_text(colour="black", size = 15, face = "plain")))
(g <- g + theme(legend.key = element_blank()))

Upvotes: 2

Views: 1389

Answers (1)

erc
erc

Reputation: 10123

  1. I think you can't. However, you can increase the width of the boxes (e.g. width=1) and then reduce the width of the plot when saving, which will create the same effect.

  2. Use bquote(bold()) in ylab (btw. I think you're missing a ])

  3. Use guides() with override.aes()

So here's the code:

g <- ggplot(data=d, aes(x=IVCe.bin, y=eGFR.MDRD, group=IVCe.bin, fill=IVCe.bin)) +
  stat_boxplot(geom ='errorbar', width=0.1, lwd=1) +
  geom_boxplot(lwd=1, width=1) +    # change width of boxes (1)
  theme_bw() +
  theme(legend.position="top") + 
  theme( panel.grid.major = element_blank() ) +
  theme( panel.grid.minor = element_blank() ) +
  theme( panel.border = element_blank()) +
  theme(axis.text.x=element_blank(), axis.ticks.x = element_blank(), axis.line.x = element_blank(), axis.title.x=element_blank()) +
  
  ylab(bquote(bold(~"eGFR [ml/min/1.73m"^2))) +    # turn axis title bold (2)
  theme(axis.ticks.y = element_line(color='black', size=1)) +
  theme(axis.line.y = element_line(color='black', size=1)) +
  theme(axis.text.y=element_text(size=15)) +
  theme(axis.title.y=element_text(size=15, hjust=0.5, vjust=10)) +
  
  theme(axis.ticks.length = unit(.25, "cm")) # Länge der Ticks
print(g)

(g <- g + scale_fill_discrete(breaks=c(0, 1), labels=c("Control", "Intervention")))
(g <- g + labs(fill="")) # Keine Legendentitel
(g <- g + theme(legend.text = element_text(colour="black", size = 15, face = "plain")))
(g <- g + theme(legend.key = element_blank()))

g + guides(fill=guide_legend(override.aes=list(colour=c(NA,NA), 
                                               shape=c(NA,NA))))  # change legend key symbol (3)

enter image description here

btw. I think people on SO prefer that you don't ask several questions in one.

Upvotes: 4

Related Questions