#r #ggplot2
#r #ggplot2
Вопрос:
У меня есть следующий набор данных ( graph_data
):
# A tibble: 18 x 7
construction phase group mean se se_top se_bottom
<chr> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
1 hacer pre-test heritage 7.67 3.67 11.3 4
2 hacer treatment heritage 15.5 3.00 18.5 12.5
3 hacer post-test heritage 9.83 4.25 14.1 5.58
4 acc pre-test heritage 0.166 0.166 0.332 0
5 acc treatment heritage 4.33 2.67 7.00 1.67
6 acc post-test heritage 0.166 0.166 0.332 0
7 spe pre-test heritage 2.33 1.36 3.69 0.975
8 spe treatment heritage 6.67 2.69 9.36 3.98
9 spe post-test heritage 0.833 0.477 1.31 0.356
10 hacer pre-test monolingual 1 0.707 1.71 0.293
11 hacer treatment monolingual 1 0.577 1.58 0.423
12 hacer post-test monolingual 0.25 0.25 0.5 0
13 acc pre-test monolingual 0 0 0 0
14 acc treatment monolingual 1 0.577 1.58 0.423
15 acc post-test monolingual 0 0 0 0
16 spe pre-test monolingual 4 3.37 7.37 0.634
17 spe treatment monolingual 15.8 2.36 18.1 13.4
18 spe post-test monolingual 3.5 3.18 6.68 0.325
Я хочу создать гистограмму, используя ggplot2
следующие условия:
- ось y:
mean
- ось x:
phase
construction
- фасет:
group
- полосы ошибок: стандартная ошибка (
se_top
,se_bottom
)
Я использовал этот код:
graph_data %>%
ggplot(aes(graph_data, x=phase, y=mean, fill =phase))
geom_bar(stat = "identity", color = "black", position = "dodge")
scale_y_continuous(limits = c(0,20))
labs(x = "Phase", y = "Average number of targets produced")
facet_wrap( ~ group)
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9))
theme(text = element_text(size=20))
theme_classic() scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57"))
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
Однако на графике, который я получаю, столбцы накладываются друг на друга, хотя я использовал position = "dodge"
в своем коде:
Что я должен изменить, чтобы получить желаемый график?
Ответ №1:
Может быть, это может быть полезно:
library(ggplot2)
#Code
graph_data %>%
ggplot(aes(graph_data, x=interaction(phase,construction), y=mean, fill =phase,group=group))
geom_bar(stat = "identity", color = "black", position = position_dodge(0.9))
scale_y_continuous(limits = c(0,20))
labs(x = "Phase", y = "Average number of targets produced")
facet_wrap( ~ group)
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9))
theme(text = element_text(size=20))
theme_classic() scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57"))
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
Вывод:
Обновление: чтобы получить некоторый порядок, попробуйте это:
#Code 2
graph_data %>%
mutate(phase=factor(phase,levels = c('pre-test','treatment','post-test'),
ordered = T)) %>%
arrange(phase) %>%
mutate(conc=paste(as.character(phase),construction),
conc=factor(conc,levels = unique(conc),ordered = T)) %>%
ggplot(aes(graph_data, x=conc, y=mean, fill =phase,group=group))
geom_bar(stat = "identity", color = "black", position = position_dodge(0.9))
scale_y_continuous(limits = c(0,20))
labs(x = "Phase", y = "Average number of targets produced")
facet_wrap( ~ group)
geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
position=position_dodge(.9))
theme(text = element_text(size=20))
theme_classic() scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57"))
theme(axis.text=element_text(size=16),
axis.title=element_text(size=15,face="bold"),
axis.text.x = element_text(angle=45, hjust = 1),
legend.position = "none")
Вывод:
Комментарии:
1. Да, это работает, но столбцы беспорядочные (не по порядку). Я хотел бы, чтобы все столбцы относились к одной и той же фазе и конструкции вместе.
2. @aprendiz Я добавил обновление, надеюсь, это поможет и это то, чего вы ожидаете!