I tried my best to read related questions to my issue. The most relevant was: question. However I was not able to figure out how to solve my issue. I had a dataset like
我尽我最大的努力去阅读有关我的问题的相关问题。最相关的是:问题。然而,我却不知道如何解决我的问题。我有一个类似的数据集
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))
By using melt (from library reshape2) I was able to obtain:
通过使用熔体(从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")
By using the following code, I was able to plot the average and company variables side by side:
通过使用下面的代码,我能够将平均变量和公司变量并排绘制出来:
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)
i want to add space between the first two bars ("your company performance orientation" and "average performance orientation") and the last 2 bars, but I am not able to do that. Any suggestion on what to add to my code? Thanks.
我想在前两个小节(“您的公司性能导向”和“平均性能导向”)和最后两个小节之间添加空格,但是我不能这样做。对我的代码添加什么建议?谢谢。
2 个解决方案
#1
3
Use your melted dataframe mydataset
and add new variable that contains grouping number for each two bars you need to combine. Then use this new variable for the x values.
使用您的融化的dataframe mydataset,并添加新的变量,该变量包含需要合并的每两个bar的分组编号。然后为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)
#2
1
This does not directly answer your question about space between the bars, but provides another way to think about presenting your data. Assuming df
has your original data (before melt(...)
).
这并不能直接回答你关于酒吧之间空间的问题,但提供了另一种方式来考虑展示你的数据。假设df有你的原始数据(熔融前(…))。
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"))
Produces this:
生产:
#1
3
Use your melted dataframe mydataset
and add new variable that contains grouping number for each two bars you need to combine. Then use this new variable for the x values.
使用您的融化的dataframe mydataset,并添加新的变量,该变量包含需要合并的每两个bar的分组编号。然后为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)
#2
1
This does not directly answer your question about space between the bars, but provides another way to think about presenting your data. Assuming df
has your original data (before melt(...)
).
这并不能直接回答你关于酒吧之间空间的问题,但提供了另一种方式来考虑展示你的数据。假设df有你的原始数据(熔融前(…))。
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"))
Produces this:
生产: