Simply I want to Plot a bar chart like the following figure from Orange dataset
Any help will be appreciated.
Simply I want to Plot a bar chart like the following figure from Orange dataset
Any help will be appreciated.
The idea of my code is to use case_when
to create the ageGroup
column first, summarize the data to show only maximum for each Tree
and ageGroup
combination, and then convert the ageGroup
column to factor and arrange
the column, which is relevant to the order on the axis of bar-chart.
We can then plot the data using ggplot2
. Notice that geom_col
is a simpler version to create bar-chart compared to geom_bar
without the needs to call stat = "identity"
. scale_fill_brewer
can call the colorbrewer palette directly, which is quite handy.
data("Orange")
library(dplyr)
library(ggplot2)
Orange2 <- Orange %>%
mutate(ageGroup = case_when(
age <= 250 ~"Young",
age > 250 & age <= 900 ~"Adult",
age > 900 ~"Old"
)) %>%
group_by(Tree, ageGroup) %>%
summarise(circumference = max(circumference)) %>%
ungroup() %>%
mutate(ageGroup = factor(ageGroup, levels = c("Young", "Adult", "Old"))) %>%
arrange()
ggplot(Orange2, aes(x = ageGroup, y = circumference, fill = Tree)) +
geom_col(position = position_dodge()) +
scale_x_discrete(name = "Age Group") +
scale_y_continuous(name = "Circumference") +
coord_flip() +
scale_fill_brewer(type = "qual", palette = "Paired") +
theme_bw() +
ggtitle("Growth of Orange Trees")
as you wished, same color, labes, axes
library(tidyverse)
color_palette <- c("#a5cde2", "#1e78b5", "#b0dd89", "#33a02b", "#f99a98")
Orange %>%
mutate(AgeGroup=ifelse(age<250, "young", ifelse(age>900, "old", "adult"))) %>%
group_by(Tree, AgeGroup) %>%
summarise(circumference = max(circumference)) %>%
ggplot(aes(AgeGroup, circumference, fill=Tree)) +
geom_bar(position = "dodge", stat="identity") +
scale_x_discrete(limits=c("young","adult", "old")) +
coord_flip() +
scale_fill_manual(values = color_palette) +
theme_bw()
For variation, a dplyr
less answer.
Use cut
to discretise the age
variable
Orange$ageGrp <- with(Orange, cut(age, c(0, 250, 900, Inf),
c("Young", "Adult", "old")))
position_dodge()
is used so the bars are next to each other, and setting fun.y=max
selects the maximum circumference
.
library(ggplot2)
ggplot(Orange, aes(x=ageGrp, y=circumference, fill=Tree)) +
stat_summary(geom="bar", fun.y=max, position=position_dodge()) +
coord_flip()
Or using geom_bar
directly
ggplot(Orange, aes(x=ageGrp, y=circumference, fill=Tree)) +
geom_bar(stat="summary", fun.y=max, position=position_dodge()) +
coord_flip()
You could assign the groups based on age
by using mutate
and if_else
.
library("tidyverse")
data(Orange)
Orange%>%
mutate(age_group=if_else(age>900,"Old",
if_else(age<900&age>250,"Adult",
if_else(age<250,"Young",""))))%>%
ggplot(aes(age_group,circumference,fill=Tree))+
geom_bar(stat="identity",position=position_dodge())+
scale_x_discrete(limits=c("Young","Adult","Old")))+
coord_flip()