【问题标题】:Bar plot for two column of a data frame, side by side数据框两列的条形图,并排
【发布时间】:2022-11-23 23:38:11
【问题描述】:

对于下面的数据,描述了某个模型的训练集和测试集中出现的癌症类型:

structure(list(cancer_type = c("Breast", "colorectal", "Melanoma", 
"Lung Cancer", "Renal Cell Carcinoma", "Sarcoma", "Thymic Carcinoma", 
"Bladder Cancer"), train_set = c(11, 4, 321, 28, 527, 5, 5, 242
), test_set = c(0, 2, 108, 13, 174, 0, 5, 77)), class = "data.frame", row.names = c(NA, 
-8L))

我需要一个条形图来并排绘制,如下所示:

【问题讨论】:

  • 你有没有尝试过?您到底在哪里遇到问题?

标签: r ggplot2 bar-chart


【解决方案1】:

你可以试试,

library(tidyverse)

df %>% 
 pivot_longer(-1) %>% 
 ggplot(aes(x = cancer_type, y = value, fill = name)) + 
 geom_bar(stat = 'identity', position = 'dodge')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2019-02-01
    • 1970-01-01
    相关资源
    最近更新 更多