在具有负y值的条形图上方放置x轴

4

我有一些与实验处理相关的数据,这些数据是负值。我想把 x 轴放在柱状图上方,并且让标签位于传统柱状图中心位置。

我查找了很多地方,但找不到任何东西可以用单词命名轴标签,只能设置刻度和数字。

以下是数据:- “wp_means”

>         12         15          3          6          9    Control 
-0.3000000 -0.2416667 -0.3416667 -0.3916667 -0.2750000 -0.2750000 
        DL 
-0.2833333

这是我的绘制条形图的代码。我可以将x轴从通常的位置省略,但似乎无法将其与标签等放在我想要的位置的顶部。

cols<-c("blue1", "cyan","chartreuse","mediumspringgreen","maroon1","orange","red")
    wp<-data.frame(a=c(wp_means),b=c(12,15,3,6,9,"Control","DL"))
    wp$c=factor(wp$b, levels = c("Control",15,"DL",12,9,6,3))
    wp <- wp[order(wp$c), ]
    barplot(height=wp$a,names.arg=wp$c,col=cols,main="Water Potential",las=1,xaxt="n",
    ylab = "Water potential 
    (MPA)",pch=21,bg="black",cex=0.7,cex.lab=0.8,font.lab=2,
    cex.axis=0.7,font.axis=2,cex.main=1,ylim=c(-0.5,0),xaxt="n")

这里是需要翻译的内容:

以下是主要内容:我希望上部 X 轴上的标签按照 "levels = c("Control"....)" 中的顺序出现。同时,我也想设置一个标签标题,就像使用 xlab="some name" 一样,但不影响我的图表标题。

谢谢

enter image description here


你能再详细解释一下吗?根据我拥有的数据,我应该在哪里设置条形图的位置 bp<-barplot(...)。 - A.Benson
1个回答

5

我认为适当的方法是在现有的barplot中添加?axis时使用pos=参数。但是您需要先保存条形图中心的位置,例如:bp <- barplot(...),因此:

wp_means <- c(-0.3, -0.2, -0.34, -0.39, -0.275, -0.275, -0.283)

cols<-c("blue1", "cyan","chartreuse","mediumspringgreen","maroon1","orange","red")

wp <-data.frame(a=c(wp_means),b=c(12,15,3,6,9,"Control","DL"))

wp$c=factor(wp$b, levels = c("Control",15,"DL",12,9,6,3))
wp <- wp[order(wp$c), ]
bp <- barplot(height=wp$a, names.arg=wp$c, col=cols, las=1, xaxt="n",
         ylab = "Water potential (MPA)",
         pch=21, bg="black", cex=0.7, cex.lab=0.8, font.lab=2,
         cex.axis=0.7, font.axis=2, cex.main=1, ylim=c(-0.5,0), xaxt="n")

axis(side=3, pos=0, at=bp, labels=levels(wp$c))
title(main="Water Potential", line=3)

请注意,在添加标题(title())时使用line=3,可以将您的图表标题与组标签分开。

enter image description here


太好了!最后一个问题。我如何使新的x轴标签的字体和大小与y轴标签的字体匹配?font=2,cex.axis=0.8。标题字体的大小为cex=1。 - A.Benson
不用担心。我搞定了。谢谢你的帮助。 - A.Benson
与之前设置相同:axis(side=3, pos=0, at=bp, labels=levels(wp$c), cex.axis=0.8, font=2)title(main="水势", line=3, cex.main=1) - thelatemail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接