如何在没有手动限制的情况下在一侧扩展 ggplot 条形比例而不在另一侧扩展
How expand ggplot bar scale on one side but not the other without manual limits
目标是去除刻度线和条形底部之间的 space,而不切断条形另一端以外的任何百分比标签。
我是 运行 数十个使用 R 的 ggplot2 的条形图,并试图遵循我们的组织风格指南,该指南是使用 Excel 为每个图表手动开发的。最大长度条在不同图表中的长度不同,并且可能随着源数据的变化而变化,所以我不想手动设置限制。 [也许这里有一个解决方法:有没有一种方法可以根据输入自动调整限制?]
我已经咨询过:
Removing negative plot area in ggplot2
How to remove space between axis & area-plot in ggplot2?
Force the origin to start at 0 in ggplot2 (R)
http://docs.ggplot2.org/dev/vignettes/themes.html
一个几乎可以工作的图表是从下面的代码生成的。出于 public 目的,我使用了 MASS 包中的 "quine" 数据集。首先,我按年龄分组查找女性百分比。然后我按女性百分比对年龄组进行排序。
require(MASS)
attach(quine)
p.SexAge <- prop.table(table(Sex, Age), 2)
perc.SexAge <- round(p.SexAge * 100)
perc.SexAge.flattened <- as.data.frame(perc.SexAge)
perc.SexAge.flattened.F <- subset(perc.SexAge.flattened, Sex == "F")
require(ggplot2)
ggplot(data=perc.SexAge.flattened.F, aes(x=reorder(Age, -Freq), y=Freq)) +
geom_bar(stat="identity", fill = "#00ABE1") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0,6)) +
ggtitle("Percent Female By Age") +
ylab("Percent Female") +
xlab("Age Group\n") +
#theme_classic() +
theme(plot.margin = unit(c(0,0,0,0), "in")) +
coord_flip() +
geom_text(aes(label = Freq), vjust = 0.4, hjust = - 0.4, size = 3.5)
当取消注释 theme_classic()
以创建空白 space 以满足我们的样式指南时,很明显垂直轴刻度线和底部之间存在过多的 space酒吧。如果有更多条(未显示),这个问题会变得更糟。
如果我将 scale_y_continuous(expand = c(0,6))
更改为
scale_y_continuous(expand = c(0,0))
,
标签在最长的条上被切掉,
违反组织风格指南。
注意:expand
的实现将随着即将发布的 ggplot2
版本 2.3.0 而改变,两端都将提供灵活性。以下答案将继续有效,但不再需要。见 ?expand_scale
.
expand
不会成为你的朋友,因为这两个参数是双方的乘法和加法扩展常数。所以 c(0, 6)
总是会在每边添加 6 个单位。连续数据的默认值是 c(0.05, 0)
,两端各增加 5%。
我们可以预先计算所需的范围。左边界应始终设置为 0,右边界我们设置为 max + 6。(如果绘图之间的范围变化很大,您也可以使用乘法因子。)
lim <- c(0, max(perc.SexAge.flattened.F$Freq) + 6)
#lim <- c(0, max(perc.SexAge.flattened.F$Freq) * 1.1) # 10% increase
ggplot(data=perc.SexAge.flattened.F, aes(x=reorder(Age, -Freq), y=Freq)) +
geom_bar(stat="identity", fill = "#00ABE1") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0), limits = lim) + #This changed!
ggtitle("Percent Female By Age") +
ylab("Percent Female") +
xlab("Age Group\n") +
theme_classic() +
theme(plot.margin = unit(c(0,0,0,0), "in")) +
coord_flip() +
geom_text(aes(label = Freq), vjust = 0.4, hjust = - 0.4, size = 3.5)
p.s。请不要使用 attach
,尤其是在其他人加载到他们的环境中的代码上 。
在 ggplot2 版本 3.3.3 中,scale_y_continuous(expand = expansion(mult = c(0, .1)))
完成了这项工作。这只会将 Y 轴的右端延伸 10% (.1)。您还可以将该端扩展固定数量:例如,scale_y_continuous(expand = expansion(add = c(0, 5)))
将其扩展 5 个单位 space。
目标是去除刻度线和条形底部之间的 space,而不切断条形另一端以外的任何百分比标签。
我是 运行 数十个使用 R 的 ggplot2 的条形图,并试图遵循我们的组织风格指南,该指南是使用 Excel 为每个图表手动开发的。最大长度条在不同图表中的长度不同,并且可能随着源数据的变化而变化,所以我不想手动设置限制。 [也许这里有一个解决方法:有没有一种方法可以根据输入自动调整限制?]
我已经咨询过:
Removing negative plot area in ggplot2
How to remove space between axis & area-plot in ggplot2?
Force the origin to start at 0 in ggplot2 (R)
http://docs.ggplot2.org/dev/vignettes/themes.html
一个几乎可以工作的图表是从下面的代码生成的。出于 public 目的,我使用了 MASS 包中的 "quine" 数据集。首先,我按年龄分组查找女性百分比。然后我按女性百分比对年龄组进行排序。
require(MASS)
attach(quine)
p.SexAge <- prop.table(table(Sex, Age), 2)
perc.SexAge <- round(p.SexAge * 100)
perc.SexAge.flattened <- as.data.frame(perc.SexAge)
perc.SexAge.flattened.F <- subset(perc.SexAge.flattened, Sex == "F")
require(ggplot2)
ggplot(data=perc.SexAge.flattened.F, aes(x=reorder(Age, -Freq), y=Freq)) +
geom_bar(stat="identity", fill = "#00ABE1") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0,6)) +
ggtitle("Percent Female By Age") +
ylab("Percent Female") +
xlab("Age Group\n") +
#theme_classic() +
theme(plot.margin = unit(c(0,0,0,0), "in")) +
coord_flip() +
geom_text(aes(label = Freq), vjust = 0.4, hjust = - 0.4, size = 3.5)
当取消注释 theme_classic()
以创建空白 space 以满足我们的样式指南时,很明显垂直轴刻度线和底部之间存在过多的 space酒吧。如果有更多条(未显示),这个问题会变得更糟。
如果我将 scale_y_continuous(expand = c(0,6))
更改为
scale_y_continuous(expand = c(0,0))
,
标签在最长的条上被切掉,
违反组织风格指南。
注意:expand
的实现将随着即将发布的 ggplot2
版本 2.3.0 而改变,两端都将提供灵活性。以下答案将继续有效,但不再需要。见 ?expand_scale
.
expand
不会成为你的朋友,因为这两个参数是双方的乘法和加法扩展常数。所以 c(0, 6)
总是会在每边添加 6 个单位。连续数据的默认值是 c(0.05, 0)
,两端各增加 5%。
我们可以预先计算所需的范围。左边界应始终设置为 0,右边界我们设置为 max + 6。(如果绘图之间的范围变化很大,您也可以使用乘法因子。)
lim <- c(0, max(perc.SexAge.flattened.F$Freq) + 6)
#lim <- c(0, max(perc.SexAge.flattened.F$Freq) * 1.1) # 10% increase
ggplot(data=perc.SexAge.flattened.F, aes(x=reorder(Age, -Freq), y=Freq)) +
geom_bar(stat="identity", fill = "#00ABE1") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0), limits = lim) + #This changed!
ggtitle("Percent Female By Age") +
ylab("Percent Female") +
xlab("Age Group\n") +
theme_classic() +
theme(plot.margin = unit(c(0,0,0,0), "in")) +
coord_flip() +
geom_text(aes(label = Freq), vjust = 0.4, hjust = - 0.4, size = 3.5)
p.s。请不要使用 attach
,尤其是在其他人加载到他们的环境中的代码上 。
在 ggplot2 版本 3.3.3 中,scale_y_continuous(expand = expansion(mult = c(0, .1)))
完成了这项工作。这只会将 Y 轴的右端延伸 10% (.1)。您还可以将该端扩展固定数量:例如,scale_y_continuous(expand = expansion(add = c(0, 5)))
将其扩展 5 个单位 space。