使用不同的矩阵创建条形图 - 可能需要循环

时间:2015-12-17 13:18:14

标签: r

我有四个数据集,我想用它来创建条形图。我必须为昨天制作那些条款......所以如果答案能让我满意的话,我可以保证创造一个价值100分的赏金。谢谢! 让我们首先向您展示数据:

data1 <- structure(c(1.071378962, 0.845918605, 0.943329547, 0.814648308, 1.190586037, 0.842555028, 1.006615522, 0.943341723, 0.970762595, 0.842846286, 0.554258129, 0.87289711, 0.890745502, 0.941635877, 0.999470449, 0.973126826, 0.857023562, 0.868671406, 0.959087679, 0.931290542, 1.240037136, 1.262066016, 0.776554623, 1.032276723, 1.39086975, 0.89611471, 1.022911942, 0.99951195, 0.943864517, 1.021282723, 0.997836642, 1.095591277, 0.851299889, 1.066302784, 1.049684459, 0.751507841, 0.698681059, 1.12356928, 1.11429691,1.377308152, 1.075640032, 1.011348603, 0.712689025, 1.160469927, 0.995125559, 1.00048805, 1.090401663, 1.10721464, 0.931480983, 0.941864276, 0.876112393, 1.018348149, 1.082099793, 1.006545876, 1.233818591, 1.053432293, 1.088147123, 1.421639524, 1.20566246, 1.234880201), 
                   .Dim = c(6L, 10L), .Dimnames = list(c("Glutamic acid (3TMS)", "Glutamine (3TMS)", "Glutamine [-H2O] (3TMS) MP", "Glycine (3TMS)", "Homoserine (3TMS)", "Isoleucine (2TMS)"), c("35sT0_1", "35sT6_2", "35sT12_1", "35sT18_1", "35sT24/0_1", "35sT6", "35sT12", "35sT18", "35sT24", "36")))

data2 <- structure(c(0.877569815, 0.754104132, 0.876580546, 0.79174725, 0.740627647, 0.78477491, 1.391074787, 0.903400207, 0.880369023, 1.016378295, 0.687526731, 0.950215351, 0.826410582, 0.869522074, 0.994630648, 1.078985901, 1.104160252, 1.025710541, 0.950735109, 0.771993562, 1.294087566, 1.035714675, 2.882357132, 0.934083376, 1.247748287, 0.907247361, 1.085978546, 0.815903305, 0.86025955, 1.068675991, 1.009812793, 0.968107855, 0.80863382, 1.005377463, 1.211018321, 1.265644741, 0.990187207, 1.229007661, 1.442401907, 1.73732055, 0.891099345, 1.572392641, 0.940013025, 1.354693148, 1.880948023, 0.244167096, 1.004958904, 0.886012155, 0.694279913, 1.007486834, 1.889314819, 0.353570235, 1.054019938, 0.76681767, 1.24476, 1.329996557, 1.468541149, 7.406748974, 1.545245839, 1.106562035), 
                   .Dim = c(6L, 10L), .Dimnames = list(c("Glutamic acid (3TMS)", "Glutamine (3TMS)", "Glutamine [-H2O] (3TMS) MP", "Glycine (3TMS)", "Homoserine (3TMS)", "Isoleucine (2TMS)"), c("CY T0", "CY T6", "CYT12", "CYT18", "CYT24/0", "CYT6", "CYT12.1", "CYT18.1", "CYT24", "CY T36")))

data3 <- structure(c(0.849113132, 0.867446824, 1.09944032, 1.087639782, 1.658436326, 0.848084135, 0.932477314, 0.732136554, 0.831174363, 4.653172735, 1.919225351, 1.249698531, 1.019379011, 1.075136263, 0.84454205, 3.775160164, 1.059201807, 1.184276257, 1.092907083, 1.221943192, 1.108780315, 0.435989903, 1.125116414, 0.623062027, 0.725781032, 0.810901749, 0.658652321, 0.510525467, 1.380560662, 0.774070062, 1.233302825, 0.891978129, 0.920812453, 1.61882119, 1.275302363, 1.350957955, 1.340678283, 1.093757492, 0.843189879, 1.960540873, 0.722676434, 1.162607818, 1.100110909, 1.273253134, 1.319698547, 0.583615725, 0.656060363, 1.008817173, 0.949718076, 0.742767254, 0.839453751, 0.310951866, 0.621772828, 0.739753179, 1.042081677, 0.975524021, 1.264973389, 1.048201078, 0.557721294, 1.271553398), 
                   .Dim = c(6L, 10L), .Dimnames = list(c("Glutamic acid (3TMS)", "Glutamine (3TMS)", "Glutamine [-H2O] (3TMS) MP", "Glycine (3TMS)", "Homoserine (3TMS)", "Isoleucine (2TMS)"), c("BY T0", "BY_T6", "BY_T12_1_H", "BY_T18_1_H", "BY_T24/0_1_H", "BY_T6_1_E", "BY_T12_1_E", "BY_T18_1_E", "BY_T24_2_E", "BY_T36_1_E")))

data4 <- structure(c(0.84701505, 0.802885267, 0.904237652, 1.32987322, 2.128714361, 0.805699204, 0.810320764, 0.903767386, 0.935202459, 6.780328979, 2.063612653, 1.077378921, 0.97516314, 0.985386891, 1.067841643, 2.815618456, 0.918593598, 0.999272562, 1.008076422, 1.189472266, 1.395712495, 0.282374593, 0.983116661, 0.568664497, 0.909818192, 0.893018596, 1.222437736, 0.604158391, 1.246582632, 0.70758154, 1.389695369, 0.932400597, 0.860221952, 1.910176239, 1.118800023, 1.249962128, 1.306451181, 1.145684561, 0.901161127, 1.215557692, 0.865679023, 1.221414084, 0.917444989, 1.060004352, 1.190554065, 0.074366064, 0.439345422, 1.000727438, 1.207610084, 0.845900309, 0.908110661, 0.335904595, 0.913901528, 0.660771151, 1.093251198, 2.254039524, 1.463008439, 0.897777659, 1.11725439, 1.237916462), 
                   .Dim = c(6L, 10L), .Dimnames = list(c("Glutamic acid (3TMS)", "Glutamine (3TMS)", "Glutamine [-H2O] (3TMS) MP", "Glycine (3TMS)", "Homoserine (3TMS)", "Isoleucine (2TMS)"), c("BY?C_T0_1_H", "BY?C_T6_1_H", "BY?C_T12_1_H", "BY?C_T18_1_H", "BY?C__T24/0_1_H", "BY?C_T6_1_E", "BY?C_T12_1_E", "BY?C_T18_1_E", "BY?C_T24_1_E", "BY?C_T36_1_E")))

现在我将向您展示所需的输出:

Desired Output

我的第一个想法是使用以下代码创建两个循环:

barplot(cbind(data1[1,1],data2[1,1],data3[1,1],data4[1,1]), col=c("orange", "blue", "grey", "yellow"), main=paste(row.names(data1))[1],
        ylim=c(0,5),
        xlab="",
        ylab="",
        cex.names = 0.8,
        cex.axis= 0.8)
mtext("", side=1, line = 5, las=1, cex = 1.2)
mtext("", side=2, line = 2.2, cex = 1.2, las = 0)

但是我意识到这有点复杂。

一般情况下,我们有4个数据集,每个10列,因此图表应包含10个图表(根据列数),每个图表应由4个条形图(每个数据集中的每个条形图)创建......

当然,我在上面展示的用于创建条形图的代码就是我开始的方式,但是你想要的输出高于那个......

SD表:

> dput(data1_sd)
structure(c(0.174604366, 0.165563427, 0.124041335, 0.14511094, 
0.139918267, 0.23339937, 0.23755908, 0.155852388, 0.068730865, 
0.304260431, 0.124101514, 0.11349789, 0.205413587, 0.173345394, 
0.171627016, 0.243774428, 0.121321474, 0.046465904, 0.230572932, 
0.2014359, 0.10900584, 0.382311381, 0.491431901, 0.154356476, 
0.242469141, 0.308106893, 0.13337921, 0.360878813, 0.232004107, 
0.064899743, 0.24188698, 0.268135924, 0.142627079, 0.703070034, 
0.089433556, 0.173714936, 0.156795242, 0.185515491, 0.096163392, 
0.277589961, 0.204984493, 0.188805738, 0.079921899, 0.203036841, 
0.081821339, 0.173845851, 0.191866041, 0.064497002, 0.194148639, 
0.168003853, 0.277488565, 0.225448229, 0.150500502, 0.136685668, 
0.156833994, 0.351029035, 0.274898782, 0.830614885, 0.248569794, 
0.383625218), .Dim = c(6L, 10L), .Dimnames = list(c("Glutamic acid (3TMS)", 
"Glutamine (3TMS)", "Glutamine [-H2O] (3TMS) MP", "Glycine (3TMS)", 
"Homoserine (3TMS)", "Isoleucine (2TMS)"), c("35sT0_1", "35sT6_2", 
"35sT12_1", "35sT18_1", "35sT24/0_1", "35sT6", "35sT12", "35sT18", 
"35sT24", "36")))

2 个答案:

答案 0 :(得分:5)

以下是获得所需内容的方法:

# put your data.frame in a list
l_all <- list(data1, data2, data3, data4)
# add an index (actually, it's not necessary but may be useful to retrieve the data later
l_all <- lapply(seq(l_all), function(x){l_all[[x]] <- cbind(l_all[[x]], ind=x)})
# bind the data
all_tab <- do.call(rbind, l_all)

# then draw your plot, for example for "Glutamic acid (3TMS)"
barplot(all_tab[row.names(all_tab) %in% "Glutamic acid (3TMS)", 1:10], beside=TRUE, col=c("blue", "orange", "grey", "yellow"), main="Glutamic acid (3TMS)", las=1)

enter image description here

修改

要立即获取所有图表,您可以执行以下操作:

par(mfrow=c(5, 1)) # for example
for(name in unique(row.names(all_tab))){
  barplot(all_tab[row.names(all_tab) %in% name, 1:10], beside=TRUE, col=c("blue", "orange", "grey", "yellow"), main=name, las=1)
  par(page=FALSE)
}

<强> EDIT2

如果您希望导出为pdf(每个地块一页),这是一种方式(您必须填写“空白”):

pdf("path_to_file.pdf", width=xxx, height=xxx) # for example 7 for the width, 4 for the heigth, it's in inches, 7 is the default for both values
for(name in unique(row.names(all_tab))){
  barplot(all_tab[row.names(all_tab) %in% name, 1:10], beside=TRUE, col=c("blue", "orange", "grey", "yellow"), main=name, las=1)
}
dev.off()

答案 1 :(得分:3)

简介

一种可能的方法如下(使用data.table)。 该方法假定您的dataX名为dfX

首先,它将数据转换为ggplot格式(又称长格式),然后替换变量名称(new_col)并使用ggplot绘制数据。

代码:

library(data.table)

# 1. convert each df to the long format
dt1 <- data.table(t(df1), colnam = colnames(df1), df = "df1")
dt1 <- melt(dt1, id.vars = c("colnam", "df"))

dt2 <- data.table(t(df2), colnam = colnames(df2), df = "df2")
dt2 <- melt(dt2, id.vars =  c("colnam", "df"))

dt3 <- data.table(t(df3), colnam = colnames(df3), df = "df3")
dt3 <- melt(dt3, id.vars = c("colnam", "df"))

dt4 <- data.table(t(df4), colnam = colnames(df4), df = "df4")
dt4 <- melt(dt4, id.vars = c("colnam", "df"))

dt <- rbindlist(list(dt1, dt2, dt3, dt4))

# a function that searches for "T0" in x and replaces x with "T0" i.e., "35sT0_1" changes to "T0"
repl_fun <- function(x) ifelse(grepl("T0", x), "T0",
                               ifelse(grepl("T6", x), "T6",
                                      ifelse(grepl("T12", x), "T12",
                                             ifelse(grepl("T18", x), "T18",
                                                    ifelse(grepl("T24", x), "T24",
                                                           ifelse(grepl("T36", x), "T36",
                                                                  ifelse(grepl("36", x), "T36", 
                                                                  "NA")))))))

dt[, new_col := repl_fun(colnam)]

# adds a fake row with standard deviations (set to 0.5)
dt[, sd := 0.5]
dt
# colnam  df             variable     value new_col  sd
# 1:      35sT0_1 df1 Glutamic acid (3TMS) 1.0713790      T0 0.5
# 2:      35sT6_2 df1 Glutamic acid (3TMS) 1.0066155      T6 0.5
# 3:     35sT12_1 df1 Glutamic acid (3TMS) 0.8907455     T12 0.5
# 4:     35sT18_1 df1 Glutamic acid (3TMS) 0.9590877     T18 0.5
# 5:   35sT24/0_1 df1 Glutamic acid (3TMS) 1.3908698     T24 0.5
# ---                                                            
# 236:  BY?C_T6_1_E df4    Isoleucine (2TMS) 1.2499621      T6 0.5
# 237: BY?C_T12_1_E df4    Isoleucine (2TMS) 1.2214141     T12 0.5
# 238: BY?C_T18_1_E df4    Isoleucine (2TMS) 1.0007274     T18 0.5
# 239: BY?C_T24_1_E df4    Isoleucine (2TMS) 0.6607712     T24 0.5
# 240: BY?C_T36_1_E df4    Isoleucine (2TMS) 1.2379165     T36 0.5

library(ggplot2)


ggplot(dt[variable == "Glutamic acid (3TMS)"], 
       aes(x = new_col, y = value, fill = df, ymax = value + sd, ymin = value - sd)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  ggtitle("Glutamic acid (3TMS)") + 
  geom_errorbar(position = position_dodge(width = 0.9))
# will sometimes add two errorbars as you have multiple obs per case...

看起来像这样(请注意,有时候有多个观察我们有时会得到多个错误栏,其次,因为sd值设置为0.5,看起来很丑陋......)

First Plot

此外,如果您想要查看彼此之间的所有变量,我们可以使用facet_wrap

# all in one plot
ggplot(dt, aes(x = new_col, y = value, fill = df, 
               ymax = value + sd, ymin = value - sd)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  facet_wrap(~variable, ncol = 2, scales = "free_y") +
  geom_errorbar(position = position_dodge(width = 0.9))      
# if scales = "fixed", then all plots have the same y-axis range!

Second Plot

这有助于你/给你一些你需要的东西吗?

保存

要保存ggplot,您只需将ggplot-object分配给变量并保存如下:

plot_obj <- ggplot(....) # this is the long and complex plot...
ggsave(filename = "plot1.pdf", plot = plot_obj, scale = 1) # adjust scale, height, and width as you like