1

I would like to create plots which have a constant distance between major grid lines in the final saved image.

What I tried to do was: (a) set the upper and lower plot margins at 1cm each, and then (b) add 0.75 cm for each “square” between major gridlines.

Using this approach, my plots do not have a constant distance between major gridlines.

enter image description here

A minimal example is below.

I was hoping someone could please help me solve this problem.

library(ggplot2)
library(gridExtra)  #to set up plot grid

num_points<-4
dat_pop<-data.frame(
  est=runif(num_points,0,6),
  ord=c(1,2)
)
dat_pop$varname<-c('Cobalt','Chromium')

margin_height<-1
border_total<-margin_height*2
num_square<-num_points+1
image_height<-0.75*num_square+border_total

dat_pop$grpN<-seq(1,num_points,1)

xstart=-1*(num_points+1)
xend=0

ThemeMain<-theme(legend.position = "none", 
                plot.margin = unit(c(margin_height,0.25,margin_height,0.5),"cm"),
                 panel.margin = unit(c(0,0,0,0),"cm"), 
                 axis.ticks.y = element_blank(),
                 axis.text.y = element_blank(),
                 panel.grid.minor.y = element_blank()
)

#######################################################################################################
#MAIN PLOT
#######################################################################################################
p<-
  ggplot(dat_pop, aes(x=-grpN,y=est)) +
  scale_y_continuous(name=NULL, limits=c(0, 6), expand=c(0,0)) + 
  geom_point(aes(shape="1")) +
  coord_flip() +
  scale_x_continuous(limits=c(xstart,xend), breaks=seq(xstart,xend),expand=c(0,0))+xlab(NULL)+
  ThemeMain

ggsave(file="plot.pdf",p,width=21.59,height=image_height,units='cm')
user1375871
  • 1,199
  • 1
  • 12
  • 23
  • 1
    You may have more luck getting an answer if you provide a *minimal* reproducible example. – shadow Feb 02 '15 at 15:12
  • maybe this helps: http://stackoverflow.com/questions/24188986/r-ggplot2-make-two-geom-tile-plots-have-equal-height/24189239#24189239 – baptiste Feb 02 '15 at 18:31

1 Answers1

0

If found applying set_panel_size() works: R, ggplot2, size of plot area

library(ggplot2)
library(gridExtra)  #to set up plot grid
set_panel_size <- function(p=NULL, g=ggplotGrob(p), width=unit(3, "cm"), height=unit(3, "cm")){
  panel_index_w<- g$layout$l[g$layout$name=="panel"]
  panel_index_h<- g$layout$t[g$layout$name=="panel"]
  g$widths[[panel_index_w]] <- width
  g$heights[[panel_index_h]] <- height
  class(g) <- c("fixed", class(g), "ggplot")
  g
}

num_points<-4
dat_pop<-data.frame(
  est=runif(num_points,0,6),
  ord=c(1,2)
)
dat_pop$varname<-c('Cobalt','Chromium')

margin_height<-1
border_total<-margin_height*2
num_square<-num_points+1
image_height<-0.75*num_square

dat_pop$grpN<-seq(1,num_points,1)

xstart=-1*(num_points+1)
xend=0

ThemeMain<-theme(legend.position = "none", 
                plot.margin = unit(c(margin_height,0.25,margin_height,0.5),"cm"),
                 panel.margin = unit(c(0,0,0,0),"cm"), 
                 axis.ticks.y = element_blank(),
                 axis.text.y = element_blank(),
                 panel.grid.minor.y = element_blank()
)

#######################################################################################################
#MAIN PLOT
#######################################################################################################
p<-
  ggplot(dat_pop, aes(x=-grpN,y=est)) +
  scale_y_continuous(name=NULL, limits=c(0, 6), expand=c(0,0)) + 
  geom_point(aes(shape="1")) +
  coord_flip() +
  scale_x_continuous(limits=c(xstart,xend), breaks=seq(xstart,xend),expand=c(0,0))+xlab(NULL)+
  ThemeMain

p2<-set_panel_size(p, height=unit(image_height, "cm"),width=unit(21.6, "cm"))

ggsave(file="plot.pdf",p2,width=21.6,units='cm')
Community
  • 1
  • 1
user1375871
  • 1,199
  • 1
  • 12
  • 23