Reputation: 1652
I have two data frames first percentage_of_worker
Country,Factory,worker_efficiency
USA,factory_A,90
Germany,factory_A,93
France,factory_A,90
USA,factory_B,80
Germany,factory_B,94
France,factory_B,91
USA,factory_C,78
Germany,factory_C,95
France,factory_C,92
USA,factory_D,90
Germany,factory_D,50
France,factory_D,36
and percentage_of_factory
Country,Factory,factory_efficiency
USA,factory_A,95
Germany,factory_A,93
France,factory_A,90
USA,factory_B,96
Germany,factory_B,94
France,factory_B,91
USA,factory_C,97
Germany,factory_C,95
France,factory_C,92
USA,factory_D,90
Germany,factory_D,91
France,factory_D,93
I draw bar plot using ggplot for both using this command
factory_plot <- ggplot(percentage_of_factory,aes(Factory,factory_efficiency, fill=Country)) +
geom_bar(stat="identity", position=position_dodge()) + geom_text(aes(label = round(factory_efficiency,1)) , position=position_dodge(width=0.9), vjust=-0.25, size = 3) +
labs(title = "Factory performance percentage", x = "Factory")
worker_plot <- ggplot(percentage_of_worker,aes(Factory,worker_efficiency, fill=Country)) +
geom_bar(stat="identity", position=position_dodge()) + geom_text(aes(label = round(worker_efficiency,1)) , position=position_dodge(width=0.9), vjust=-0.25, size = 3) +
labs(title = "worker performance percentage", x = "Factory")
I have those graphs
I would like to combine them in one graph with the same axes where column of efficiency beside each other to show the difference between the factory ans worker efficiency: for example in case of factory_D it will be factory efficiency for France "with red" is 93% followed by the worker efficiency 36% and Germany 91% for factory followed by 50% for worker etc.. for all factories
what I tried is:
c <- percentage_of_factory
c$name <- "factory"
colnames(c)[3] <- "Efficiency"
p <- percentage_of_worker
p$name <- "worker"
colnames(p)[3] <- "Efficiency"
l <- rbind(c, p)
ggplot(l, aes(x=Factory, y=Efficiency , group=Country, fill=Country, colour=name)) +
geom_bar(stat="identity", position="dodge") + scale_colour_brewer(palette = "Set1")
but it gave this graph and it is not what I need
Help would be appreciated
Upvotes: 1
Views: 2183
Reputation: 4761
To do that you need to combine your 2 dataframes:
df1=read.table(text="Country,Factory,worker_efficiency
USA,factory_A,90
Germany,factory_A,93
France,factory_A,90
USA,factory_B,80
Germany,factory_B,94
France,factory_B,91
USA,factory_C,78
Germany,factory_C,95
France,factory_C,92
USA,factory_D,90
Germany,factory_D,50
France,factory_D,36",sep=",",header=T)
df2=read.table(text="Country,Factory,factory_efficiency
USA,factory_A,95
Germany,factory_A,93
France,factory_A,90
USA,factory_B,96
Germany,factory_B,94
France,factory_B,91
USA,factory_C,97
Germany,factory_C,95
France,factory_C,92
USA,factory_D,90
Germany,factory_D,91
France,factory_D,93",sep=",",header=T)
Because we want to keep track of which one is for worker/factory I add an extra column type
to both df:
df1$type<-"worker"
df2$type<-"factory"
We then merge them and to make it easy the third column is renamed to a similar name : efficiency
colnames(df1)[3]<-"efficiency"
colnames(df2)[3]<-"efficiency"
We bind them with rbind()
(or bind_rows
if you're used to dplyr
)
dftot=rbind(df1,df2)
Using ggplot2
and interaction()
in the grouping we plot the histogram
ggplot(dftot)+geom_bar(aes(x=Factory,y=efficiency,group=interaction(type,Country),fill=Country,alpha=type),position="dodge",colour="grey",stat="identity")+ scale_alpha_discrete(range= c(0.5, 1))
Following Axeman's suggestion, I added alpha
to differentiate worker from factory and scale_alpha_discrete(range= c(0.5, 1))
which allows to set the alphas manually.
coulour="grey"
is optional, it adds a contour.
Note: the order of the factor in interaction()
matters, if you do interaction(Country,type) you will have 3 bars for the factory followed by 3 bars for the workers.
Upvotes: 1
Reputation: 35297
Using @Haboryme's data prep (thanks!):
ggplot(dftot, aes(x = type, y = efficiency, group = Country, fill = Country))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Factory) +
theme(axis.title.x = element_blank())
Or:
ggplot(dftot, aes(x = Factory, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Country) +
theme(axis.title.x = element_blank())
Or:
ggplot(dftot, aes(x = Country, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Factory) +
theme(axis.title.x = element_blank())
Upvotes: 1