简体   繁体   English

根据原始数据使用barplot()在R中创建分组条形图

[英]Creating a grouped bar plot in R using barplot() from raw data

I have the following data: 我有以下数据:

         CT         VT          TT
A*      5.923076923 6.529411765 5.305555556
Not A*  5.555555556 6.434782609 5.352941176

I want to make a grouped bar chart in R from the data such that the grouping is on A* and Not A* , the x-axis ticks are CT , VT and TT and the numeric values are plotted in the y-direction . 我想根据数据在R中创建分组的条形图,以使分组位于A*Not A*x-axis刻度线为CTVTTT ,数值y-direction绘制。

What do I need to do to produce the bar plot from this raw .csv data? 我需要怎么做才能从原始的.csv数据生成条形图?

Next time, you should provide a reproducible example, but I use ggplot2 to create the desired bar plot: 下次,您应该提供一个可重现的示例,但是我使用ggplot2创建所需的条形图:

Before jumping into the main body, make sure you have the required packages installed as follows: 跳到主体之前,请确保您已按以下步骤安装了必需的软件包:

install.packages(c("ggplot2","data.table"))

Now for a stacked bar chart: 现在查看堆积的条形图:

require(ggplot2)
require(data.table)
data <- data.frame(CT = c( 5.923076923 ,5.555555556), 
                   VT = c(6.529411765,6.434782609), 
                   TT = c(5.305555556, 5.352941176))
rownames(data) <- c("A*", "Not A*")

long_format <- melt(as.matrix(data))

ggplot(long_format, aes(x = Var2,
                        y = value,
                        fill = Var1)) + 
    geom_col()

A grouped bar chart: 分组条形图:

ggplot(data = long_format, 
       aes(x = Var2, 
           y = value,
           fill = Var1)) + 
    geom_bar(position = "dodge", 
             stat = "identity")

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

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