Reputation: 595
I am trying to create a plot like below. Especially, How do I custermize the x
axis following the example figure?
I used the code below and got a different plot. Raw data can be found in the code chunck.
pplotcoef1 <- ggplot(plotcoef1, aes(x=rowname, y=Estimate, fill = Season)) +
geom_bar(stat="identity", position=position_dodge(width = 0.9)) +
scale_x_discrete(limits=c('FFrez','0-18','18-23','>23',
'WFrez','0-10','10-19','>19',
'SFrez','0-25','25-31','>25')) +
geom_errorbar(aes(ymin=`2.5 %`, ymax=`97.5 %`), width=.2,
position=position_dodge(.9)) +
theme_classic() +
theme(legend.position = c(0.1, 0.8)) +
theme(axis.title = element_text(size = 10),
axis.text.x = element_text(size = 10),
axis.text.y = element_text(size = 10))
# below is the raw data
structure(list(rowname = c("FFrez", "0-18", "18-23", ">23", "WFrez",
"0-10", "10-19", ">19", "SFrez", "0-25", "25-31", ">25"), Estimate = c(-0.0012003609086715,
-0.00224572693825549, 0.0317249635237625, -0.0309580812988414,
0.0055434323683689, 0.0100720094187078, -0.0426109330603611,
0.767362840735131, -0.119914397214232, -0.0027280282076923, 0.0118992891275962,
-0.038882023304552), `2.5 %` = c(-0.0144483912522908, -0.00394322882833284,
0.0210026254674657, -0.0424119567821081, -3.41088614867324e-05,
0.00540180608554072, -0.0582083846824812, 0.569529275981415,
-0.159785447309533, -0.00451436995685489, 0.00263298667404979,
-0.0533405234131965), `97.5 %` = c(0.0120476694349478, -0.00054822504817814,
0.0424473015800594, -0.0195042058155746, 0.0111209735982245,
0.0147422127518749, -0.027013481438241, 0.965196405488847, -0.0800433471189307,
-0.00094168645852972, 0.0211655915811427, -0.0244235231959076
), Season = c("Fall", "Fall", "Fall", "Fall", "Winter", "Winter",
"Winter", "Winter", "Spring", "Spring", "Spring", "Spring")), row.names = c(NA,
-12L), class = "data.frame")
Forgot to provide the source of the example figure. It was obtained from the paper by Tack et al. (2015).
Upvotes: 1
Views: 72
Reputation: 39613
You can also this approach with facets that emulate a plot as the one you shared:
library(tidyverse)
#Code
plotcoef1 %>%
mutate(rowname=factor(rowname,levels = c('FFrez','0-18','18-23','>23',
'WFrez','0-10','10-19','>19',
'SFrez','0-25','25-31','>25'),
ordered = T)) %>%
ggplot(aes(x=rowname, y=Estimate,group=Season,fill=Season)) +
geom_bar(stat="identity", position=position_dodge(width = 0.9)) +
geom_errorbar(aes(ymin=`2.5 %`, ymax=`97.5 %`), width=.2,
position=position_dodge(.9)) +
scale_fill_manual(values=c('blue','yellow','red'))+
facet_wrap(.~Season,scales='free_x',drop = T,strip.position = 'bottom')+
theme_classic() +
theme(legend.position = 'top',
axis.title = element_text(color='black',face='bold',size = 10),
axis.text.x = element_text(color='black',face='bold',size = 10),
axis.text.y = element_text(color='black',face='bold',size = 10),
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.placement = "outside",
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
strip.text = element_text(color='black',face='bold'),
plot.title = element_text(color='black',face='bold',hjust=0.5))+
ggtitle('My title')
Output:
In order to have the desired order in facets, try this:
#Code 2
plotcoef1 %>%
mutate(rowname=factor(rowname,levels = c('FFrez','0-18','18-23','>23',
'WFrez','0-10','10-19','>19',
'SFrez','0-25','25-31','>25'),
ordered = T)) %>%
mutate(Season=factor(Season,levels = unique(Season),ordered = T)) %>%
ggplot(aes(x=rowname, y=Estimate,group=Season,fill=Season)) +
geom_bar(stat="identity", position=position_dodge(width = 0.9)) +
geom_errorbar(aes(ymin=`2.5 %`, ymax=`97.5 %`), width=.2,
position=position_dodge(.9)) +
scale_fill_manual(values=c('blue','yellow','red'))+
facet_wrap(.~Season,scales='free_x',drop = T,strip.position = 'bottom')+
theme_classic() +
theme(legend.position = 'top',
axis.title = element_text(color='black',face='bold',size = 10),
axis.text.x = element_text(color='black',face='bold',size = 10),
axis.text.y = element_text(color='black',face='bold',size = 10),
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.placement = "outside",
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
strip.text = element_text(color='black',face='bold'),
plot.title = element_text(color='black',face='bold',hjust=0.5))+
ggtitle('My title')
Output:
Upvotes: 2
Reputation: 480
If okay for you, you could use facets
. To order the bars within the plot you have to set factors first. And also I deleted fill
plotcoef1$rowname = factor(
plotcoef1$rowname,
c(
'FFrez',
'0-18',
'18-23',
'>23',
'WFrez',
'0-10',
'10-19',
'>19',
'SFrez',
'0-25',
'25-31',
'>25'
)
)
ggplot(plotcoef1, aes(x = rowname, y = Estimate)) +
geom_bar(stat = "identity")+
geom_errorbar(aes(ymin=`2.5 %`, ymax=`97.5 %`), width=.2,
position=position_dodge(.9)) +
theme_classic() +
facet_grid(. ~ Season, scales = "free_x")
Upvotes: 1