2

I would like to have the title not be chopped off and have the axis labels removed from this chart that I generated with plot_grid from cowplot.enter image description here

Here is my code

    data(mtcars)
 library(ggplot2)
 library(cowplot)
 mpg = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
 coord_flip()
 am=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
 coord_flip()
 vs=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
 coord_flip()
 gear = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
 coord_flip()
 p=plot_grid(mpg,am,vs,gear, labels = "Variables effecting Mileage", label_size = 14, hjust = -0.5,
 + vjust = 0.5)+theme_grey()
p

Also, if it would be simpler to create this without cowplot, what do you suggest?

FTF
  • 181
  • 3
  • 11

3 Answers3

5

Here is a cowplot only answer. It might be more what you want.

library(ggplot2)
library(cowplot)
library(gtable)

data(mtcars)

mpg = ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
  coord_flip() + labs(x="",y="")
am=ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
vs=ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
gear = ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
p=plot_grid(mpg,am,vs,gear) +
  theme_grey() +

# Use annotation text as it is centered at the x,y point
  annotate("text",x=0.5,y=1.04,size=7,label="Variables affecting Mileage") +

# Add some space around the edges  
  theme(plot.margin = unit(c(1,0.5,0.5,0.5), "cm")) 


# Suppress tick marks
p=p+scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)

# Have to turn off clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"

# need to draw it with the new clip settings
grid.draw(gt)

Yields:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • I had to add library(gtable) in order to call grid.draw and then I was able draw other themes from ggthemes - so that was nice. But your solution did not remove the rug ticks on the x and y axes. – FTF Dec 26 '15 at 14:51
  • 1
    So is this an okay solution? Not really sure what a "rug tick" is... First time I have heard of them. I just fixed the `library(gtable)` – Mike Wise Dec 26 '15 at 14:55
  • 1
    We have put a fair amount of work into this upvotes are appreciated. – Mike Wise Dec 26 '15 at 15:29
  • 1
    Used annotation text as it is centered. – Mike Wise Dec 26 '15 at 16:39
  • 1
    There might be a better way to get rid of the background grid, but I have to go see a movie now :). Please mark correct if you like it. – Mike Wise Dec 26 '15 at 16:40
  • Perhaps simpler to suppress both the tick marks and the text use: `theme(axis.ticks=element_blank())+theme(axis.text=element_blank())` – FTF Dec 26 '15 at 19:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99029/discussion-between-ftf-and-mike-wise). – FTF Dec 26 '15 at 22:43
  • It is after midnight for me now, just got back from seeing Star Wars. Let's try again tomorrow. I am in Timezone GMT+1 (Germany). – Mike Wise Dec 26 '15 at 23:33
2

would this workout for you?

library(ggplot2)
library(gridExtra)
a <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) + coord_flip()  + theme_grey() + theme(axis.title.x = element_blank()) 
b <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) + coord_flip()  + theme_grey() + theme(axis.title.x = element_blank())
c <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
d <-  ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank()) 
grid.arrange(a, b, c, d, ncol=2, top = "Variables effecting Mileage")

enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Are you implying that the only way to change the theme is within the individual plot? Also, can you explain why some of the ggthemes such as theme_economist do not work within the individual plots while others such as theme_wsj do? – FTF Dec 26 '15 at 02:55
  • 1
    you did not mention theme in your post. some of the themes might not work but you could do it manually anyway. my answer is just an alternative to your question. But if you really want a plot using cowplot, see@Mike Wise answer :) – MLavoie Dec 26 '15 at 11:15
1

Thanks Mike. This does the job.

 data(mtcars)
 library(ggplot2)
 library(cowplot)
 library(gtable)
 mpg = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
 coord_flip() + labs(x="",y="")
 am=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
 coord_flip()+ labs(x="",y="")
 vs=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
 coord_flip()+ labs(x="",y="")
 gear = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
 coord_flip()+ labs(x="",y="")
 p=plot_grid(mpg,am,vs,gear, 
        labels = "Variables effecting Mileage", 
        label_size = 14, hjust = -1.3[![enter image description here][1]][1], vjust = -0.1)+
 theme_grey() +

 # Add some space around the edges  
 theme(plot.margin = unit(c(1,0.5,0.5,0.5), "cm")) 

 # Suppress tick marks
 p=p+scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)

# Have to turn off clipping
 gt <- ggplot_gtable(ggplot_build(p))
 gt$layout$clip[gt$layout$name == "panel"] <- "off"

# need to draw it with the new clip settings
 grid.draw(gt)

Here is my plot Solution

FTF
  • 181
  • 3
  • 11
  • 1
    Oh, that. I would have called it the "background grid". Any chance I can add it to my solution and get the correct? Can give you a tip for your troubles :). – Mike Wise Dec 26 '15 at 15:58
  • Please do and FWIW if you have any hints on centering the title with ggplot2_english_first_edition.pdf, that would be cool too. Really appreciate the time that you invested. – FTF Dec 26 '15 at 16:21
  • 1
    Ok, did it, but didn't get the pdf reference. – Mike Wise Dec 26 '15 at 16:39