如何使用' par'操纵情节边距?

时间:2017-10-20 05:49:39

标签: r plot margin boxplot par

我正在策划一个箱形图,但很难获得我喜欢的外边距。我使用以下代码:

tiff("Boxplot.tif", height=10, width=10, units="in", res=300, compression="lzw");
boxplot(Betadisp, col=c("darkgreen", "blue", "red"), boxwex=0.4, outline=T,
        ylim=c(0, 0.46), xlim=c(0.75,2.25), names = c("OG", "LIL","HIL"), 
        cex.lab=1.5, ylab="Distance to centroid", xlab='x', las = 1, notch=F, axis=T, at=at.x)
text(1, 0.36, cex=1.8, bquote(paste("ab")))
text(1.5, 0.235, cex=1.8, bquote(paste("a")))
text(2, 0.39, cex=1.8, bquote(paste("b")))
text(2.2, 0.45, cex=1.8, bquote(paste("NRI")))
par(mai = par("mai")*.75, oma = c(0,0,0,0) + 0.1, mar = c(0,0,0,0) + 0.1)
dev.off()

我希望减少外边距,特别是顶边和右边。使用par()似乎没有任何改变。任何关于如何调整代码的建议都将非常感激。

该图如下所示:

enter image description here

> dput(PMPhyloSOBetadisp)
structure(list(eig = structure(4.15892930122748, .Names = "PCoA1"), 
    vectors = structure(c(0.187894881623694, -0.0462669264321831, 
    0.078270716100596, -0.0708373862452924, 0.0330432406942923, 
    0.00808151460353643, 0.189307120491109, 0.191686534306445, 
    0.279993576281652, 0.28571659518387, -0.292963094442439, 
    0.130307798194384, -0.108034670599994, -0.279412265592119, 
    -0.317212249248107, -0.201985621091697, 0.0132092286292795, 
    0.396467849399822, 0.0211368138831204, 0.30188342895443, 
    0.206873272795936, 0.138385483231689, 0.124786446212896, 
    -0.281430210350172, -0.0305849438602823, -0.00255636044610587, 
    0.177458408517268, -0.12231080332995, -0.281871351530724, 
    -0.152724144457444, 0.176186794362196, -0.155165330924759, 
    0.0320858194688813, -0.223473307376246, 0.127613754254968, 
    0.166962002449328, 0.158685218038405, 0.100008083268373, 
    -0.0940881894843899, 0.121856979176153, -0.140265149171084, 
    0.0613648415652045, -0.190745874749306, -0.00219528996378689, 
    -0.1430862908501, -0.26172388196876, -0.0727143694332558, 
    0.331841908277, 0.220415829691309, 0.123101542527033, 0.180278291830266, 
    0.215427947847535, 0.190756247591512, 0.165126573796417, 
    0.139890849869231, 0.0933688471441757, 0.0729113372604114, 
    0.19610126202653, -0.0522909285621633, -0.12468483037786, 
    -0.0921365696698343, 0.14270372033243, -0.0720884766538631, 
    0.0606904684333239, -0.15285729454955, -0.144265372972496, 
    -0.0887902944817819, -0.0159417136106468, -0.085279046400012, 
    -0.0691969457493266, 0.147813990005166, -0.2912450223718, 
    0.0852462637551566, -0.158567373346935, -0.100576512229591, 
    0.0057226914258182, -0.325235562525615, -0.0981544032193745, 
    0.0829599052753697, -0.0819102728840672, -0.100848420365388, 
    0.0284072979563995, -0.196807045981046, 0.0738952152156015, 
    0.0448146573968611, -0.0534264885898359, -0.117434245302843, 
    0.0826195634628877, 0.199034727378092, -0.0467567273886303, 
    0.0532651574812087, 0.00569859196113407, -0.120110959638929, 
    -0.0205367406051655, -0.104116321893903, -0.153130831169889, 
    -0.0666402710314291, 0.246990006965615, 0.0540941400034081, 
    -0.0185000753890068, 0.149547217533089, 0.090513237877524, 
    0.00387509228547038, 0.0229945137667384, 0.23535371290394, 
    0.33598374482058, 0.269258915330889, 0.00689306033364249, 
    0.00226287725342899, -0.186909704550723, 0.0841922617367422, 
    -0.119109532559289, -0.209836298377522, 0.145071133844632, 
    -0.332068338669535, -0.0975072972715105, -0.0443222416741678, 
    -0.0980041604145747, -0.219892017644063, 0.282116111212308, 
    0.128980339196711, -0.141574399557034, -0.0657539413882544, 
    -0.45247717974156, -0.183785397552269, 0.128383205531913, 
    -0.0101083206716934, -0.0178247844444956, -0.0133953258963639, 
    0.178986768698698, -0.138538624428617, 0.0432527997337566, 
    -0.603532150600179, -0.286840743894003, 0.331764689135947, 
    0.0383952829464595, 0.174327620102299, 0.050025120352419, 
    -0.00996422334957243), .Dim = c(139L, 1L), .Dimnames = list(
        c("H01", "H02", "H03", "H04", "H05", "H06", "H07", "H08", 
        "H09", "H10", "H11", "H12", "H13", "H14", "H15", "H16", 
        "H17", "H18", "H19", "H20", "H21", "H22", "H23", "H24", 
        "H25", "H26", "H27", "H28", "H29", "H30", "H31", "H32", 
        "H33", "H34", "H35", "H36", "H37", "H38", "H39", "H40", 
        "H41", "H42", "H43", "H44", "H45", "H46", "H47", "H48", 
        "H49", "L01", "L02", "L03", "L04", "L05", "L06", "L07", 
        "L08", "L09", "L10", "L11", "L12", "L13", "L14", "L15", 
        "L16", "L17", "L18", "L19", "L20", "L21", "L22", "L23", 
        "L24", "L25", "L26", "L27", "L28", "L29", "L30", "L31", 
        "L32", "L33", "L34", "L35", "L36", "L37", "L38", "L39", 
        "L40", "L41", "L42", "L43", "L44", "L45", "O01", "O02", 
        "O03", "O04", "O05", "O06", "O07", "O08", "O09", "O10", 
        "O11", "O12", "O13", "O14", "O15", "O16", "O17", "O18", 
        "O19", "O20", "O21", "O22", "O23", "O24", "O25", "O26", 
        "O27", "O28", "O29", "O30", "O31", "O32", "O33", "O34", 
        "O35", "O36", "O37", "O38", "O39", "O40", "O41", "O42", 
        "O43", "O44", "O45"), "PCoA1")), distances = structure(c(0.171774954519456, 
    0.0623868535364207, 0.0621507889963585, 0.0869573133495299, 
    0.0169233135900547, 0.0080384125007011, 0.173187193386872, 
    0.175566607202207, 0.263873649177415, 0.269596668079633, 
    0.309083021546677, 0.114187871090147, 0.124154597704232, 
    0.295532192696357, 0.333332176352344, 0.218105548195935, 
    0.00291069847495801, 0.380347922295584, 0.0050168867788829, 
    0.285763501850193, 0.190753345691698, 0.122265556127451, 
    0.108666519108659, 0.29755013745441, 0.0467048709645198, 
    0.0186762875503434, 0.16133848141303, 0.138430730434188, 
    0.297991278634962, 0.168844071561681, 0.160066867257958, 
    0.171285258028997, 0.0159658923646438, 0.239593234480484, 
    0.11149382715073, 0.150842075345091, 0.142565290934167, 0.0838881561641354, 
    0.110208116588627, 0.105737052071915, 0.156385076275321, 
    0.045244914460967, 0.206865801853543, 0.0183152170680244, 
    0.159206217954337, 0.277843809072997, 0.0888342965374933, 
    0.315721981172762, 0.204295902587072, 0.12797501136046, 0.185151760663692, 
    0.220301416680961, 0.195629716424938, 0.170000042629844, 
    0.144764318702658, 0.0982423159776023, 0.077784806093838, 
    0.200974730859957, 0.0474174597287366, 0.119811361544433, 
    0.0872631008364077, 0.147577189165856, 0.0672150078204365, 
    0.0655639372667505, 0.147983825716124, 0.13939190413907, 
    0.0839168256483553, 0.0110682447772202, 0.0804055775665854, 
    0.0643234769159, 0.152687458838593, 0.286371553538374, 0.0901197325885831, 
    0.153693904513509, 0.0957030433961639, 0.0105961602592448, 
    0.320362093692189, 0.0932809343859479, 0.0878333741087963, 
    0.0770368040506406, 0.0959749515319617, 0.0332807667898261, 
    0.191933577147619, 0.0787686840490281, 0.0496881262302877, 
    0.0485530197564093, 0.112560776469416, 0.0874930322963143, 
    0.203908196211518, 0.0418832585552037, 0.0581386263146353, 
    0.0105720607945607, 0.115237490805503, 0.0156632717717389, 
    0.0914369812138262, 0.140451490489812, 0.0539609303513526, 
    0.259669347645691, 0.0667734806834846, 0.0058207347089303, 
    0.162226558213165, 0.103192578557601, 0.0165544329655469, 
    0.0356738544468149, 0.248033053584017, 0.348663085500656, 
    0.281938256010966, 0.019572401013719, 0.0149422179335055, 
    0.174230363870647, 0.0968716024168187, 0.106430191879212, 
    0.197156957697445, 0.157750474524708, 0.319388997989459, 
    0.084827956591434, 0.0316429009940913, 0.0853248197344982, 
    0.207212676963987, 0.294795451892385, 0.141659679876788, 
    0.128895058876958, 0.0530746007081779, 0.439797839061484, 
    0.171106056872192, 0.141062546211989, 0.00257102000838311, 
    0.00514544376441906, 0.000715985216287402, 0.191666109378775, 
    0.12585928374854, 0.0559321404138331, 0.590852809920102, 
    0.274161403213926, 0.344444029816024, 0.051074623626536, 
    0.187006960782375, 0.0627044610324955, 0.00271511733050407
    ), .Names = c("H01", "H02", "H03", "H04", "H05", "H06", "H07", 
    "H08", "H09", "H10", "H11", "H12", "H13", "H14", "H15", "H16", 
    "H17", "H18", "H19", "H20", "H21", "H22", "H23", "H24", "H25", 
    "H26", "H27", "H28", "H29", "H30", "H31", "H32", "H33", "H34", 
    "H35", "H36", "H37", "H38", "H39", "H40", "H41", "H42", "H43", 
    "H44", "H45", "H46", "H47", "H48", "H49", "L01", "L02", "L03", 
    "L04", "L05", "L06", "L07", "L08", "L09", "L10", "L11", "L12", 
    "L13", "L14", "L15", "L16", "L17", "L18", "L19", "L20", "L21", 
    "L22", "L23", "L24", "L25", "L26", "L27", "L28", "L29", "L30", 
    "L31", "L32", "L33", "L34", "L35", "L36", "L37", "L38", "L39", 
    "L40", "L41", "L42", "L43", "L44", "L45", "O01", "O02", "O03", 
    "O04", "O05", "O06", "O07", "O08", "O09", "O10", "O11", "O12", 
    "O13", "O14", "O15", "O16", "O17", "O18", "O19", "O20", "O21", 
    "O22", "O23", "O24", "O25", "O26", "O27", "O28", "O29", "O30", 
    "O31", "O32", "O33", "O34", "O35", "O36", "O37", "O38", "O39", 
    "O40", "O41", "O42", "O43", "O44", "O45")), group = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    centroids = structure(c(-0.0126793406800765, -0.0048734688334266, 
    0.0161199271042375), .Dim = c(3L, 1L), .Dimnames = list(c("1", 
    "2", "3"), "PCoA1")), call = betadisper(d = PMPhyloSOVegdist, 
        group = Predictors$class, type = "centroid")), .Names = c("eig", 
"vectors", "distances", "group", "centroids", "call"), class = "betadisper", method = "gower", type = "centroid", bias.adjust = FALSE)

1 个答案:

答案 0 :(得分:0)

在R基础图中,par中有一个选项可以使用“漂亮”边距。对于漂亮的标签,此默认选项会将轴值的数据范围扩展4%。这有副作用,有时会忽略xlimylim

要尝试的一件事是切换此选项以准确使用数据范围。对于X和Y轴,选项分别为xaxs="i"yaxs="i"

以下是我使用boxplot代码时打开和不打开这些选项的示例:

Betadisp<-data.frame(distances=
                       c(0.171774954519456, 
                           0.0623868535364207, 0.0621507889963585, 0.0869573133495299, 
                           0.0169233135900547, 0.0080384125007011, 0.173187193386872, 
                           0.175566607202207, 0.263873649177415, 0.269596668079633, 
                           0.309083021546677, 0.114187871090147, 0.124154597704232, 
                           0.295532192696357, 0.333332176352344, 0.218105548195935, 
                           0.00291069847495801, 0.380347922295584, 0.0050168867788829, 
                           0.285763501850193, 0.190753345691698, 0.122265556127451, 
                           0.108666519108659, 0.29755013745441, 0.0467048709645198, 
                           0.0186762875503434, 0.16133848141303, 0.138430730434188, 
                           0.297991278634962, 0.168844071561681, 0.160066867257958, 
                           0.171285258028997, 0.0159658923646438, 0.239593234480484, 
                           0.11149382715073, 0.150842075345091, 0.142565290934167, 0.0838881561641354, 
                           0.110208116588627, 0.105737052071915, 0.156385076275321, 
                           0.045244914460967, 0.206865801853543, 0.0183152170680244, 
                           0.159206217954337, 0.277843809072997, 0.0888342965374933, 
                           0.315721981172762, 0.204295902587072, 0.12797501136046, 0.185151760663692, 
                           0.220301416680961, 0.195629716424938, 0.170000042629844, 
                           0.144764318702658, 0.0982423159776023, 0.077784806093838, 
                           0.200974730859957, 0.0474174597287366, 0.119811361544433, 
                           0.0872631008364077, 0.147577189165856, 0.0672150078204365, 
                           0.0655639372667505, 0.147983825716124, 0.13939190413907, 
                           0.0839168256483553, 0.0110682447772202, 0.0804055775665854, 
                           0.0643234769159, 0.152687458838593, 0.286371553538374, 0.0901197325885831, 
                           0.153693904513509, 0.0957030433961639, 0.0105961602592448, 
                           0.320362093692189, 0.0932809343859479, 0.0878333741087963, 
                           0.0770368040506406, 0.0959749515319617, 0.0332807667898261, 
                           0.191933577147619, 0.0787686840490281, 0.0496881262302877, 
                           0.0485530197564093, 0.112560776469416, 0.0874930322963143, 
                           0.203908196211518, 0.0418832585552037, 0.0581386263146353, 
                           0.0105720607945607, 0.115237490805503, 0.0156632717717389, 
                           0.0914369812138262, 0.140451490489812, 0.0539609303513526, 
                           0.259669347645691, 0.0667734806834846, 0.0058207347089303, 
                           0.162226558213165, 0.103192578557601, 0.0165544329655469, 
                           0.0356738544468149, 0.248033053584017, 0.348663085500656, 
                           0.281938256010966, 0.019572401013719, 0.0149422179335055, 
                           0.174230363870647, 0.0968716024168187, 0.106430191879212, 
                           0.197156957697445, 0.157750474524708, 0.319388997989459, 
                           0.084827956591434, 0.0316429009940913, 0.0853248197344982, 
                           0.207212676963987, 0.294795451892385, 0.141659679876788, 
                           0.128895058876958, 0.0530746007081779, 0.439797839061484, 
                           0.171106056872192, 0.141062546211989, 0.00257102000838311, 
                           0.00514544376441906, 0.000715985216287402, 0.191666109378775, 
                           0.12585928374854, 0.0559321404138331, 0.590852809920102, 
                           0.274161403213926, 0.344444029816024, 0.051074623626536, 
                           0.187006960782375, 0.0627044610324955, 0.00271511733050407),
                     group=c(3, 
                             3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
                             3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
                             3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
                             3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
                             2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                             1, 1, 1)
                       )
Betadisp$group<-factor(Betadisp$group,levels=c("3","2","1"))

没有xaxs="i"yaxs="i"

boxplot(Betadisp$distances ~ Betadisp$group, col=c("darkgreen", "blue", "red"), 
boxwex=0.4, outline=T, ylim=c(0, 0.46), xlim=c(0.75,3.25), names = c("OG", "LIL","HIL"), 
cex.lab=1.5, ylab="Distance to centroid", xlab='x', las = 1, notch=F, axis=T)

Default xaxs="r" and yaxs="r", slightly extended past data range

xaxs="i"yaxs="i"

boxplot(Betadisp$distances ~ Betadisp$group, col=c("darkgreen", "blue", "red"), boxwex=0.4, 
outline=T, ylim=c(0, 0.46), xlim=c(0.75,3.25), names = c("OG", "LIL","HIL"), 
cex.lab=1.5, ylab="Distance to centroid", xlab='x', las = 1, notch=F, axis=T, 
xaxs="i",yaxs="i")

Changed xaxs="i" and yaxs="i", so that the ylim is exactly 0 to 0.46

供参考,请看右上角。我没有触及任何保证金参数。

此外, Vincent Bonhomme 是正确的:您必须在提交地图/箱图代码之前提交par。这是因为par修改仅适用于下一组基本图形命令。在tiff()表达式中使用par的方式将不允许par影响任何内容。将所有par表达式移至tiff()下的下一行。