0

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: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Jan 29 '20 at 20:11
  • 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 Jan 29 '20 at 20:24
  • 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 Jan 29 '20 at 20:26
  • 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 Jan 29 '20 at 20:34

1 Answers1

0

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

dc37
  • 15,840
  • 4
  • 15
  • 32
  • It did work thank you! As suspected I just needed someone with better trained eyes & R knowledge take a look. Thanks for explaining how that function works as well (and for the posting tips! :D ) – Cameron Richards Jan 29 '20 at 20:57
  • Glad that it works for you ;) I edited my answer to provide an alternative solution using `ggplot2`. Maybe when you will have time to digest it, you can try it and see how it fits to your need. Personally, I feel it's much easier (once you understand the logic behind) – dc37 Jan 29 '20 at 21:02
  • Thanks very much, I'll definitely look into it! had a friend mention ggplot yesterday as well when talking to them about this. – Cameron Richards Jan 30 '20 at 13:43
  • I was also just looking at it again, it certainly fixed my original issue which is all I was checking for when looking at this yesterday before moving on, but now realize it did also remove the numerical year labels - I am going to fiddle around with it see what I can do, & hopefully I'll update with that particular fix. – Cameron Richards Jan 30 '20 at 14:11
  • Okay, removing the ' xaxt = "n" ' brought the years back and the original issue still resolved! – Cameron Richards Jan 30 '20 at 14:41
  • Glad you figure it out ;) – dc37 Jan 30 '20 at 15:40