ggplot2 geom_bar:中心条
ggplot2 geom_bar: Centering bars
对于 ggplot2 问题的帮助,我将不胜感激。我一直在周围寻找很长时间,所以现在我最好的机会来了。我提供了一个可重现的脚本。
我需要做的是将条形水平居中。目前,它们偏向两边——尤其是在 2009 年和 2012 年的柱线上。
将它们居中可以让我自由调整它们的宽度,而现在我不能。
这是当前的独立代码:
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 )
--非常感谢
如果删除 theme
内容,您会看到问题所在,X 轴是年,它试图在该年的 x 值上绘制每个年。解决此问题的一种方法是将 scales
参数设置为 facet_grid
为 "free_x"
.
facet_grid(list('Busy', 'Year'), scales="free_x")
然后您只需调整条形的大小即可。
geom_bar(width=0.5, stat = "identity", position = "dodge")
您只需将 scales = 'free_x'
添加到您的脚本中。您也可以将 geom_bar(stat = 'identity')
替换为 geom_col()
相关信息摘自?facet_grid
scales
:尺度是在所有方面共享(默认,"fixed"),还是它们在行("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).
由 reprex package (v0.2.1.9000)
创建于 2018-09-20
对于 ggplot2 问题的帮助,我将不胜感激。我一直在周围寻找很长时间,所以现在我最好的机会来了。我提供了一个可重现的脚本。
我需要做的是将条形水平居中。目前,它们偏向两边——尤其是在 2009 年和 2012 年的柱线上。
将它们居中可以让我自由调整它们的宽度,而现在我不能。
这是当前的独立代码:
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 )
--非常感谢
如果删除 theme
内容,您会看到问题所在,X 轴是年,它试图在该年的 x 值上绘制每个年。解决此问题的一种方法是将 scales
参数设置为 facet_grid
为 "free_x"
.
facet_grid(list('Busy', 'Year'), scales="free_x")
然后您只需调整条形的大小即可。
geom_bar(width=0.5, stat = "identity", position = "dodge")
您只需将 scales = 'free_x'
添加到您的脚本中。您也可以将 geom_bar(stat = 'identity')
替换为 geom_col()
相关信息摘自?facet_grid
scales
:尺度是在所有方面共享(默认,"fixed"),还是它们在行("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).
由 reprex package (v0.2.1.9000)
创建于 2018-09-20