1

I am relatively new user in R - and I seem stuck on what should be fairly easy, I am just not finding the problem in my code set-up. I am trying to create a legend on a simply box plot but I cannot get it to line up correctly, without overlaying itself.

My box plot:

boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'))

Then when I add a legend:

legend("topright", c("linux", "windows"), border="black", fill = "blue", "gold1")

All it does is show me a blue square with the words gold1 - instead of double stacking the Linux and windows groups with the corresponding colors.

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
A.Douglas
  • 11
  • 1
  • 2
  • 2
    Hi, welcome to StackOverflow. In general, it's recommended to create some data for us so that we can replicate the problem ourselves. Screenshots of the plot would also have been appreciated, as it shows us what the problem is. However, in this case the solution is rather easy. You typed `fill = "blue, gold1"`. You tried supplying a vector that contains blue and gold1, correct? Try googling on how to make vectors in R... (or type `?c`). – slamballais Sep 22 '17 at 14:34
  • 1
    Please see [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways to include sample data in order to make it easier to help you. – MrFlick Sep 22 '17 at 14:39

1 Answers1

3

I think you made a simple mistake by not concatenating the fill colors:

Mock data:

OS <- data.frame(
  x = rnorm(100),
  y = runif(100)
)

boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'), frame = F)
legend("topright", c("linux", "windows"), border="black", fill = c("blue", "gold1"))

enter image description here

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34