有两个因素的分组条形图

时间:2018-10-14 15:27:04

标签: r ggplot2 bar-chart tibble

我想用ggplot2创建一个箱形图,该箱形图在x轴上有一个因子变量,在y轴上有一个数值,并且每个x轴刻度都带有多个条。 这与我正在使用的类似:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;

@Configuration
public class DatasourceConfig {

    @Value("${domain.datasource.url}")
    private String url;

    @Value("${domain.datasource.username}")
    private String username;

    @Value("${domain.datasource.password}")
    private String password;

    @Value("${domain.datasource.type}")
    private String type;

    @Value("${domain.datasource.driver-class}")
    private String driverClass;

    @Bean
    public DataSource dataSource() {
        if (type.equals("MYSQL")) {
            return DataSourceBuilder
                    .create()
                    .username(username)
                    .password(password)
                    .url(url)
                    .driverClassName(driverClass)
                    .build();
        } else {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            return builder
                    .setType(EmbeddedDatabaseType.H2)
                    .build();
        }
    }
}

以下是数据:

A tibble: 26 x 4
     Param Value      Res                   Comp
    <fctr> <dbl>   <fctr>                  <chr>
 1    Par1 13.45      Re1                      a
 2    Par2 10.86      Re1                      a
 3    Par3 10.32      Re2                      a
 4    Par2 23.62      Re2                      a
 5    Par1 19.43      Re2                      a
 6    <NA>  0.00      Re3                      b
 7    <NA>  0.00      Re4                      b
 8    Par1 27.44      Re5                      b
 9    <NA>  0.00      Re6                      b
10    <NA>  0.00      Re6                      b

所以我想在x轴上有Res,在y轴上有Value,并且如果每个Res有多个Params,则应该有不同的列。我还希望在小平面包装中,如果不存在Res,则它不应在x轴上(例如Re1不应在b小平面中)。

现在我已经完成了此操作:

structure(list(Param = c("Par1", "Par2", "Par3", "Par1", 
"Par4", NA, NA, "Par5", NA, NA, "Par6", "Par3", "Par7", "Par8", 
"Par3", "Par3", "Par6", "Par3", "Par7", "Par8", "Par3", "Par3", 
"Par6", "Par3", "Par7", "Par8"), Value = c(13.45, 10.86, 10.32, 
23.62, 19.43, 0, 0, 27.44, 0, 0, 11.37, 37.94, 11.23, 22.8, 19.25, 
22.26, 11.36, 19.03, 11.94, 22.79, 14.22, 17.21, 11.66, 23.93, 
12.33, 23.39), Res = c("Re1", "Re1", "Re2", "Re2", "Re2", 
"Re3", "Re4", "Re5", "Re6", "Re6_DOC", "Re7", "Re7", "Re7", 
"Re7", "Re6", "Re6_1", "Re7", "Re7", "Re7", "Re7", "Re6", 
"Re6_1", "Re7", "Re7", "Re7", "Re7"), Comp = c("Comp1", 
"Comp1", "Comp1", "Comp1", 
"Comp1", "Comp1", "Comp1", 
"Comp1", "Comp2", "Comp2", "Comp2", 
"Comp2", "Comp2", "Comp2", "Comp3", 
"Comp3", "Comp3", "Comp3", "Comp3", "Comp3", 
"Comp4", "Comp4", "Comp4", "Comp4", "Comp4", 
"Comp4")), .Names = c("Param", "Value", "Res", "Comp"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -26L))

但是,每个Res都显示一个条形图,并且在各个构面的x轴上显示空刻度。
有人知道如何解决吗?
谢谢

1 个答案:

答案 0 :(得分:1)

然后是最终图像的代码:

df%>%ggplot(aes(x=Res,y=Value,fill=Param,label=Param))+geom_bar(position=position_dodge2(width = 0.9, preserve = "single"),stat = "identity")+facet_wrap(~Comp,scales = "free_x", as.table = FALSE)+
  geom_text(position = position_dodge(width = 0.9),hjust="top",angle = 90,size=5) + guides(fill=FALSE)+scale_fill_manual(values=brewer.pal(8, "Paired"))+theme(axis.title.x=element_blank())+scale_y_continuous("Percentage")