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.
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')