I am trying to write a code for multiple pie charts showing top 10 bacterial abundance during initial treatment days, mid treatment days and end of treatment days antibiotic usage for two drugs say X and Y . But in this plot I need to fix three points
- putting Others at the end for all plots.
- Add a common legend
- add a common order so that all the successive pie charts take the bacteria order from the first. Not reorder them every time. So that it will be easier to compare by eye. Can anyone please help?
My code is as bellow.
dput(xInitialDays)
structure(list(Dataset = c("Lachnospiraceae", "Bifidobacteriaceae",
"Oscillospiraceae", "Enterobacteriaceae", "Peptostreptococcaceae",
"Sutterellaceae", "Erysipelotrichaceae", "Peptoniphilaceae",
"Clostridiaceae", "Bacteroidaceae", "Others"), Values = c(41670.41,
22926.45, 21939.35, 15083.38, 11544.14, 10824.77, 9349.65, 6716.93,
4856.3, 3807.31, 20828.8), per = c(24.58, 13.52, 12.94, 8.9,
6.81, 6.38, 5.51, 3.96, 2.86, 2.25, 12.27)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 65L), class = "data.frame")
allcols= 20
getPalette = colorRampPalette(brewer.pal(12, "Set3"))
fill=getPalette(allcols)
allcols<-fill
xInitialDays %>%
mutate(taxa = xInitialDays$Dataset) %>%
mutate(cols = fill[c(1:11)]) %>%
select(taxa,cols) -> ColAssgn
len = 11
xinidays <- function(){
xInitialDays%>%
ggplot(aes(x='',y=reorder(Values,Values),fill=Dataset))+
geom_bar(width=1,stat="identity")+
scale_fill_manual(breaks = ColAssgn$taxa,
values = ColAssgn$cols)+
theme_void()+
theme_classic() +
theme(legend.position = "top") +
coord_polar("y",start=0) +
theme(axis.line = element_blank())+
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank())+
labs(x = NULL, y = NULL, fill = NULL)+
ylab("Baseline")
}
order(xInitialDays$Values)
xinidays()
I have repeated the similar code for middays and final days and for X and Y.
> dput(xMidDays)
structure(list(Dataset = c("Bifidobacteriaceae", "Oscillospiraceae",
"Enterobacteriaceae", "Lachnospiraceae", "Erysipelotrichaceae",
"Sutterellaceae", "Akkermansiaceae", "Acidaminococcaceae", "Bacteroidaceae",
"Lactobacillaceae", "Others"), Values = c(41204.96, 22093.39,
21504.25, 19273.93, 14853.98, 7146.53, 6734.07, 4340.38, 3444.21,
2541.56, 18130.24), per = c(25.55, 13.7, 13.33, 11.95, 9.21,
4.43, 4.18, 2.69, 2.14, 1.58, 11.23)), row.names = c(1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 65L), class = "data.frame")
And I took this strategy bellow to select the same bacteria colours
taxalen <- xMidDays$Dataset[c(which(!(xMidDays$Dataset %in% ColAssgn$taxa)))]
for (count in 1:length(taxalen)){
if(length(taxalen) > 0){
len = len +1
ColAssgn %>%
rbind(c(taxalen[count],allcols[len])) -> ColAssgn
}
}
xmiddays <- function(){
xMidDays %>%
ggplot(aes(x='',y=reorder(Values,Values),fill = Dataset))+
geom_bar(width=1,stat="identity")+
scale_fill_manual(breaks = ColAssgn$taxa,
values = ColAssgn$cols)+
theme_void()+
theme_classic() +
theme(legend.position = "top") +
coord_polar("y",start=0) +
theme(axis.line = element_blank())+
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank())+
labs(x = NULL, y = NULL, fill = NULL)+
ylab("Mid Treatment")
}
xmiddays()
And for final days
> dput(xFinaldays)
structure(list(Dataset = c("Lachnospiraceae", "Bifidobacteriaceae",
"Oscillospiraceae", "Enterobacteriaceae", "Erysipelotrichaceae",
"Clostridiaceae", "Acidaminococcaceae", "Peptostreptococcaceae",
"Sutterellaceae", "Bacteroidaceae", "Others"), Values = c(32106.33,
21813.1, 12246.03, 8259.62, 7029.33, 6365.3, 4091.83, 2610.92,
1968.89, 1864.72, 6713.37), per = c(30.56, 20.76, 11.66, 7.86,
6.69, 6.06, 3.89, 2.48, 1.87, 1.77, 6.39)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 52L), class = "data.frame")
taxalen <- xFinaldays$Dataset[c(which(!(xFinaldays$Dataset %in% ColAssgn$taxa)))]
for (count in 1:length(taxalen)){
if(length(taxalen) > 0){
len = len +1
ColAssgn %>%
rbind(c(taxalen[count],allcols[len])) -> ColAssgn
}
}
xfindays <- function(){
xFinaldays %>%
ggplot(aes(x='',y=reorder(Values,Values),fill = Dataset))+
geom_bar(width=1,stat="identity")+
scale_fill_manual(breaks = ColAssgn$taxa,
values = ColAssgn$cols)+
theme_void()+
theme_classic() +
theme(legend.position = "top") +
coord_polar("y",start=0) +
theme(axis.line = element_blank())+
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank())+
labs(x = NULL, y = NULL, fill = NULL)+
ylab("End of Treatment")
}
xfindays()
And now for the combined group X plot :
group_x <- plot_grid(
xinidays() + theme(legend.position="none"),
xmiddays() + theme(legend.position="none"),
xfindays() + theme(legend.position="none"),
nrow = 1
)
group_x_plot <-plot_grid(group_x,ncol=1
,axis = "tblr"
,labels = "Genus level comparison at multiple treatment time points for Drug X",
label_size = 15
)
group_x_plot
Please anyone can help me with the above questions and fix this code? many thanks, Mitra