【问题标题】:multivariate bar chart in R ggplotR ggplot中的多元条形图
【发布时间】:2018-03-21 23:18:43
【问题描述】:

我的数据表是这样的:

table_name   expected_value  observed_value   
   <chr>          <dbl>          <dbl>  

 1 table1       95237608       95100229  
 2 table2        3014052        3014052     
 3 table3        3024749        3024749  

我想创建一个多元条形图来比较每个表的expected_valueobserved_value

看起来像这样:

【问题讨论】:

  • 你试过什么?你看过 ggplot2 文档中的例子吗?

标签: r ggplot2 bar-chart data-visualization


【解决方案1】:

首先在 ggplot 中这样做,您必须将格式从宽更改为长,(tidyr::gather) 然后映射美学(选择要映射的变量),最后您必须使用 stat "identity" 而不是默认的“计数”,

library(ggplot2)
library(tidyr)
library(dplyr)
df <- data.frame(table_name =c("a", "b","c"), expected=runif(3), observed=runif(3))
gather(df, key, value, -table_name) %>% ggplot(aes(table_name, value, fill=key)) + geom_bar(stat="identity", position="dodge")

【讨论】:

    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多