-1

ExampleDataTable

ggplot(data = Flint %>% filter(Flint$Lead <=7), aes(y=Flint$Lead, group=Flint$Ward)) + geom_boxplot() + stat_boxplot()

I have an excel file which Im trying to ggplot. I have to remove all values above 7 from the ggplot as its considered an outlier. I have tried to use subset and filter but am receiving this error. Error: Aesthetics must be either length 1 or the same as the data (5): y, group Is there a way I can get the data for Flint$First_Draw to be all values below . Sample Output would be multiple box plots based on wards. An example would be Ward 1: Lead 5.0 6.3, Ward 2: Lead 3.6, Ward 3: 2.8. For Ward 1 Lead 7.8 wouldn't appear as it is an outlier and is above 7. Thanks in advance!

  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do not post code or data as images because then we have to retype everything in order to test it. – MrFlick Feb 19 '20 at 21:17
  • I edited it to make it a reproducible example :). Thanks for letting me know – Cantrash123 Feb 19 '20 at 21:23
  • 1
    Without a `dput` of your `Flint` data set, as explained in the link above, we are unlikely to be able to do much to help. – cardinal40 Feb 19 '20 at 21:31
  • Instead of `data = Flint` try `data = Flint[Flint$First_Draw < 50, ]` – G5W Feb 19 '20 at 21:33
  • @G5W it gave the same error unfortunately – Cantrash123 Feb 19 '20 at 21:36

1 Answers1

0

If you remove the Flint$ elements from the aes call then it should work.

This works:

data(mtcars)
ggplot(mtcars %>% filter(mpg <= 20), aes(y = hp, group = cyl)) +
  geom_boxplot()

But this fails with a similar error message:

ggplot(mtcars %>% filter(mtcars$mpg <= 20), aes(y = mtcars$hp, group = mtcars$cyl)) +
  geom_boxplot()

Error: Aesthetics must be either length 1 or the same as the data (18): y, group
cardinal40
  • 1,245
  • 1
  • 9
  • 11
  • I tried changing it to use just the name of the column but it still gave me the same error. That works if the column isn't the same as y or group – Cantrash123 Feb 19 '20 at 21:22