条形图中条形之间的特定空间 - ggplot2 - R
Specific spaces between bars in a barplot - ggplot2 - R
我有一个简单的条形图,如下所示
a<-data.frame(x=c("total","male","female","low education",
"mid education","high education","working","not working"),
y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))
ggplot(a,aes(x,y)) +
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw()
现在,因为 x 轴的水平(翻转,现在看起来像 y)彼此有关系,例如男性和女性代表性别崩溃,工作和不工作代表另一个崩溃等,我希望轴留下一些 space 在每个细分之间以指出这些细分。
我用 scale_x_discrete
和它的参数 break 做了一些尝试,但似乎不是这样。
有任何想法吗 ?
a<-data.frame(x=c("total","male","female","low education","mid education","high education","working","not working"),y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))
a$rel = c("a", "a", "a", "b", "b", "b", "c", "c") # set groups
ggplot(a, aes(rel, y, fill = factor(x))) +
geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))
我不知道有什么方法可以在条形图中的条形之间设置不同的距离。但是,您可以在组之间添加高度为 0 且没有标签的条,如下所示:
a<-data.frame(x=c("total","a","male","female","b","low education",
"mid education","high education","c","working","not working"),
y=c(80,0,30,50,0,20,40,20,0,65,35))
a$x<-factor(a$x,levels=unique(a$x))
ggplot(a,aes(x,y)) +
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw() +
scale_x_discrete(breaks=a$x[nchar(as.character(a$x))!=1])
一些备注:
a$x
是一开始的字符,所以不需要调用as.character
。
- 它只有在每个 "empty" 条具有不同标签的情况下才有效。这就是为什么我选择了三个不同的字母。
scale_x_discrete
用于抑制标签和刻度线。
结果如下:
如果您指定 scale_x_discrete(或 y),您可以简单地在任何您希望 space 出现在限制和标签语句中的地方添加一个“”。这类似于第一个答案,但您不必向数据集添加零值。
例如该数据集只有 8 个条,但它们被分成两组,每组四个。
scale_x_discrete(
limits=c("BMayC","UMayC","BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"),
labels=c("BMayC","UMayC","BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"))
我有一个简单的条形图,如下所示
a<-data.frame(x=c("total","male","female","low education",
"mid education","high education","working","not working"),
y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))
ggplot(a,aes(x,y)) +
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw()
我用 scale_x_discrete
和它的参数 break 做了一些尝试,但似乎不是这样。
有任何想法吗 ?
a<-data.frame(x=c("total","male","female","low education","mid education","high education","working","not working"),y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))
a$rel = c("a", "a", "a", "b", "b", "b", "c", "c") # set groups
ggplot(a, aes(rel, y, fill = factor(x))) +
geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))
我不知道有什么方法可以在条形图中的条形之间设置不同的距离。但是,您可以在组之间添加高度为 0 且没有标签的条,如下所示:
a<-data.frame(x=c("total","a","male","female","b","low education",
"mid education","high education","c","working","not working"),
y=c(80,0,30,50,0,20,40,20,0,65,35))
a$x<-factor(a$x,levels=unique(a$x))
ggplot(a,aes(x,y)) +
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw() +
scale_x_discrete(breaks=a$x[nchar(as.character(a$x))!=1])
一些备注:
a$x
是一开始的字符,所以不需要调用as.character
。- 它只有在每个 "empty" 条具有不同标签的情况下才有效。这就是为什么我选择了三个不同的字母。
scale_x_discrete
用于抑制标签和刻度线。
结果如下:
如果您指定 scale_x_discrete(或 y),您可以简单地在任何您希望 space 出现在限制和标签语句中的地方添加一个“”。这类似于第一个答案,但您不必向数据集添加零值。
例如该数据集只有 8 个条,但它们被分成两组,每组四个。
scale_x_discrete( limits=c("BMayC","UMayC","BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"), labels=c("BMayC","UMayC","BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"))