我尽力阅读与我的问题相关的问题。最相关的是:question。但是我无法弄清楚如何解决我的问题。我有一个像
这样的数据集 structure(list(COMPANY = structure(1:5, .Label = c("Architecten",
" 2.0", " Adema", "B.V.",
" Alex"), class = "factor"), `YOUR COMPANY PERFORMANCE ORIENTATION` = c(5,
7, 6, 4.5, 4.5), `AVERAGE PERFORMANCE ORIENTATION` = c(5.17,
5.17, 5.17, 5.17, 5.17), `YOUR COMPANY LEARNING ORIENTATION` = c(5,
5.6, 5.8, 6.2, 3.8), `AVERAGE LEARNING ORIENTATION` = c(5.67, 5.67, 5.67, 5.67, 5.67)), .Names = c("COMPANY", "YOUR COMPANY PERFORMANCE ORIENTATION",
"AVERAGE PERFORMANCE ORIENTATION", "YOUR COMPANY LEARNING ORIENTATION",
"AVERAGE LEARNING ORIENTATION"), class = "data.frame", row.names = c(NA,
5L))
通过使用melt(来自library reshape2),我能够获得:
structure(list(COMPANY = structure(c(1L, 2L, 3L, 4L, 5L, 1L,
+ 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), class = "factor", .Label = c("Architecten",
" 2.0", " Adema", "B.V.",
" Alex")), variable = structure(c(1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L), .Label = c("YOUR COMPANY PERFORMANCE ORIENTATION", "AVERAGE PERFORMANCE ORIENTATION",
"YOUR COMPANY LEARNING ORIENTATION", "AVERAGE LEARNING ORIENTATION"
), class = "factor"), value = c(5, 7, 6, 4.5, 4.5, 5.17, 5.17,
5.17, 5.17, 5.17, 5, 5.6, 5.8, 6.2, 3.8, 5.67, 5.67, 5.67, 5.67,
5.67)), .Names = c("COMPANY", "variable", "value"), row.names = c(NA,
-20L), class = "data.frame")
通过使用以下代码,我能够并排绘制平均值和公司变量:
plot_try <-ggplot(mydataset,aes(COMPANY,value,fill=variable))+
geom_bar(stat="identity",position="dodge")+ theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ facet_wrap(~COMPANY)
我想在前两个栏之间添加空格(“你的公司表现取向”和“平均表现取向”)和最后两个栏,但我无法做到这一点。关于如何添加到我的代码的任何建议?谢谢。
答案 0 :(得分:3)
使用您的融合数据框mydataset
并添加新变量,其中包含您需要合并的每两个条形图的分组编号。然后将这个新变量用于x值。
mydataset$group<-rep(c(1,2),each=10)
#as suggested by @Henrik more general solution
mydataset$group <- grepl("LEARNING", mydataset$variable)
ggplot(mydataset,aes(group,value,fill=variable))+
geom_bar(stat="identity",position="dodge")+
theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
facet_wrap(~COMPANY)
答案 1 :(得分:1)
这并没有直接回答你关于栏之间空间的问题,而是提供另一种思考呈现数据的方法。假设df
包含您的原始数据(melt(...)
之前)。
gg <- reshape(df,idvar="COMPANY",
varying=list(c(2,4),c(3,5)), v.names=c("Individual","Average"),
timevar="Metric", times=c("Performance","Learning"),
direction="long")
ggplot(gg)+
geom_point(aes(x=COMPANY,y=Individual))+
geom_hline(aes(yintercept=Average))+
geom_linerange(aes(x=COMPANY,ymin=Average,ymax=Individual,
color=factor(sign(Individual-Average))))+
facet_grid(~Metric) +
labs(x="",y="Scores") +
theme(legend.position="none",
axis.text.x=element_text(angle=90, vjust=0.4, hjust=1, size=15, face="bold"))
产生这个: