简体   繁体   English

使用 ggplot2 对条进行分组

[英]Grouping bars with ggplot2

I have one table.我有一张桌子。 With this table I want to plot bar plot.So below you can see data:用这张表我想绘制条形图。所以下面你可以看到数据:

MEASURE_ERROR_TS_MODEL<-structure(list(Model = structure(c(6L, 8L, 1L, 9L, 5L, 7L, 2L, 
3L, 10L, 4L), .Label = c("ARIMA", "CART", "CTREE", "Ensamble_ML", 
"Ensamble_SM", "ETS", "LM", "STL", "TBATS", "XGBoost"), class = "factor"), 
    RMSE = c(12.89, 6.08, 11.46, 5.23, 4.92, 4.86, 4.78, 4.65, 
    4.64, 3.532698), MAE = c(11.79, 4.7, 8.57, 4, 4.09, 2.97, 
    3.05, 2.84, 3.05, 2.208528), Approach = structure(c(2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Machine learning", 
    "Statistical modelling"), class = "factor")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

So next step is to plot this data into barplot.So in order to do this I try with this code but plot is not like I want to be所以下一步是将这些数据绘制成条形图。所以为了做到这一点,我尝试使用这段代码,但情节并不像我想要的那样

gplot(data=MEASURE_ERROR_TS_MODEL, aes(x=Model, y=MAE, fill=Approach),palette = c("#0073C2FF", "#FC4E07")) +
  geom_bar(
    stat="identity",
           position=position_dodge(), colour="black")

From chart below you can see that this bars are not grouped and I want to make group (eg green bars from left side while red bars on right side).So can anybody help me ho to make this ?从下面的图表中,您可以看到这些条形没有分组,我想分组(例如左侧的绿色条形,右侧的红色条形)。那么有人可以帮我做这个吗?

Perhaps you can try facets :也许你可以尝试 facets :

library(ggplot2)
ggplot(data=MEASURE_ERROR_TS_MODEL, aes(x=Model, y=MAE, fill=Approach)) +
  geom_bar(stat="identity",position=position_dodge()) + 
  facet_wrap(.~Approach, scales = 'free')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM