首页 > 解决方案 > 需要使用 data.frame 中的两列数据制作条形图

问题描述

我正在尝试从 data.frame 中并排制作两列的条形图。我努力了:

barplot(data.frame$data1, data.frame$data2, data=data.frame)

here is data:   
   Neutral Emotional
1   0.790   1.6400
2   0.051   0.0880
3   0.891   2.7200
4   0.430   1.1800
5   -0.009  -0.6000

但它会产生大量的酒吧,而不仅仅是两个。我试图有两个条,一个带有中性条,一个带有代表 SEM 的情感条和误差条。

标签: r

解决方案


一个选项是gather进入“长”格式,然后使用geom_barfromggplot2

library(tidyverse)
library(ggplot2)
gather(df1) %>% 
    ggplot(., aes(x = key, y = value)) +
        geom_bar(stat = 'identity')

如果我们还需要一个错误栏,那么

gather(df1) %>% 
     ggplot(., aes(x = key, y = value)) +
         stat_summary(fun.y = mean, geom = "bar") + 
         stat_summary(fun.data = mean_se, geom = "errorbar")

在此处输入图像描述

数据

df1 <- structure(list(Neutral = c(0.79, 0.051, 0.891, 0.43), Emotional = c(1.64, 
0.088, 2.72, 1.18)), class = "data.frame", row.names = c("1", 
"2", "3", "4"))

推荐阅读