[英]ggplot2 geom_bar: Centering bars
I would really appreciate help with a ggplot2 issue. 我真的很感谢ggplot2问题的帮助。 I've been looking around it for long, so my best chance is now here.
我一直在寻找它,所以现在最好的机会就在这里。 I provide a reproducible script.
我提供了一个可复制的脚本。
What I'd need to do is center the bars horizontally. 我需要做的是将横条水平居中。 For now, they polarise towards the sides--especially notably on the 2009 and 2012 bars.
就目前而言,它们两极分化-特别是在2009年和2012年的酒吧上。
Centering them would allow me to freely adjust their width, whereas now I cannot. 将它们居中可使我自由地调整其宽度,而现在我不能。
This is the current, self-contained code: 这是当前的独立代码:
install.packages('ggplot2')
library(ggplot2)
# Data
Year = c('2009','2009','2009','2009','2010','2010','2010','2010','2011','2011','2011','2011','2012','2012','2012','2012')
City = c('Kaunas','Vilnius','Kaunas','Vilnius','Kaunas','Vilnius','Kaunas','Vilnius',
'Kaunas','Vilnius','Kaunas','Vilnius','Kaunas','Vilnius','Kaunas','Vilnius')
Busy = c('Peak', 'Peak', 'Off-peak', 'Off-peak', 'Peak', 'Peak', 'Off-peak', 'Off-peak', 'Peak', 'Peak', 'Off-peak',
'Off-peak', 'Peak', 'Peak', 'Off-peak', 'Off-peak')
Result = c(-0.417277550641143, 0.540415934506096, -0.887323992801467, -1.0016151369497, -1.69512596882652,
-0.474180286913426, -1.87972395432173, -1.55647964460481, -1.10385766877877, -3.19261229467907,
-2.11137007243349, -3.24910554029829, -0.949278709972917, -2.22558030912235, -1.3496651478542, -2.59961942855099)
pvalue.asterisks = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, '*', NA, '*', NA, NA, NA, NA)
Results = data.frame(Year, City, Busy, Result, pvalue.asterisks)
str(Results)
Results
# Plot
ggplot(Results, aes(Year, Result, fill=City)) +
ylab(expression(paste(italic('t'), '-value'))) +
facet_grid(list('Busy', 'Year')) + scale_fill_grey(start = 0, end = .6) +
geom_bar(width=2.9, stat = "identity", position = position_dodge(width = 3)) +
scale_y_continuous(expand = c(.035,0)) + scale_x_discrete(expand = c(.7,.4)) +
theme( axis.title.x=element_blank(), axis.text.x = element_blank(),
axis.title.y = element_text(size=19, face="bold", margin = margin(0, 3, 0, 0)),
axis.text.y = element_text(size = 13),
legend.title = element_blank(), legend.text = element_text(size=17),
legend.background = element_rect(fill="white", size=0.1, linetype="solid"),
legend.key.size = unit(1.7,"line"), axis.ticks.x=element_blank(), legend.position = c(.167, .6),
panel.background = element_blank(), plot.title = element_blank(),
legend.margin=margin(c(-.05,.14,.11,.1), unit='cm'),
panel.grid.major.y = element_line(colour = "grey90"),
panel.grid.major.x = element_blank(), plot.margin = unit(c(.05, .1, 0, .1), 'cm'),
strip.text.x = element_text(face='bold', size=15, margin = margin(.2, 0, .2, 0, "cm")),
strip.text.y = element_text(face='bold', size=15, margin = margin(0, .2, 0, .2, "cm")),
strip.background = element_rect(colour="grey48", fill='grey95'), panel.spacing = unit(.08, "lines")) +
geom_text(aes(x=Year, y=Result, label=pvalue.asterisks), vjust=-.01, colour= 'white', size=15, hjust=-.168 )
-- Thank you very much - 非常感谢你
You just need to add scales = 'free_x'
to your script. 您只需要在脚本中添加
scales = 'free_x'
。 You can also replace geom_bar(stat = 'identity')
with geom_col()
您也可以将
geom_bar(stat = 'identity')
替换为geom_col()
Relevant info taken from ?facet_grid
来自
?facet_grid
相关信息
scales
: Are scales shared across all facets (the default, "fixed"), or do they vary across rows ("free_x"), columns ("free_y"), or both rows and columns ("free")? scales
: scales
是否在所有构面之间共享(默认值是“固定”),还是在行(“ free_x”),列(“ free_y”)或行和列(“ free”)之间变化?
p1 <- ggplot(Results, aes(Year, Result, fill=City)) +
ylab(expression(paste(italic('t'), '-value'))) +
facet_grid(rows = vars(Busy), cols = vars(Year), scales = 'free_x') +
scale_fill_grey(start = 0, end = .6) +
geom_col(position = position_dodge(width = 0.9)) +
scale_y_continuous(expand = c(.035,0)) +
scale_x_discrete(expand = c(.7,.4))
p1 +
theme( axis.title.x=element_blank(), axis.text.x = element_blank(),
axis.title.y = element_text(size=19, face="bold", margin = margin(0, 3, 0, 0)),
axis.text.y = element_text(size = 13),
legend.title = element_blank(), legend.text = element_text(size=17),
legend.background = element_rect(fill="white", size=0.1, linetype="solid"),
legend.key.size = unit(1.7,"line"), axis.ticks.x=element_blank(), legend.position = c(.167, .6),
panel.background = element_blank(), plot.title = element_blank(),
legend.margin=margin(c(-.05,.14,.11,.1), unit='cm'),
panel.grid.major.y = element_line(colour = "grey90"),
panel.grid.major.x = element_blank(), plot.margin = unit(c(.05, .1, 0, .1), 'cm'),
strip.text.x = element_text(face='bold', size=15, margin = margin(.2, 0, .2, 0, "cm")),
strip.text.y = element_text(face='bold', size=15, margin = margin(0, .2, 0, .2, "cm")),
strip.background = element_rect(colour="grey48", fill='grey95'), panel.spacing = unit(.08, "lines")) +
geom_text(aes(x=Year, y=Result, label=pvalue.asterisks), vjust=-.01, colour= 'white', size=15, hjust=-.168 )
#> Warning: Removed 14 rows containing missing values (geom_text).
Created on 2018-09-20 by the reprex package (v0.2.1.9000) 由reprex软件包 (v0.2.1.9000)创建于2018-09-20
If you remove the theme
stuff you will see the problem, the X axis is years, and it's attempting to plot each one on the x-value for that year. 如果删除
theme
内容,您将看到问题,X轴为年份,并且它试图将每个年份绘制在该年份的x值上。 One way to fix this is to set the scales
parameter to facet_grid
to be "free_x"
. 解决此问题的一种方法是将
scales
参数设置为facet_grid
为"free_x"
。
facet_grid(list('Busy', 'Year'), scales="free_x")
and then you only have to adjust the sizing of the bars. 然后您只需要调整条的大小即可。
geom_bar(width=0.5, stat = "identity", position = "dodge")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.