The correct figure should be grouped by Class and then otherwise the bars should be in descending order - essentially exactly the same as example 1 above.
CodePudding user response:
ggplot
automatically order alphabetically. If you want a different order, you can order the levels yourself.
df$Family <- factor(df$Family, levels = c("Muridae","Mustelidae",....))
I would also advise you to use facets to group the Classes: ggplot(...) facet_wrap("~Animal.group")
# randomize the order
df$Family <- factor(cdfiao$Family,
levels = sample(unique(df$Family)))
ggplot(df, aes(x = Family, y = n, fill = Animal.group))
facet_wrap("~Animal.group" ,,scales = "free_y", ncol =1)
geom_bar(stat = "identity")
scale_y_continuous(breaks = c(0, 250, 500, 750, 1000, 1250))
theme_classic()
labs(x = "Family", y = "Number", fill = "Class")
theme(axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.text.y = element_text(),
legend.title = element_text(face = "bold"))
CodePudding user response:
The problem ended up being in the dataset itself - the dataset was at the level of n.bp.sp$Common.name
, by grouping the dataset at the level of n.bp.sp$Family
one of @fra previous answers then works fine.
Code below:
n.bp.f2 <- n.bp.sp %>%
group_by(Animal.group, Family) %>%
summarise(n = sum(n))
n.bp.f2 %>%
arrange(Animal.group, desc(n)) %>%
mutate(Family = fct_inorder(Family)) %>%
ggplot()
aes(x = Family, y = n, fill = Animal.group)
geom_bar(stat = "identity")
scale_y_continuous(breaks = c(0, 250, 500, 750, 1000, 1250))
theme_classic()
labs(x = "Family", y = "Number", fill = "Class")
theme(axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.text.y = element_text(),
legend.title = element_text(face = "bold"))