我如何控制ggplot中的画布大小? [英] How can I control the canvas size in ggplot?

查看:2922
本文介绍了我如何控制ggplot中的画布大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的ggplot:



我在它上面画了一个黑色的边缘,所以想象它有多大是比较容易的。



我不明白为什么这个情节如此之大。我的代码中没有任何内容告诉ggplot我想要任何区域上方或下方的情节。

这似乎让我每次都感到满意。如何控制ggplot中的画布大小?

  library(ggplot2)

hex = c( #CC0000,#90BD31,#178CCB)

q < - ggplot(data = NULL)
q < - q + geom_rect(data = NULL,aes(xmin = xmax = 1,ymin = 0.5,ymax = 1.5),fill = hex [1])$ ​​b $ bq <-q + geom_rect(data = NULL,aes(xmin = 1.5,xmax = 2.5,ymin = (数据= NULL,aes(xmin = 3,xmax = 4,ymin = 0.5,ymax = 1.5),fill = 0.5,ymax = 1.5),fill = hex [2])bbqq- (b)<-q +注释(text,x = .5,y = 0.1,label ='Impaired',size = 4) text,x = 2,y = 0.1,label ='Normal',size = 4)
q <-q + annotate(text,x = 3.5,y = 0.1,label ='Optimal' ,size = 4)
q <-q + coord_fixed()
q <-q + theme_classic()
q <-q + theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank())
q


解决方案

使用 ggplot2 plot.margin和/或panel.margin主题选项你应该能够控制内部边距,一旦你设置好了画布的输出尺寸。这肯定适用于普通的图形设备,也应该在闪亮的应用程序中工作:

  library(ggplot2)
library (grid)

hex = c(#CC0000,#90BD31,#178CCB)

q < - ggplot(data = NULL)
q <-q + geom_rect(data = NULL,aes(xmin = 0,xmax = 1,ymin = 0.5,ymax = 1.5),fill = hex [1])$ ​​b $ bq <-q + geom_rect = NULL,aes(xmin = 1.5,xmax = 2.5,ymin = 0.5,ymax = 1.5),fill = hex [2])
q < - q + geom_rect(data = NULL,aes(xmin = xmax = 4,ymin = 0.5,ymax = 1.5),fill = hex [3])
q <-q + annotate(text,x = .5,y = 0.1,label ='Impaired' (text),x = 2,y = 0.1,label ='Normal',size = 4)
q <-q + annotate(text ),x = 3.5,y = 0.1,label ='Optimal',size = 4)
q <-q + coord_fixed()
q <-q + theme_classic()
q< - q + theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.te xt.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank(),
plot.margin =单位(c(0,0,0,0),cm),
panel.margin =单位(c 0,0,0,0)cm))
q


Consider this simple ggplot:

I drew a black edge on it, so it's easier to envision how large it is.

I don't understand why this plot is so large. There is nothing in my code telling ggplot that I want any area above or beneath the plot.

This seems to get me every time. How can I control the canvas size in ggplot?

library(ggplot2)

hex=c("#CC0000", "#90BD31", "#178CCB")    

q <- ggplot(data=NULL)
    q <- q + geom_rect(data=NULL, aes(xmin=0, xmax=1, ymin=0.5, ymax=1.5), fill=hex[1])
    q <- q + geom_rect(data=NULL, aes(xmin=1.5, xmax=2.5, ymin=0.5, ymax=1.5), fill=hex[2])
    q <- q + geom_rect(data=NULL, aes(xmin=3, xmax=4, ymin=0.5, ymax=1.5), fill=hex[3])
    q <- q + annotate("text", x=.5, y=0.1, label='Impaired', size=4)
    q <- q + annotate("text", x=2, y=0.1, label='Normal', size=4)
    q <- q + annotate("text", x=3.5, y=0.1, label='Optimal', size=4)
    q <- q + coord_fixed()
    q <- q + theme_classic()
    q <- q + theme(axis.line=element_blank(),
                   axis.text.x=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks=element_blank(),
                   axis.title.x=element_blank(),
                   axis.title.y=element_blank(),
                   panel.background=element_blank(),
                   panel.border=element_blank(),
                   panel.grid.major=element_blank(),
                   panel.grid.minor=element_blank(),
                   plot.background=element_blank())
    q

解决方案

Using the ggplot2 plot.margin and/or panel.margin theme options you should be able to control the internal margins, once you've adequately set the output dimensions of the canvas. This certainly will work for normal graphics devices and should also work within the shiny application:

library(ggplot2)
library(grid)

hex=c("#CC0000", "#90BD31", "#178CCB")    

q <- ggplot(data=NULL)
    q <- q + geom_rect(data=NULL, aes(xmin=0, xmax=1, ymin=0.5, ymax=1.5), fill=hex[1])
    q <- q + geom_rect(data=NULL, aes(xmin=1.5, xmax=2.5, ymin=0.5, ymax=1.5), fill=hex[2])
    q <- q + geom_rect(data=NULL, aes(xmin=3, xmax=4, ymin=0.5, ymax=1.5), fill=hex[3])
    q <- q + annotate("text", x=.5, y=0.1, label='Impaired', size=4)
    q <- q + annotate("text", x=2, y=0.1, label='Normal', size=4)
    q <- q + annotate("text", x=3.5, y=0.1, label='Optimal', size=4)
    q <- q + coord_fixed()
    q <- q + theme_classic()
    q <- q + theme(axis.line=element_blank(),
                   axis.text.x=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks=element_blank(),
                   axis.title.x=element_blank(),
                   axis.title.y=element_blank(),
                   panel.background=element_blank(),
                   panel.border=element_blank(),
                   panel.grid.major=element_blank(),
                   panel.grid.minor=element_blank(),
                   plot.background=element_blank(),
                   plot.margin=unit(c(0,0,0,0), "cm"),
                   panel.margin=unit(c(0,0,0,0), "cm"))
    q

这篇关于我如何控制ggplot中的画布大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆