'Reordering boxplot (ggplot) with scale_x_discrete(limits=(...) results in Warning message: Removed 103 rows containing missing values (stat_boxplot)

Probably a user error but I can't find it for the life of me. Trying to rearrange the order of factors in a boxplot I am making using ggplot using the, "scale_x_discrete(limits=(...)" function. Doing so results in the following warning message "Warning message: Removed 103 rows containing missing values (stat_boxplot". and a plot where one grouping is missing it's boxplot all together. I am confused as this has worked just fine for nearly identical plots of other factors, and the plot is totally normal if I don't try to rearrange and run it as is. There is nothing wrong with the underlying dataframe that I can tell (e.g. no missing values or NAs). Any thoughts on what the issue might be here?

Thanks!

example of boxplot pre-issue:

maxn_topo_plot<- ggplot(wide.df2, aes(x=topo, y=sum_maxn)) + 
  geom_boxplot(show.legend = FALSE) + theme_pubclean(base_size = 20) +  xlab("Island Geomorpholgy") + ylab("Shark MaxN") + 
 theme(axis.text.x =  element_text(angle = 12))


maxn_topo_plot

The code that has the issue:

maxn_topo_plot<- ggplot(wide.df2, aes(x=topo, y=sum_maxn)) + 
  geom_boxplot(show.legend = FALSE) + theme_pubclean(base_size = 20) +  xlab("Island Geomorpholgy") + ylab("Shark MaxN") + 
  scale_x_discrete(limits=c("open atoll","closed atoll","near atoll", "high barrier", "high rocky", "high fringing")) + 
  theme(axis.text.x =  element_text(angle = 12))


maxn_topo_plot

code that works just fine:

max_isl_group<- ggplot(wide.df2, aes(x=isl_grp, y=sum_maxn)) + 
geom_boxplot(show.legend = FALSE) + theme_pubclean(base_size = 20) +  xlab("Island Group") + ylab("Shark MaxN") + 
 scale_x_discrete(limits=c("west tuamotu","east tuamotu","windward", "leeward", "marquesas", "australes")) + 
  theme(axis.text.x =  element_text(angle = 20))

max_isl_group 


Solution 1:[1]

I dont know exactly whats happening on your code

But, if you want other way to reorder the x axis without this message here it is:

maxn_topo_plot<- ggplot(wide.df2, aes(x= factor(topo,
                                          levels = c("open atoll",
                                                     "closed atoll",
                                                     "near atoll", 
                                                     "high barrier", 
                                                     "high rocky", 
                                                     "high fringing")), y=sum_maxn)) + 
  geom_boxplot(show.legend = FALSE)  +  
  xlab("Island Geomorpholgy") + 
  ylab("Shark MaxN") + 
  theme_pubclean(base_size = 20) +
  theme(axis.text.x =  element_text(angle = 12))

Solution 2:[2]

It was a rogue space in one of my factors "high fringing" that was the underlying issue. bangs head on desk. Hours well spent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucca Nielsen
Solution 2 N.F.F.