barplot中的酒吧之间的特定空间 - ggplot2 - R [英] Specific spaces between bars in a barplot - ggplot2 - R
问题描述
a< -data.frame(x = c(total, 男性,女性,低教育,
中等教育,高等教育,工作,不工作),
y = c(80,30,50, (a $ x,levels = unique(a $ x))
a $ x <-as.character(a $ x)
a $ x <-factor
ggplot(a,aes(x,y))+
geom_bar(stat =identity,fill =orange,width = 0.4)+
coord_flip()+
theme_bw()
现在,因为x轴的水平(翻转并且现在看起来像y)彼此具有关系,例如男性和女性代表性别崩溃,工作和不工作代表另一个崩溃等,我想轴在每个细分之间留出一些空间,以指出这些细分。
我已经尝试了一些使用 scale_x_discrete
和它的参数中断但是我似乎这不是它的方式。
任何想法?
我不知道如何在barplot中设置不同的条间距。但是,您可以在组之间添加高度为0和无标签的条,如下所示:
a< -data.frame(x = c(总数,a,男性,女性,b,低等教育,
中等教育,高等教育,c,工作 不工作),
y = c(80,0,30,50,0,20,40,20,0,65,35))
a $ x <-factor(a $ x,水平=独特(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
就可以了。
scale_x_discrete
用于抑制标签和刻度线。 b
结果如下所示:
I have a simple bargraph like the following
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()
Now , because the levels of the x axis (flipped and now seems like y ) have a relation with each other e.g male and female represent sex breakdown , working and not working represent another breakdown etc., I want the axis to leave some space between each breakdown in order to point out these breakdowns.
I have tried some things with scale_x_discrete
and its parameter break but it seems that this is not the way it goes .
Any ideas ?
I don't know of a way to set different distances between bars in a barplot. However, you can add bars with height 0 and no label between the groups as follows:
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])
Some remarks:
a$x
is a character from the start, so there is no need to callas.character
on it.- It only works, if the each "empty" bar has a different label. That's why I chose three different letters.
scale_x_discrete
is used to suppress the labels and tick marks.
The result looks as follows:
这篇关于barplot中的酒吧之间的特定空间 - ggplot2 - R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!