简体   繁体   English

将两个绘图中的绘制条重新排列为 R 中的三个不同绘图

[英]Re-arrangement of plotted bars from two plots into three different plots in R

I am quite new at processing data with R, as I've mostly used it to visualize plots and data.我对用 R 处理数据很陌生,因为我主要用它来可视化图表和数据。 I hope somebody has a suggestion regarding my following problem.我希望有人对我的以下问题提出建议。

I have two plots p1 (data: lgcanet ) and p2 (data: snpnet ), scripts have been attached below.我有两个图p1 (数据: lgcanet )和p2 (数据: snpnet ),下面附上了脚本。 Datasets have been dput ed at the bottom of this post.数据集已dput这篇文章的底部。

p1<-ggplot(lgcanet, aes(x = hour, y = Y, fill = predicted, ymin = YMIN, ymax = YMAX)) +
  geom_col(position = position_dodge2()) +
  geom_errorbar(position = position_dodge2()) +
  scale_fill_manual(values = c("Standing" = "gray80", "Feeding" = "gray60", "Moving" = "gray40" ))



p1<- p1 + labs(x = "Hour of the day", y = "Proportion",fill="Behavioural category" ) +


p1<-p1+theme(axis.text=element_text(size=17),
             axis.title=element_text(size=20))  + theme_set(theme_classic(base_size = 14)) +
  labs(title = "B") + ylim(0, 1) + theme(legend.position = "none") 

p1

p2<-ggplot(snpnet, aes(x = hour, y = Y, fill = predicted, ymin = YMIN, ymax = YMAX)) +
  geom_col(position = position_dodge2()) +
  geom_errorbar(position = position_dodge2()) + 
  scale_fill_manual(values = c("Stationary" = "gray80", "Feeding" = "gray60", "Moving" = "gray40" ))



p2<- p2 + labs(x = "Hour of the day", y = "Proportion",fill="Behavioural category" )

p2<-p2+theme(axis.text=element_text(size=17),
           axis.title=element_text(size=20))  + theme_set(theme_classic(base_size = 14)) +
           labs(title = "a") + ylim(0, 1) + theme(legend.position = "none") 

p2

Plots represent the time a subject allocated to a given behaviour ( feeding , moving , stationary ) for all hours of the day in two different areas, each represented by a plot ( p1 = lgcanet and p2 = snpnet ).绘图表示受试者在一天中的所有时间在两个不同区域分配给给定行为( feedingmovingstationary )的时间,每个区域由一个绘图( p1 = lgcanetp2 = snpnet )表示。

Hence, I would like to manipulate my datasets so that I end up with one plot for each behaviour ( feeding , moving , stationary ), for which I will have two bars for each hour of the day corresponding to the time the subject allocated to that behaviour in each area.因此,我想操纵我的数据集,以便我最终为每种行为( feedingmovingstationary )绘制一个图,为此我将在一天中的每个小时有两个条形图,对应于主题分配给那个的时间每个领域的行为。

If there is a function that can re-arrange my datasets in a quick and efficient way, I would be glad to know that.如果有一个函数可以快速有效地重新排列我的数据集,我会很高兴知道这一点。 Otherwise, I hope somebody can at least set me into the right track.否则,我希望有人至少能让我走上正轨。

Thanks in advance.提前致谢。

lgcanet dataset: lgcanet数据集:

> dput(lgcanet)
structure(list(hour = c(0L, 0L, 0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 
21L, 22L, 22L, 22L, 23L, 23L, 23L), predicted = c("Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing", "Feeding", 
"Moving", "Standing", "Feeding", "Moving", "Standing"), Y = c(0.154681, 
0.674219792, 0.26018171, 0.123956169, 0.770734667, 0.196624589, 
0.100965488, 0.817486337, 0.150570688, 0.210191433, 0.279995373, 
0.522647236, 0.234739907, 0.139925966, 0.686217363, 0.24114481, 
0.184914944, 0.627808135, 0.234325872, 0.254871562, 0.546399513, 
0.248319131, 0.261282084, 0.508018619, 0.227005233, 0.251133647, 
0.549151992, 0.217179979, 0.226309486, 0.590163933, 0.225626231, 
0.228739084, 0.583517505, 0.219801659, 0.252196842, 0.554826957, 
0.213678598, 0.216436233, 0.592714024, 0.181927787, 0.176007486, 
0.673224042, 0.153096459, 0.103211711, 0.791985426, 0.097062644, 
0.076794171, 0.907589555, 0.173318656, 0.291811127, 0.55956284, 
0.114016498, 0.771948996, 0.18684528, 0.10592056, 0.758283747, 
0.196845864, 0.153399026, 0.637745394, 0.288967977, 0.154103192, 
0.545776799, 0.414282403, 0.17916971, 0.477163312, 0.428690807, 
0.188625532, 0.410643344, 0.460762284, 0.185673466, 0.556603437, 
0.360123553), YMIN = c(0.111622584, 0.57558919, 0.178703401, 
0.088143131, 0.687337528, 0.1217464, 0.0733921, 0.755478814, 
0.093856058, 0.1693268, 0.216235582, 0.452992291, 0.189585149, 
0.075280623, 0.621341306, 0.199077922, 0.128682591, 0.559547066, 
0.191776222, 0.188045367, 0.470199491, 0.205963272, 0.201685928, 
0.445531582, 0.186592457, 0.191004511, 0.485288287, 0.18087768, 
0.172783057, 0.524384284, 0.187255536, 0.166292047, 0.511705373, 
0.180626825, 0.196842022, 0.488278071, 0.180383614, 0.170865092, 
0.531781636, 0.147661097, 0.124588753, 0.61272206, 0.119970955, 
0.069121836, 0.738342094, 0.070756358, 0.040137355, 0.861936686, 
0.142587275, 0.228164929, 0.488514426, 0.082217165, 0.689415552, 
0.114563802, 0.077481332, 0.682770968, 0.13755071, 0.113970229, 
0.536293985, 0.20134563, 0.112932108, 0.431542955, 0.318313417, 
0.139101201, 0.377559378, 0.339132576, 0.142921212, 0.308316469, 
0.368743688, 0.132494556, 0.452613721, 0.277533897), YMAX = c(0.197739415, 
0.772850394, 0.341660019, 0.159769208, 0.854131807, 0.271502778, 
0.128538875, 0.87949386, 0.207285318, 0.251056066, 0.343755165, 
0.592302181, 0.279894666, 0.204571309, 0.751093421, 0.283211698, 
0.241147296, 0.696069203, 0.276875523, 0.321697757, 0.622599535, 
0.29067499, 0.320878239, 0.570505655, 0.267418009, 0.311262784, 
0.613015698, 0.253482278, 0.279835915, 0.655943582, 0.263996927, 
0.291186121, 0.655329638, 0.258976493, 0.307551662, 0.621375843, 
0.246973583, 0.262007374, 0.653646412, 0.216194476, 0.22742622, 
0.733726024, 0.186221963, 0.137301586, 0.845628758, 0.12336893, 
0.113450987, 0.953242423, 0.204050036, 0.355457325, 0.630611254, 
0.145815831, 0.854482441, 0.259126758, 0.134359788, 0.833796525, 
0.256141017, 0.192827823, 0.739196803, 0.376590324, 0.195274276, 
0.660010642, 0.510251389, 0.219238218, 0.576767247, 0.518249038, 
0.234329851, 0.51297022, 0.552780881, 0.238852376, 0.660593152, 
0.442713209)), row.names = c(NA, -72L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000002631ef0>)

snpnet dataset: snpnet数据集:

> dput(snpnet)
structure(list(hour = c(0L, 0L, 0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 
21L, 22L, 22L, 22L, 23L, 23L, 23L), predicted = c("Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary"), Y = c(0.186468006, 
0.619848556, 0.102946434, 0.117420501, 0.710255217, 0.085296176, 
0.122578887, 0.705326855, 0.082978251, 0.282820082, 0.369538165, 
0.225187701, 0.340835793, 0.290042658, 0.254104054, 0.303040651, 
0.361150303, 0.219680679, 0.281013065, 0.38755169, 0.215007656, 
0.267808505, 0.437847187, 0.188066206, 0.256738674, 0.418523792, 
0.191317998, 0.282095454, 0.378921649, 0.192197946, 0.296221256, 
0.343671498, 0.21204071, 0.264178641, 0.37476084, 0.215286911, 
0.285715162, 0.343315118, 0.225349727, 0.274045769, 0.354524621, 
0.233979875, 0.277640649, 0.2921432, 0.289202238, 0.323114441, 
0.221145203, 0.357155217, 0.240638977, 0.470048865, 0.150795243, 
0.146938436, 0.673705726, 0.071591529, 0.143295364, 0.666298039, 
0.079103593, 0.218088342, 0.568232731, 0.110925072, 0.267305927, 
0.482546431, 0.151432786, 0.306767296, 0.476052652, 0.142276041, 
0.264867497, 0.502361776, 0.148534296, 0.238832836, 0.539938351, 
0.14438792), YMIN = c(0.128852501, 0.512838079, 0.068314237, 
0.07729792, 0.602868608, 0.048590748, 0.075040937, 0.604890563, 
0.013469543, 0.227039126, 0.290705926, 0.153093633, 0.278753614, 
0.216026099, 0.184689256, 0.233381948, 0.285383003, 0.165621149, 
0.209569197, 0.304152031, 0.150927274, 0.206967076, 0.356326729, 
0.133252296, 0.201372373, 0.337442795, 0.144078897, 0.222104493, 
0.301436677, 0.145942262, 0.234941302, 0.272937178, 0.165504544, 
0.207677165, 0.300390926, 0.159293156, 0.227882598, 0.281692727, 
0.167766199, 0.210148377, 0.278131075, 0.176383174, 0.215768541, 
0.212191421, 0.225708559, 0.252433463, 0.15739698, 0.299506967, 
0.190150005, 0.387794877, 0.114610831, 0.106533711, 0.58181318, 
0.047868642, 0.100032326, 0.580337524, 0.041469188, 0.149052706, 
0.476381247, 0.069468115, 0.196848657, 0.403074576, 0.100976953, 
0.223306634, 0.39000654, 0.0915594, 0.195878551, 0.409341697, 
0.083305359, 0.172234465, 0.439986563, 0.08055391), YMAX = c(0.24408351, 
0.726859034, 0.137578631, 0.157543082, 0.817641826, 0.122001604, 
0.170116836, 0.805763147, 0.15248696, 0.338601039, 0.448370404, 
0.297281769, 0.402917971, 0.364059216, 0.323518852, 0.372699353, 
0.436917603, 0.27374021, 0.352456934, 0.47095135, 0.279088038, 
0.328649933, 0.519367645, 0.242880117, 0.312104975, 0.499604789, 
0.238557099, 0.342086415, 0.456406621, 0.23845363, 0.357501211, 
0.414405819, 0.258576876, 0.320680116, 0.449130755, 0.271280665, 
0.343547727, 0.404937508, 0.282933255, 0.337943161, 0.430918168, 
0.291576576, 0.339512757, 0.372094979, 0.352695917, 0.39379542, 
0.284893427, 0.414803466, 0.29112795, 0.552302853, 0.186979655, 
0.187343161, 0.765598272, 0.095314417, 0.186558401, 0.752258555, 
0.116737998, 0.287123977, 0.660084216, 0.152382029, 0.337763196, 
0.562018286, 0.20188862, 0.390227957, 0.562098763, 0.192992682, 
0.333856444, 0.595381856, 0.213763233, 0.305431207, 0.639890139, 
0.20822193)), row.names = c(NA, -72L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000002631ef0>)

In your two dataset, you have Stationary and Standing .. I assumed they are the same and I will convert all of them into Stationary :在您的两个数据集中,您有StationaryStanding .. 我假设它们是相同的,我会将它们全部转换为Stationary

lgcanet$predicted = as.character(lgcanet$predicted)
lgcanet$predicted = gsub("Standing","Stationary",lgcanet$predicted)

First we combine the data frames and give them an annotation according to the dataset:首先我们组合数据框,并根据数据集给它们一个注释:

dat = data.frame(rbind(
cbind(lgcanet,data="lgcanet"),
cbind(snpnet,data="snpnet")))

You can look at them in one plot first:您可以先在一个图中查看它们:

ggplot(dat, aes(x = hour, y = Y, fill = data, ymin = YMIN, ymax = YMAX)) +
  geom_col(position = position_dodge2()) +
  geom_errorbar(position = position_dodge2()) + 
  facet_wrap(~predicted,ncol=1,scale="free_y")

在此处输入图片说明

To have them separately, do:要将它们分开,请执行以下操作:

allplots = by(dat,dat$predicted,function(i){
    ggplot(i, aes(x = hour, y = Y, fill = data, ymin = YMIN, ymax = YMAX)) +
    geom_col(position = position_dodge2()) +
    geom_errorbar(position = position_dodge2()) +
    ggtitle(unique(i$predicted))
})

names(allplots)
[1] "Feeding"    "Moving"     "Stationary"

print(allplots[["Feeding"]])
# or save using ggsave(allplots[["Feeding"]],file=...)

在此处输入图片说明

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

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