#r #ggplot2
Вопрос:
Я пытаюсь создать график этих точек данных с двумя группами до и после. Ось y-это процент, а ось x-антибиотик. Я хочу, чтобы группы до и после были разделены, но не уверен, что я делаю неправильно.
Данные:
#Bar chart of Before and after ####
d1 <- MDRclasslong0611
d2 <- MDRclasslong1217
All <- rbind(
within(d1, {DS <- 'Before'}),
within(d2, {DS <- 'After'}))
names(All)[3] <- "Antibiotic"
names(All)[3] <- "Number of Isolates"
names(All)[3] <- "Percent"
names(All)[4] <- "Group"
ggplot(All, aes(factor(Antibiotic), Percent, fill = factor(Antibiotic)))
geom_bar(stat = 'identity', position="dodge")
xlab("Antimicrobial Class")
ylab("Percent Resistant Isolates")
ggtitle("Antimicrobial Resistance: 2012-2017")
theme(plot.title = element_text(hjust = 0.5))
theme_bw()
scale_y_continuous(labels = scales::percent_format(scale = 1), limits = c(0,90))
scale_fill_manual("Class", values=colors, labels = labels)
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), text
= element_text(size=16), legend.title = element_text(size=14), legend.text = element_text(size = 10))
Комментарии:
1. Рассматривали ли вы возможность использования фасетов для сравнения до и после?
2. Даст ли это мне графическое представление, похожее на гистограмму?
3. Да, просто попробуй
facet_wrap(~ Group)
.4. Это дало две отдельные гистограммы с группами, есть ли способ, чтобы я мог расположить столбцы непосредственно рядом друг с другом? Спасибо!
5. Возможно, вы просто забыли
aes(group = Group)
об этойgeom_bar()
функции.
Ответ №1:
Вот сценарий с возможным решением. Пожалуйста, дайте мне знать.
library(tidyverse)
library(scales)
# tweak data
df %>%
mutate(across(where(is.character), as.factor)) %>%
mutate(Group = fct_relevel(Group, "Before", "After"))
# Plot 1
ggplot(df) aes(x = Antibiotic, y = Percent, fill=Group)
geom_bar(stat="identity", position=position_dodge())
geom_text(aes(label=paste(round(Percent,1),"%")), vjust=-0.25, color="black",
position = position_dodge(0.9), size=3.5)
xlab("Antimicrobial Class")
ylab("Percent Resistant Isolates")
ggtitle("Antimicrobial Resistance: 2012-2017")
theme(plot.title = element_text(hjust = 0.25))
theme_bw()
scale_y_continuous(labels = scales::percent_format(scale = 1), limits = c(0,90))
#scale_fill_manual("Class", values=colors, labels = labels)
scale_fill_manual(values = c("red", "blue"))
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), text
= element_text(size=16), legend.title = element_text(size=14), legend.text = element_text(size = 10))
# Plot 2
# The palette with grey:
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#999999")
ggplot(df) aes(x = Group, y = Percent, fill=Antibiotic)
geom_bar(stat="identity", position=position_dodge())
geom_text(aes(label=paste(round(Percent,1),"%")), vjust=-0.25, color="black",
position = position_dodge(0.9), size=3.5)
xlab("Antimicrobial Class")
ylab("Percent Resistant Isolates")
ggtitle("Antimicrobial Resistance: 2012-2017")
theme(plot.title = element_text(hjust = 0.25))
theme_bw()
scale_y_continuous(labels = scales::percent_format(scale = 1), limits = c(0,90))
scale_fill_manual(values=cbPalette)
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), text
= element_text(size=16), legend.title = element_text(size=14), legend.text = element_text(size = 10))
Участок 1
Участок 2
Данные:
df <- structure(list(Antibiotic = c("A", "A", "B", "B", "C", "C", "F",
"F", "Mo", "Mo", "Pen", "Pen", "Ph", "Ph"), `Number of Isolates` = c(176,
140, 117, 105, 117, 103, 174, 137, 11, 2, 147, 110, 164, 134),
Percent = c(82.242991, 89.74359, 54.672897, 67.307692, 54.672897,
66.025641, 81.308411, 87.820513, 5.140187, 1.282051, 68.691589,
70.512821, 76.635514, 85.897436), Group = c("Before", "After",
"Before", "After", "Before", "After", "Before", "After",
"Before", "After", "Before", "After", "Before", "After")), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), spec = structure(list(
cols = list(Antibiotic = structure(list(), class = c("collector_character",
"collector")), `Number of Isolates` = structure(list(), class = c("collector_double",
"collector")), Percent = structure(list(), class = c("collector_double",
"collector")), Group = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))