questions about axes and mtext

2019-08-09 09:33发布

问题:

I am swimming backwards in my R knowledge. Please help!

ExampleData:

Site, Aluminum_Dissolved, Federal_Guideline
M1, 0.1, 0.4
M1, 0.2, 0.4
M1, 0.5, 0.4
M2, 0.6, 0.4
M2, 0.4, 0.4
M2, 0.3, 0.4

I have a simple function:

boxplot(ExampleData$Aluminum_Dissolved ~ ExampleData$Site, col="purple", 
        par (cex.axis=2, las=2), mar=c(7,4,4,2)+0.1

X and Y axis Labels:

Once I increase the values on the axis so much, my xlab and ylab are obscured by axis text.

  1. I have tried using:

    `mpg=c(3,1,0)` 
    

    and altering values but that seems to get mess up with margin increase

    `mar=c(7,4,4,2)+0.1`
    
  2. I tried scrapping the xlab and ylab altogether and using mtext, but I can't get that to give me labels outside my axis text that are parallel to the y-axis. I have tried:

    `mtext("Dissolved Aluminum", side=2, adj=0, las)` etc....
    

45 degree text on x-axis:

And, finally, I have tried reconstructing my x and y-axis with no avail and I can't seem to rotate my x-axis labels 45 degrees using SRT function. I have tried:

boxplot(ExampleData$Aluminum_Dissolved ~ ExampleData$Site, col="purple",
        xaxt='n', yaxt='n', axis(2, cex.axis=2, xlab="Dissolved Aluminum"),
axis(1, cex.axis=2, srt=45)

and this doesn't work. What am I missing. Is there a simple way to do this I am missing...

回答1:

A quick tutorial:

The way that plotting works in base R graphics is general thought of as a "pen on paper" model. This means that each function you call draws "on top" of what you've created up to that point. Graphical parameters can either be set beforehand via a call to par, or passed directly to the plotting function directly (with some caveats). So for example, I would have done this as:

par(cex.axis=2, las=2,mar=c(7,4,4,2)+0.1)
boxplot(Aluminum_Dissolved ~ Site,data = dat, 
        col="purple",ylab = "Dissolved Aluminum",xlab = "Dissolved Aluminum")

If you wanted custom axes, you would have done something like:

par(cex.axis=2, las=2,mar=c(7,4,4,2)+0.1)
boxplot(Aluminum_Dissolved ~ Site,data = dat, 
        col="purple",ylab = "Dissolved Aluminum",xlab = "Dissolved Aluminum",axes = FALSE)
axis(...)

Subsequent call (on separate lines) to things like points or lines would add points or lines to the graph, respectively.

The caveat with par is that some parameters can only be set by calling par directly, not by passing them as named arguments to plotting functions. There is a list of those (which includes mar) located at ?par.



回答2:

@joran was right -- i think i just messed up the order of the function. I get the axis labels working despite greater size in text using this code:

    boxplot(ExampleData$Aluminum_Dissolved ~ ExampleData$Site, col="purple", par(cex.axis=2, cex.lab=1.8), ylab="Dissolved Aluminum")

The only problem with this is that the label is very close to text, but it is alright.



标签: r axes