如何扩大ggplot酒吧一方面,而不是没有手动限制的其他规模 [英] How expand ggplot bar scale on one side but not the other without manual limits

查看:141
本文介绍了如何扩大ggplot酒吧一方面,而不是没有手动限制的其他规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标是摆脱勾号和条形底部之间的空间,而不会切断条形另一端以外的任何百分比标签。



我使用R的ggplot2运行数十个条形图并试图遵循我们的组织风格指南,这是使用Excel为每个图形手动开发的。不同图形中的最大长度条长度不同,并且可能随着源数据更改而变化,所以我不想手动设置限制。 [也许这里有一个解决方法:是否有一种方法可以根据输入自动调整限制?]

我已经咨询过:



>未注释创建空白空间以满足我们的风格指南,但很明显,垂直轴刻度线和条形底部之间存在过多空间。如果有更多的酒吧(未显示),这个问题变得更糟糕。





如果我将 scale_y_continuous(expand = c(0,6))更改为
scale_y_continuous(expand = c(0 ,0))
标签在最长的栏上被切掉,
违反组织风格指南。



解决方案

expand 不会成为你的朋友,因为这两个参数是双方乘法和加法扩展常量。所以 c(0,6)总是会在每边增加6个单位。连续数据的默认值是 c(0.05,0),这两端的数值增加了5%。

我们可以预先计算所需的范围。左边界应始终设置为0,我们将其设置为max + 6的右边界。(如果区域之间的范围非常可变,您也可以使用乘法因子。)



(0,max(perc.SexAge.flattened.F $ Freq)+ 6)
#lim< -c(0,max( perc.SexAge.flattened.F $ Freq)* 1.1)#10%增加

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(按年龄分列的女性百分比)+
ylab(女性百分比)+
xlab(年龄组\)+
theme_classic()+
主题(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)

a>



ps请勿使用 attach ,尤其是在其他人加载到他们的环境中的代码中。


The goal is to get rid of the space between the tick marks and the base of the bars without cutting off any of the percentage labels beyond the other end of the bars.

I am running dozens of bar graphs using R's ggplot2 and trying to follow our organizational style guide, which was developed using Excel manually for each graph. The maximum length bars are of different lengths in the different graphs and could change as the source data changes, so I don't want to manually set limits. [Perhaps there is a workaround here: is there a way to automatically adjust limits depending on the input?]

I have already consulted:

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

A graph which almost works is generated from the following code. For public purposes I'm using the "quine" dataset from the MASS package. First I find percentages female by age grouping. Then I order the age groups by percentage female.

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)

When theme_classic() is uncommented to create empty white space to satisfy our style guide, it is clear that there is excessive space between the vertical axis tick marks and the base of the bars. This problem gets much worse if there are more bars (not shown).

If I change scale_y_continuous(expand = c(0,6)) to scale_y_continuous(expand = c(0,0)), the label gets chopped off on the longest bar, violating the organizational style guide.

解决方案

expand isn't going to be your friend, as the two arguments are multiplicative and additive expansion constants for both sides. So c(0, 6) will always add 6 units on each side. The default for continuous data is c(0.05, 0) which is 5% range increase on either end.

We can pre-calculate the required range instead. The left boundary should always be set to 0, the right one we set to max + 6. (You could also use a multiplicative factor if the range is very variable between plots.)

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. Please don't use attach, especially on code that others load into their environments.

这篇关于如何扩大ggplot酒吧一方面,而不是没有手动限制的其他规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆