简体   繁体   English

如何增加条形图中的y轴?

[英]How to increase y-axis in barplot?

My code: 我的代码:

#!/usr/bin/R
layout(matrix(c(1, 1, 2, 2, 3, 3, 4, 4, 5), ncol=1))

# main plots
par(mar=c(5,2,4,2))

fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
gk  <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
gg  <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(6,7,8,9,10,11,12)

fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
gk  <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
gg  <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
data2 <- rbind(fcm,gk,gg)
colnames(data2) <- c(6,7,8,9,10,11,12)

fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
gk  <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
gg  <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
data3 <- rbind(fcm,gk,gg)
colnames(data3) <- c(6,7,8,9,10,11,12)

fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
gk  <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
data4 <- rbind(fcm,gk)
colnames(data4) <- c(6,7,8,9,10,11,12)

barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
        xlab="number of clusters", ylab="traveltime rms(ms)",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data2),ylim=c(0,3),main="MT",
        xlab="number of clusters", ylab="MT functions",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data3),ylim=c(0,13),main="XBI",
        xlab="number of clusters", ylab="index value",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
        xlab="number of clusters", ylab="index value",
        col=c("red", "black"), beside=TRUE)

par(mai=c(0,0,0,0))
plot.new()
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"), 
    "center", horiz=TRUE)

This image will be part of my paper,plan to submit it to a journal.I am afraid editor will complain that values are not visible.How to change this?My goal is that there is clear distinction between all barplots,of course if this is possible. 这张图片将成为我论文的一部分,计划将其提交给期刊。我担心编辑会抱怨价值观不可见。如何改变这一点?我的目标是所有条形图之间有明显的区别,当然如果这样是可能的。 在此输入图像描述

Stacking your plots is compressing them unnecessarily & creating a lot of wasted white space. 堆积你的阴谋会不必要地压缩它们并造成大量浪费的空白区域。

Why not shift to a 2x2 representation? 为什么不转换为2x2表示?

Basically all you need to change is: 基本上你需要改变的是:

layout(matrix(c(1, 2, 5, 3, 4, 5), ncol=2))

I get this output with par(mar=c(3.1,2.1,2.1,0.1)) : 我得到这个输出与par(mar=c(3.1,2.1,2.1,0.1))

在此输入图像描述

You may notice we're giving too much space to the legend; 你可能会注意到我们给传说留下了太多的空间; this can be fixed by tinkering with the heights parameter to layout ; 这可以通过修改heights参数来修复layout ; here's the output with heights=c(.45,.45,.1) . 这是heights=c(.45,.45,.1) See ?layout . ?layout

在此输入图像描述

I prefer @MichaelChirico's answer, but if you need them to be stacked in one column for some reason, here is another option. 我更喜欢@MichaelChirico的回答,但是如果你因为某种原因需要将它们堆叠在一列中,这是另一种选择。 I've added comments to explain the changes to your original code. 我添加了注释来解释原始代码的更改。 Basically, I've created some additional vertical space by playing with the margins and the layout matrix, and getting rid of unnecessary x-axis titles: 基本上,我通过使用边距和布局矩阵创建了一些额外的垂直空间,并摆脱了不必要的x轴标题:

# Change layout matrix to allow more vertical space for bottom plot
# (since it is now the only one with an x-axis title)
layout(matrix(1:5, ncol=1), heights=c(rep(0.21,3), 0.26, 0.11))  

# main plots
par(mar=c(2,2.5,3.5,2))   # Decrease top and bottom margins

fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
gk  <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
gg  <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)

colnames(data1) <- c(6,7,8,9,10,11,12)

fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
gk  <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
gg  <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
data2 <- rbind(fcm,gk,gg)
colnames(data2) <- c(6,7,8,9,10,11,12)

fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
gk  <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
gg  <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
data3 <- rbind(fcm,gk,gg)
colnames(data3) <- c(6,7,8,9,10,11,12)

fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
gk  <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
data4 <- rbind(fcm,gk)
colnames(data4) <- c(6,7,8,9,10,11,12)

# Remove x-axis title from first three plots
# Add las=1 to all plots, so y-axis labels will be rotated
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
        ylab="traveltime rms(ms)", las=1,
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data2),ylim=c(0,3),main="MT",
        ylab="MT functions", las=1,
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data3),ylim=c(0,13),main="XBI",
        ylab="index value", las=1,
        col=c("red", "black", "green"), beside=TRUE)

# Change bottom margin for last plot so x-axis title will be included
par(mar=c(4,2.5,3.5,2))
barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
        xlab="number of clusters", ylab="index value", las=1,
        col=c("red", "black"), beside=TRUE)

par(mai=c(0,0,0,0))
plot.new()
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"), 
       "center", horiz=TRUE)

在此输入图像描述

I can't post comment, so I've decided to add it as answer... 我无法发表评论,所以我决定将其添加为答案...

Let's try with las and cex parameters: 让我们尝试使用las和cex参数:

barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
        xlab="number of clusters", ylab="traveltime rms(ms)",
        col=c("red", "black", "green"), beside=TRUE, las=1, cex.axis=.9)

You can also enlarge bars area by reducing first and third element of mar paratemer. 您还可以通过减少mar paratemer的第一和第三元素来扩大酒吧区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM