Hi
Does anyone know how to make a horizontal bar plot that is ordered descending by the numeric variable and puts NA values at the end.
So just like the example below but with the New species with NA Petal.Length on the bottom.
I am looking for a way to do this without manually specifying levels and also without changing the NA value to zero.
Any help would be gratefully received.
Thanks!!
library(dplyr)
library(ggplot2)
plot_data <- iris %>%
group_by(Species) %>%
summarise(Petal.Length = mean(Petal.Length)) %>%
add_row(Species = "New species") %>%
mutate(Species = forcats::fct_reorder(Species, Petal.Length))
ggplot(plot_data) +
geom_col(aes(Species, Petal.Length)) +
coord_flip()
FJCC
2
This works with your example, though I cannot explain why.
library(dplyr)
library(ggplot2)
plot_data <- iris %>%
group_by(Species) %>%
summarise(Petal.Length = mean(Petal.Length)) %>%
add_row(Species = "New species") %>%
mutate(Species = forcats::fct_reorder(Species, Petal.Length))
ggplot(subset(plot_data, !is.na(Petal.Length))) +
geom_col(aes(Species, Petal.Length)) +
geom_col(aes(Species, Petal.Length), data = subset(plot_data, is.na(Petal.Length))) +
coord_flip()
#> Warning: Removed 1 rows containing missing values (position_stack).
Created on 2020-07-07 by the reprex package (v0.3.0)
1 Like
Thanks @FJCC for the workaround idea.
I've added this as a feature request for forcats
system
Closed
4
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.