两个实验在一个条形图上有两个变量

时间:2017-03-22 23:47:37

标签: r bar-chart

我想使用ggplot来创建一个条形图,其中显示了两个实验,其中一个数字上有两个变量。因此,实验应位于不同的窗口中,每个实验的两个变量应显示为单个条形。

该数据:

> dput(data)
structure(c(13336000, 16722900, 8231800, 2275200, 41271100, 99067800, 
            20305100, 26360500, 55416000, 219396000, 71722610, 84034100, 
            230630600, 254380400, 52402800, 50422300, 354388700, 438443760, 
            72343800, 86587600, 462991400, 497156100, 51817210, 65168400, 
            235045400, 290932300, 41362400, 50220300, 168440600, 196797700, 
            107294900, 158465800, 362305500, 375372400, 158236400, 209906300, 
            1367158670, 1122186600, 220191000, 203333900, 978435120, 983735500, 
            373804400, 362937700, 2104276960, 2220222960, 394261700, 410656800, 
            3267607300, 3223143500, 211954000, 271134600, 1080187900, 1410693500, 
            3261120700, 1299253600, 840738200, 1226346700, 1591379300, 1829220400, 
            3239340100, 1758611700, 2003838200, 2346221200, 2425368000, 3150723500, 
            3258315400, 3184999800, 3141234200, 4124976700, 3484620300, 4132765100, 
            3280830100, 4403308600, 2233078100, 4962721300, 2923459800, 4360192600, 
            5238580300, 5647975300), .Dim = c(4L, 20L), .Dimnames = list(
              c("Mark_Mon", "Mark_Tue", "Matt_Mon", "Matt_Tue"
              ), c("1", "2", "3", "4", "6", "8", "9", "10", 
                   "12", "13", "14", "15", "16", "18", "22", "24", "26", 
                   "28", "29", "30")))

期望的输出:

something like this

我想想象每个实验中变量之间的现有差异,以便条形重叠。

如果可能作为第二个输出,我希望看到每个变量的条形彼此相邻,而不是在顶部并重叠。

1 个答案:

答案 0 :(得分:2)

我正在尽力猜测你在这里想要完成的事情。如果你能澄清什么意思,它可能有所帮助。

你有四个变量 - 大值,小值(1-30),日(周一/周二)和人(马特/马克)。假设1-30是您的自变量,而大值是您的响应变量,那么您可以将日期和人物作为附加因子。您可以通过颜色识别其中一个,并通过facet分离另一个。如,

(我必须将数据按到长格式,以便我更容易使用,并使用data.table包。我还将1-30值更改为数字而不是字符串。)

library(data.table)
library(ggplot2)

dat <- structure(c(13336000, 16722900, 8231800, 2275200, 41271100, 99067800, 
            20305100, 26360500, 55416000, 219396000, 71722610, 84034100, 
            230630600, 254380400, 52402800, 50422300, 354388700, 438443760, 
            72343800, 86587600, 462991400, 497156100, 51817210, 65168400, 
            235045400, 290932300, 41362400, 50220300, 168440600, 196797700, 
            107294900, 158465800, 362305500, 375372400, 158236400, 209906300, 
            1367158670, 1122186600, 220191000, 203333900, 978435120, 983735500, 
            373804400, 362937700, 2104276960, 2220222960, 394261700, 410656800, 
            3267607300, 3223143500, 211954000, 271134600, 1080187900, 1410693500, 
            3261120700, 1299253600, 840738200, 1226346700, 1591379300, 1829220400, 
            3239340100, 1758611700, 2003838200, 2346221200, 2425368000, 3150723500, 
            3258315400, 3184999800, 3141234200, 4124976700, 3484620300, 4132765100, 
            3280830100, 4403308600, 2233078100, 4962721300, 2923459800, 4360192600, 
            5238580300, 5647975300), .Dim = c(4L, 20L), .Dimnames = list(
              c("Mark_Mon", "Mark_Tue", "Matt_Mon", "Matt_Tue"
              ), c("1", "2", "3", "4", "6", "8", "9", "10", 
                   "12", "13", "14", "15", "16", "18", "22", "24", "26", 
                   "28", "29", "30")))



dat <- data.table(t(dat))
dat[,n := c(1, 2, 3, 4, 6, 8, 9, 10, 
                   12, 13, 14, 15, 16, 18, 22, 24, 26, 
                   28, 29, 30) ]

dat.mon <- dat[,c("Mark_Mon","Matt_Mon","n")]
setnames(dat.mon, c("Mark","Matt","n"))
dat.mon[,Day := "Mon"]

dat.tue <- dat[,c("Mark_Tue","Matt_Tue","n")]
setnames(dat.tue, c("Mark","Matt","n"))
dat.tue[,Day := "Tue"]

dat.all <- rbindlist(list(dat.mon, dat.tue))

dat.long <- melt(dat.all, measure.vars=c("Mark","Matt"), variable="Person")

请注意,数据现在以长格式组织,以便于绘图:

    n Day Person      value
1:  1 Mon   Mark   13336000
2:  2 Mon   Mark   41271100
3:  3 Mon   Mark   55416000
4:  4 Mon   Mark  230630600
5:  6 Mon   Mark  354388700
6:  8 Mon   Mark  462991400
7:  9 Mon   Mark  235045400
8: 10 Mon   Mark  168440600
9: 12 Mon   Mark  362305500
...
19: 29 Mon   Mark 3280830100
20: 30 Mon   Mark 2923459800
21:  1 Tue   Mark   16722900
22:  2 Tue   Mark   99067800
23:  3 Tue   Mark  219396000
24:  4 Tue   Mark  254380400
25:  6 Tue   Mark  438443760
26:  8 Tue   Mark  497156100
...
etc

然后可以将其分开以使颜色表示人,或颜色表示日。

g1 <- ggplot(data=dat.long, mapping=aes(x=n, y=value, fill=Day)) + geom_bar(stat="identity", position="dodge") + facet_grid(Person~.)

g1

g2 <- ggplot(data=dat.long, mapping=aes(x=n, y=value, fill=Person)) + geom_bar(stat="identity", position="dodge") + facet_grid(Day~.)

g2