0

I am a beginner in R have a question about process in terms of plotting values; I'm not sure how much the code will help. I am trying to make a boxplot of age categories for birds (Age Categories: HY, SY, TY, ATY, and NAs), while removing the NAs so that birds without an age don't get a boxplot at all. I've been able to remove the NAs just fine in other code to get me to KnownBehAgeCategoriesWithoutNAs, but I don't know how to tell R to match up the remaining values (non-NA values) with their corresponding y values, or how to eliminate the non-corresponding Y values myself. Is my idea of how to do this all wrong?

Below is an example of what I've tried, keeping in mind that KnownBehAgeCategoriesWithoutNAs is a short vector and DR is longer at 110 values.

ggplot(clawregression_data, aes(x= KnownBehAgeCategoriesWithoutNAs, y=DR, fill=KnownBehAgeCategoriesWithoutNAs)) + 
  geom_boxplot() +
  theme_classic() +
  labs(y="Display Rate", x="Known Behavioral Age Categories")+
  scale_fill_brewer(palette="BuPu")

And this is the error I get, which I think might be because the xs and ys don't match up. There are 110 values for y (DR), and many fewer for birds with actual age categories and not just NAs.

Don't know how to automatically pick scale for object of type <data.frame>. Defaulting to continuous. Error in geom_boxplot(): ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (110) ✖ Fix the following mappings: x Run rlang::last_error() to see where the error occurred.

I have also tried the below way of removing NAs because I'd like to reorder the categories anyway, but the NA values remain in a boxplot and I get the below message

ggplot(clawregression_data, aes(x= reorder(KnownBehAgeCategories,DR,na.rm = TRUE), y=DR, fill=KnownBehAgeCategories)) + 
  geom_boxplot() +
  theme_classic() +
  labs(y="Display Rate", x="Known Behavioral Age Categories")+
  scale_fill_brewer(palette="BuPu")

Warning message: Removed 42 rows containing non-finite values (stat_boxplot()).

Finally, I have tried reordering with the following code but it does not appear to do anything, as it does not change the output of later code.

KnownBehAgeCategories <- factor(clawregression_data$KnownBehAgeCategories, levels=c('HY', 'SY', 'TY', 'ATY', NA))

Apologies for the double question but it seemed that removing NAs and reordering were interlinked according to some of what I've read. I'd appreciate help with any of the above.

ksh530
  • 33
  • 4
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Jan 12 '23 at 19:25
  • From the error message for your first code I would guess that `KnownBehAgeCategoriesWithoutNAs` is a dataframe. To get rid of obs. with NA values you could do `clawregression_data_filtered <- clawregression_data[!is.na(clawregression_data$KnownBehAgeCategories), ]`. Then use the filtered dataset for plotting. – stefan Jan 12 '23 at 19:27

0 Answers0