简体   繁体   中英

Placing an x axis on top of a bar plot with negative y values

I have some data for various experimental treatments which are negative values. I'd like to plot the x-axis on top of the bar-plot and have the labels located the same as if it were a traditional bar-plot at the centre of the bars.

I've checked all over and can't find anything that lets me name the axis labels with words, only setting tick marks and numbers.

Here's the data: - "wp_means"

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

Here's my code to plot the bar-plot. I can omit the x-axis from it's usual position, but I can't seem to get it on top with its labels etc. where I want it to be.

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

And here's the plot: I'd like to have the labels on the upper x axis in the order they appear in "levels = c("Control"....)" per above. I'd also like to set a label title the same way one would usually do with xlab="some name" without it interfering with my chart title.

Thanks

在此输入图像描述

The appropriate method I believe is to use the pos= argument when adding an ?axis to your existing barplot . You will need to save the positions of the bar centres first however. Like bp <- barplot(...) , so:

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)

Note the use of line=3 when adding a title() which pushes your chart title away from the group labels.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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