0
votes

Hopefully this is a quick fix. I am trying to make boxplots of nutrient river concentrations using R code written by the person previously in my position (and I am not so experienced with R, but we use it only for this). The issue is that the output boxplot axis have multiple overlapping text, some of which seems to come from another part of the code which I thought did not dictate axis labels. The original code is shown below (the working directory is already set and csv files imported and I know that works), and the resulting boxplot is in 1.

Edit: Code below

png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10,
    height=4,
    unit="in",
    res=600)
par(mar=c(5,5,3,1),
cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n"),
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year")
title(main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 - 
",max(TNhist$Year),") TN Concentration"))
dev.off()

I made the edit of adding ,xlab="",ylab="" after xlab="Year" towards the bottom, since this fixed this issue in other similar sections of boxplot code (except it seems I needed to add it to a different part of those sections, see 2 - also tried it after xaxt ="n" as in 2 and got the same result). It fixes the overlapping text issue, but the axis labels are still not what I want them to be ("Year", and "TN Concentration (ppb)), and this is shown in 3.

So, does anyone potentially know of a simple fix that might get rid of these unwanted labels and replace them with the correct ones? Am I missing something basic? The same original code seemed to work fine in the past before I was doing this (for 2018 data), and the spreadsheets the data is being imported from are the same, same setup and everything. Many thanks in advance!

Edit: I have a sample dataset which is just the last 2 years of data. See here: https://docs.google.com/spreadsheets/d/10oo9w-IzXkLWdY10A9gHYhDH67MeSibBpc2q67L6o88/edit?usp=sharing

Original code result

How this code fixed other similar issues

Partially fixed result based on edit

1
Instead of adding images of your code, can you paste it as plain text ? Also can you provide a reproducible example of your dataset ? see here: stackoverflow.com/questions/5963269/… - dc37
I can try - the preview was showing all text I pasted without the entered rows, as one line, and it looked very hard to read. I will also see what I can do about making a small enough sample dataset, thanks - Cameron Richards
Your image is readable but the point of pasting it as plain text is that people can simply copy/paste it on their R session in order to test your code and provide a solution faster. Same with the example, using your own example, we can be sure that the solution provided will work for you. - dc37
That was my original instinct too, but like I said I couldn't figure out the formatting - I got it now! Just going to grab a dataset now, thank you! - Cameron Richards

1 Answers

0
votes

I don't know if it is a typo but you have an extra parenthesis after xatx = "n". Maybe you can try something like that:

png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
    width=10, height=4, unit="in", res=600)
par(mar=c(5,5,3,1), cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010], data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
        data=TNhist,
        ylim=c(0,3000),
        xaxt="n", ylab = "", xlab = "",
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
     -50,
     paste("n=",
           tnhistconc$n),
     cex=0.8)
title(ylab="TN Concentration (ppb)",
      xlab="Year", 
      main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 - 
",max(TNhist$Year),") TN Concentration"))
dev.off()

xatx will remove the x axis (that will control by axis.break. xlab and ylab will remove x and y axis title and they will be set later by title.

Hopefully, it will works

EDIT: Using ggplot2

Your dataframe is actually in a longer format making it easily ready to be plot using ggplot2 in few lines. Here your dataset is named df:

library(ggplot2)
ggplot(df, aes(x = as.factor(Year), y = Conc_ppb))+
  geom_boxplot()+
  labs(x = "Year", y = "TN Concentration (ppb)",
       title = paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 - 
",max(df$Year),") TN Concentration"))

enter image description here