将两个数据框组合在一个图中
combine two data frames in one graph
我先有两个数据框
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
和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
我使用 ggplot 为这两个命令绘制条形图
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")
我有那些图表
我想将它们组合在一张图表中,坐标轴相同,其中效率列彼此相邻,以显示工厂效率与工人效率之间的差异:
例如,在 factory_D 的情况下,法国 "with red" 的工厂效率为 93%,其次是工人效率 36%,德国工厂效率为 91%,其次是工人效率为 50% 等等。对于所有工厂
我试过的是:
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")
但它给出了这张图,但它不是我需要的
帮助将不胜感激
为此,您需要合并 2 个数据帧:
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)
因为我们想跟踪哪一个是给 worker/factory 我添加一个额外的列 type
到两个 df:
df1$type<-"worker"
df2$type<-"factory"
然后我们合并它们,为方便起见,将第三列重命名为类似的名称:efficiency
colnames(df1)[3]<-"efficiency"
colnames(df2)[3]<-"efficiency"
我们将它们与 rbind()
绑定(或 bind_rows
,如果您习惯 dplyr
)
dftot=rbind(df1,df2)
在分组中使用 ggplot2
和 interaction()
我们绘制直方图
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))
根据 Axeman 的建议,我添加了 alpha
以区分工人和工厂,以及 scale_alpha_discrete(range= c(0.5, 1))
允许手动设置 alpha。
coulour="grey"
是可选的,它添加了一个轮廓。
注意: interaction()
中因素的顺序很重要,如果你进行交互(国家/地区,类型),你将有 3 个用于工厂的条,然后是 3 个酒吧的工人。
使用@Haboryme 的数据准备(感谢!):
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())
或者:
ggplot(dftot, aes(x = Factory, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Country) +
theme(axis.title.x = element_blank())
或者:
ggplot(dftot, aes(x = Country, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Factory) +
theme(axis.title.x = element_blank())
我先有两个数据框 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
和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
我使用 ggplot 为这两个命令绘制条形图
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")
我有那些图表
我想将它们组合在一张图表中,坐标轴相同,其中效率列彼此相邻,以显示工厂效率与工人效率之间的差异: 例如,在 factory_D 的情况下,法国 "with red" 的工厂效率为 93%,其次是工人效率 36%,德国工厂效率为 91%,其次是工人效率为 50% 等等。对于所有工厂
我试过的是:
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")
但它给出了这张图,但它不是我需要的
帮助将不胜感激
为此,您需要合并 2 个数据帧:
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)
因为我们想跟踪哪一个是给 worker/factory 我添加一个额外的列 type
到两个 df:
df1$type<-"worker"
df2$type<-"factory"
然后我们合并它们,为方便起见,将第三列重命名为类似的名称:efficiency
colnames(df1)[3]<-"efficiency"
colnames(df2)[3]<-"efficiency"
我们将它们与 rbind()
绑定(或 bind_rows
,如果您习惯 dplyr
)
dftot=rbind(df1,df2)
在分组中使用 ggplot2
和 interaction()
我们绘制直方图
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))
根据 Axeman 的建议,我添加了 alpha
以区分工人和工厂,以及 scale_alpha_discrete(range= c(0.5, 1))
允许手动设置 alpha。
coulour="grey"
是可选的,它添加了一个轮廓。
注意: interaction()
中因素的顺序很重要,如果你进行交互(国家/地区,类型),你将有 3 个用于工厂的条,然后是 3 个酒吧的工人。
使用@Haboryme 的数据准备(感谢!):
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())
或者:
ggplot(dftot, aes(x = Factory, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Country) +
theme(axis.title.x = element_blank())
或者:
ggplot(dftot, aes(x = Country, y = efficiency, fill = type))+
geom_bar(position = "dodge", stat = "identity") +
facet_grid(~Factory) +
theme(axis.title.x = element_blank())