ggplot统一图像大小?

时间:2015-02-02 15:00:34

标签: r graphics ggplot2

我想创建在最终保存的图像中主网格线之间具有恒定距离的图。

我试图做的是:(a)将上下边距设置为每个1厘米,然后(b)为主网格线之间的每个“方形”添加0.75厘米。

使用这种方法,我的绘图在主要网格线之间没有恒定的距离。

enter image description here

下面是一个最小的例子。

我希望有人可以帮我解决这个问题。

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

1 个答案:

答案 0 :(得分:0)

如果发现应用set_panel_size()有效: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')