0

Hi I'm working in R for a new job and am creating a box plot that displays levels of sample contamination. I have got the graph looking the way I want, but I can't get the legend to move to the side without disappearing and the x-axis labels falling off the bottom.

My code looks like this:

#adds padding to the inner margin of the graph, allows legend and labels to fit
par(mar=c(10,5,3,10)) 

stackedGraphGOOD = barplot(as.matrix(goodKrakenData), 
                              col = c("DODGERBLUE", "GREEN3", "RED"), 
                              ylab = "Percent", border = NA, axis.lty = 1, cex.axis = 1,  
                              main = "Kraken Quality Percentages", 
                              legend.text = c("Unclassified", "Family", "Other"), 
                              args.legend = list(x = 14, bty='y'), las = 2)
         #allows me to adjust the placement of the x-axis label
         title(xlab = "Animal ID", line = 9) 

and I get a graph that looks like this:

GoodGraph

I needed to adjust the inner and outer margins of the graph for it to display properly

taxonomy ID1 ID2 ID3
unclassified 19 5 10
Other 0.0003 0.0006 0.08
primaryFamily 80.9997 94.9994 89.92
edy2412
  • 21
  • 7
  • Can you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data in order we try to troubleshoot the code ? – dc37 Nov 28 '19 at 06:33
  • I'm an intern and the data I have belongs to the company and is used for business. I'm pretty sure it's confidential and I can't share it. I have already changed some of the labels before posting this question. – edy2412 Nov 28 '19 at 20:32
  • My data follows the format of – edy2412 Nov 28 '19 at 20:40
  • Just provide a reproducible example. It doesn't have to be real data. Just similar one. Change names, and numbers but keep the structure. – dc37 Nov 29 '19 at 05:56
  • Like the image I attached above? – edy2412 Dec 01 '19 at 19:54
  • Yes, but in a way, people can copy and paste to get re-use on R in order to trouble shoot your code.Please read this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Dec 01 '19 at 20:02

1 Answers1

0

I managed to fix my problem by adjusting the margins of the graph. By adding the line

par(mar=c(10,5,3,10)) #adds padding to the inner margin of the graph, allows legend and labels to fit

at the beginning and

title(xlab = "Animal ID", line = 9) #allows me to adjust the placement of the x-axis label

at the end to adjust the x axis label Found answer at the https://www.r-graph-gallery.com/74-margin-and-oma-cheatsheet.html

edy2412
  • 21
  • 7