使用带有stat_summary的log2传输

时间:2018-04-29 09:49:32

标签: r

我是R的新手并尝试绘制两个因素的箱线图。我可以根据我的要求绘制图表。这是代码:

library(ggplot2)

Systems <- c(rep("A", 23), rep("B", 328), rep("C", 85), rep("D", 25))
treatment <- rep(c("MEAN\n0.035                         0.252                         0.005                         0.032", "MEAN\n0.030                         0.213                         0.008                         0.033"), each = 461)
Performance <- c(
  0.041817315, 0.012366105, 0.008223291, 0.101194094, 0.032095414, 0.022424856, 0.004272651, 0.02568757, 0.012011461, 0.032519093, 0.015862062, 0.027747871, 0.024599762, 0.003786862, 0.28017758, 0.017898477, 0.00646475, 0.004093185, 0.000740573, 0.039312335, 0.002415459, 0.051774672, 0.029394015, 0.303119908, 0.247602112, 0.234497662, 0.240247293, 0.256006375, 0.241818886, 0.241818886, 0.23355839, 0.23355401, 0.24791564, 0.232738515, 0.247145881, 0.234726096, 0.233802269, 0.296144867, 0.318978078, 0.309485102, 0.24498581, 0.361880846, 0.233798292, 0.233802269, 0.23446443, 0.23446443, 0.245941147, 0.251454016, 0.256166292, 0.269703778, 0.25459034, 0.255547875, 0.293828554, 0.269443031, 0.245104749, 0.278098811, 0.304266148, 0.238588328, 0.304754431, 0.251716996, 0.260886932, 0.260633422, 0.247319476, 0.24162866, 0.268908914, 0.23960451, 0.255093289, 0.268908914, 0.245971624, 0.26665091, 0.279822691, 0.268784457, 0.298624598, 0.241807867, 0.232050361, 0.241527075, 0.27750843, 0.26665091, 0.2317304, 0.234567279, 0.232487121, 0.233076363, 0.242322263, 0.241818886, 0.241818886, 0.235521394, 0.261765062, 0.270101105, 0.242477399, 0.232661245, 0.283295967, 0.247602112, 0.263427876, 0.239485768, 0.237005999, 0.243690789, 0.247371437, 0.237005999, 0.247231137, 0.249111033, 0.26665091, 0.254097987, 0.273745634, 0.290167407, 0.245623389, 0.241930972, 0.231602752, 0.234514088, 0.256039044, 0.265463913, 0.256166292, 0.247743609, 0.232694788, 0.237961675, 0.281536336, 0.24977717, 0.262282857, 0.24543148, 0.247155404, 0.247155404, 0.26129785, 0.234514088, 0.26494978, 0.258628847, 0.257580217, 0.253061784, 0.232630126, 0.247155404, 0.244319159, 0.262394668, 0.26665091, 0.3165875, 0.268706383, 0.250278577, 0.253011248, 0.249419117, 0.245104749, 0.235595377, 0.238554589, 0.273267375, 0.248984622, 0.24520136, 0.258955737, 0.2317304, 0.272465904, 0.23870662, 0.248026932, 0.263427876, 0.278497558, 0.265828268, 0.234826045, 0.2317304, 0.232172596, 0.235692813, 0.232737079, 0.297579932, 0.286422023, 0.259325175, 0.250639446, 0.231587836, 0.300662192, 0.248210353, 0.294150342, 0.251203475, 0.251417668, 0.295233578, 0.256006375, 0.256006375, 0.245015073, 0.249356914, 0.293528269, 0.281821725, 0.248799266, 0.301499956, 0.247721985, 0.290792643, 0.328679821, 0.330321343, 0.30640335, 0.276763556, 0.247496304, 0.237458207, 0.237458207, 0.237516949, 0.233802269, 0.234468703, 0.239264575, 0.234887381, 0.23446443, 0.234887381, 0.234225205, 0.237004685, 0.23455164, 0.23446443, 0.234870008, 0.232694788, 0.234870008, 0.234860348, 0.270044052, 0.232694788, 0.269666687, 0.236614177, 0.280480722, 0.243978724, 0.249111033, 0.279925672, 0.232172596, 0.239249363, 0.272197191, 0.246987548, 0.244882001, 0.269280706, 0.249566945, 0.241863369, 0.239677242, 0.242426616, 0.253140755, 0.239869221, 0.239677242, 0.23171238, 0.275635746, 0.231602752, 0.286266232, 0.259265885, 0.239869221, 0.236614177, 0.255256967, 0.257580217, 0.290128013, 0.274832429, 0.243149974, 0.232123048, 0.235250227, 0.276830805, 0.231188689, 0.247743609, 0.245963204, 0.259455641, 0.32522266, 0.252770086, 0.236461595, 0.244470967, 0.244024628, 0.24498581, 0.2317304, 0.233464424, 0.266062106, 0.233118162, 0.243588113, 0.232694788, 0.243435021, 0.249775037, 0.240018899, 0.26665091, 0.26665091, 0.268784457, 0.237497376,
  0.237483089, 0.237145874, 0.237145874, 0.2317304, 0.243435021, 0.269419291, 0.243767405, 0.274193607, 0.243435021, 0.237281364, 0.237205977, 0.237145874, 0.237145874, 0.245029635, 0.242611375, 0.237961675, 0.266369762, 0.266369762, 0.245760558, 0.266369762, 0.249121027, 0.291800661, 0.240954111, 0.238391917, 0.235264993, 0.233355127, 0.234898357, 0.235244609, 0.238499482, 0.234215035, 0.236608826, 0.235265733, 0.25019165, 0.283415889, 0.237145874, 0.267352731, 0.237422834, 0.273147585, 0.225775832, 0.238321926, 0.235125859, 0.232694788, 0.249626478, 0.234218629, 0.235269307, 0.26533449, 0.242207321, 0.24183494, 0.241848474, 0.256006375, 0.24183494, 0.241848474, 0.2734133, 0.241848474, 0.241848474, 0.24183494, 0.23422781, 0.248608779, 0.235125859, 0.234871225, 0.235125859, 0.233774363, 0.235278439, 0.245684423, 0.234705022, 0.245684423, 0.235186554, 0.245684423, 0.23470016, 0.236614177, 0.245953538, 0.245953538, 0.245922432, 0.249522459, 0.276680994, 0.251743401, 0.235125859, 0.222236695, 0.251487861, 0.286975357, 0.240545386, 0.245488295, 0.233464424, 0.270731686, 0.241818886, 0.285677834, 0.24543148, 0.237961675, 0.262911409, 0.258234963, 0.24326508, 0.236608826, 0.237516949, 0.26533449, 0.003208185, 0.003075313, 0.003075313, 0.051965266, 0.086809801, 0.002074263, 0.003814715, 0.00241056, 0.004519913, 0.002354925, 0.003668154, 0.002411354, 0.001046015, 0.00641338, 0.002344457, 0.00090009, 0.010972388, 0.001492537, 0.00084317, 0.005872887, 0.003912383, 0.001643901, 0.001492537, 0.001844077, 0.00084317, 0.005379174, 0.001821256, 0.002505201, 0.001051525, 0.00084317, 0.002505201, 0.004837974, 0.00084317, 0.002488134, 0.005078271, 0.00084317, 0.002488134, 0.00250647, 0.00250647, 0.002480833, 0.002480833, 0.001101322, 0.010244482, 0.001321974, 0.012283591, 0.001733435, 0.001512552, 0.001671509, 0.001428863, 0.002618663, 0.005121464, 0.002808259, 0.002503417, 0.002558745, 0.00216939, 0.002021331, 0.001588465, 0.001690088, 0.003279291, 0.002295707, 0.003248547, 0.021351276, 0.002499903, 0.002503417, 0.001101322, 0.00354847, 0.002499903, 0.003075313, 0.00567605, 0.01320935, 0.001052632, 0.004384033, 0.001918676, 0.001597092, 0.015212658, 0.001046015, 0.006676165, 0.006883556, 0.002696254, 0.003247508, 0.002709837, 0.002234992, 0.011494253, 0.001311413, 0.014849595, 0.012022537, 0.006497906, 0.025349246, 0.020579744, 0.049220779, 0.018911298, 0.005464481, 0.023748064, 0.045756258, 0.010638298, 0.019893403, 0.023962608, 0.05465644, 0.028966333, 0.004459272, 0.105586592, 0.075615135, 0.022154774, 0.017105029, 0.005208333, 0.011363636, 0.008951424, 0.117416075, 0.007916667, 0.078388988, 0.030861112, 0.003519782, 0.005422482, 0.125796873, 0.005031371, 0.03362749, 0.000778961, 0.016681028, 0.004599127, 0.000454307, 0.001505359, 0.031611448, 0.011308386, 0.002134792, 0.286294212, 0.018161072, 0.006453087, 0.003571249, 0.000447235, 0.042566651, 7.77303E-05, 0.042401148, 0.023784287, 0.245226355, 0.207228787, 0.204169525, 0.211077481, 0.213258479, 0.207484024, 0.207484024, 0.201602486, 0.202638497, 0.216700318, 0.201214614, 0.220748161, 0.202433079, 0.201738383, 0.248574743, 0.262874666,
  0.252606723, 0.200504039, 0.312541501, 0.202773831, 0.201738383, 0.2030333, 0.2030333, 0.203064156, 0.209310089, 0.217450641, 0.231772732, 0.209757897, 0.217977319, 0.247090075, 0.21844954, 0.209559111, 0.225575484, 0.254032564, 0.201524647, 0.24778379, 0.217517211, 0.222981128, 0.227145657, 0.210758647, 0.208297414, 0.216762142, 0.203586766, 0.216700206, 0.216762142, 0.207592071, 0.213666768, 0.227338434, 0.215879786, 0.246166402, 0.20561442, 0.205026788, 0.207675824, 0.227655498, 0.213666768, 0.197325754, 0.210254948, 0.206766076, 0.207358384, 0.218678788, 0.207484024, 0.207484024, 0.204014221, 0.218887554, 0.225205822, 0.204384584, 0.198406675, 0.227971421, 0.207228787, 0.214348845, 0.201777676, 0.205916468, 0.213450757, 0.209587709, 0.205916468, 0.209362242, 0.20856388, 0.213666768, 0.216186368, 0.234453313, 0.248382745, 0.21701993, 0.213600036, 0.201106069, 0.203802471, 0.216655494, 0.225560499, 0.217450641, 0.208587224, 0.197681539, 0.199276425, 0.232171602, 0.210470295, 0.22175041, 0.204355918, 0.204475181, 0.204475181, 0.231548299, 0.203802471, 0.223041945, 0.217862851, 0.230785952, 0.213283262, 0.200257172, 0.204475181, 0.202897929, 0.211315524, 0.213666768, 0.263361407, 0.225185308, 0.214271007, 0.210497889, 0.212978687, 0.209559111, 0.199014939, 0.20945405, 0.224553068, 0.217749634, 0.216496692, 0.212296213, 0.197325754, 0.221826528, 0.208655636, 0.20192612, 0.214348845, 0.223101747, 0.23194135, 0.198683705, 0.197325754, 0.198089791, 0.20427291, 0.199290161, 0.24070988, 0.249032587, 0.218488173, 0.211126674, 0.197780282, 0.244029201, 0.208767987, 0.240834568, 0.209095817, 0.20977662, 0.239262653, 0.213258479, 0.213258479, 0.209840866, 0.210998294, 0.251844378, 0.22782243, 0.209390413, 0.242168553, 0.207556718, 0.230220324, 0.277619451, 0.271500416, 0.2463793, 0.236268228, 0.204252778, 0.205975522, 0.205975522, 0.20638888, 0.201738383, 0.202307262, 0.203126957, 0.203547468, 0.2030333, 0.203547468, 0.202251975, 0.206942541, 0.202009011, 0.2030333, 0.199306658, 0.197681539, 0.199306658, 0.200035605, 0.219731274, 0.197681539, 0.220112984, 0.201326042, 0.222447694, 0.204767129, 0.20856388, 0.238296852, 0.198089791, 0.20783083, 0.228566801, 0.213406582, 0.202128774, 0.223103043, 0.21174389, 0.209480203, 0.203141599, 0.213522888, 0.218265827, 0.203332504, 0.203141599, 0.200857909, 0.221504103, 0.201106069, 0.234932916, 0.224156158, 0.203332504, 0.201326042, 0.212033061, 0.230785952, 0.232169974, 0.22103837, 0.208472924, 0.201662359,
  0.197770095, 0.227186459, 0.197226949, 0.208587224, 0.203044091, 0.21799408, 0.27616292, 0.206754881, 0.205386431, 0.20518108, 0.206706255, 0.200504039, 0.197325754, 0.201725618, 0.216929898, 0.198194632, 0.210965099, 0.197681539, 0.210099248, 0.205271974, 0.200638561, 0.213666768, 0.213666768, 0.215879786, 0.209006585, 0.208696114, 0.207817635, 0.207817635, 0.197325754, 0.210099248, 0.216982956, 0.212178741, 0.217721009, 0.210099248, 0.20711904, 0.208521315, 0.207817635, 0.207817635, 0.205501252, 0.202278423, 0.199276425, 0.213447242, 0.213447242, 0.205132583, 0.213447242, 0.214546613, 0.233837102, 0.20461375, 0.200041017, 0.203376022, 0.201666622, 0.203273295, 0.206239566, 0.206005147, 0.202367361, 0.205336165, 0.206159599, 0.212043546, 0.23652672, 0.207817635, 0.222323542, 0.207992955, 0.230179094, 0.191639063, 0.203652472, 0.205707127, 0.197681539, 0.210851357, 0.20206244, 0.205851609, 0.230072742, 0.203700261, 0.204112552, 0.203387156, 0.213258479, 0.204112552, 0.203387156, 0.226895739, 0.203387156, 0.203387156, 0.204112552, 0.202062597, 0.207156386, 0.205707127, 0.204103383, 0.205707127, 0.20231351, 0.205851767, 0.202278126, 0.203456946, 0.202278126, 0.206408028, 0.202278126, 0.202940706, 0.201326042, 0.205811807, 0.205811807, 0.202945571, 0.209642676, 0.236595043, 0.211389397, 0.205707127, 0.187165901, 0.221709686, 0.235580538, 0.205130035, 0.208843627, 0.201725618, 0.228112266, 0.207484024, 0.233405455, 0.204355918, 0.199276425, 0.2214728, 0.214289366, 0.207335809, 0.205336165, 0.20638888, 0.230072742, 0.011415222, 0.003524363, 0.003524363, 0.065250462, 0.099337796, 0.002467596, 0.005194761, 0.002749848, 0.00431973, 0.002470309, 0.00429052, 0.002528805, 0.000964973, 0.007359328, 0.002605198, 0.010989011, 0.012864128, 0.001187648, 0.003546099, 0.024859586, 0.004188306, 0.002222601, 0.001187648, 0.002754248, 0.003546099, 0.001364, 0.001148428, 0.002092947, 0.000649351, 0.003546099, 0.002092947, 0.002955342, 0.003546099, 0.012749845, 0.002594875, 0.003546099, 0.012749845, 0.002994309, 0.002994309, 0.021827371, 0.021827371, 0.001730104, 0.011329362, 0.001260936, 0.01263916, 0.001845543, 0.002035106, 0.003043491, 0.002160411, 0.001939528, 0.010275639, 0.003682458, 0.002439817, 0.012294413, 0.003980996, 0.028844157, 0.002571735, 0.005293216, 0.003654834, 0.002891218, 0.002046854, 0.037579121, 0.002432344, 0.002439817, 0.001730104, 0.006082265, 0.002432344, 0.003524363, 0.006134648, 0.009195465, 0.000657895, 0.005597993, 0.002345224, 0.001933761, 0.0150357, 0.000964973, 0.004387928, 0.00773195, 0.002727137, 0.006166549, 0.002631304, 0.002269814, 0.000711744, 0.001519004, 0.013924241, 0.016998883, 0.004487617, 0.009501123, 0.06314105, 0.00686984, 0.016491278, 0.005154639, 0.006493982, 0.028453674, 0.005319149, 0.165847332, 0.015483992, 0.151746052, 0.058287127, 0.004457018, 0.005368773, 0.037517336, 0.012809372, 0.012127533, 0.002079002, 0.003448276, 0.017034111, 0.117145251, 0.016396803, 0.092959905
)
data <- data.frame(Systems, treatment, Performance)
data$Systems <- factor(data$Systems, levels = c("A", "B", "C", "D"))
colors <- c(rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25), rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25))

scaleFUN <- function(x) sprintf("%.3f", x)

ggplot(data, aes(x = Systems, y = Performance, fill = treatment)) +
  geom_boxplot(alpha = 1, aes(fill = factor(colors))) +
  facet_wrap(~ treatment) +
  stat_summary(
    fun.y = mean, geom = "point", shape = 21, size = 5, color = "#FFFFFF", fill = "#222222"
  ) + 
  ylab("Average Precision") +
  ggtitle("       TEC-Gensim                                                                            TEC-Matlab") +
  theme(
    legend.position = "none",
    panel.grid.major = element_line(colour = "#d3d3d3"),
    panel.border = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5)
  ) +
  scale_fill_brewer(palette = "Greys") +
  coord_cartesian(ylim = c(0.001, 0.3)) +
  scale_y_continuous(breaks = c(0, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256))

这样的转换使得显示更好,但是使用stat_summary来干扰通过geom-point表现出来的平均值。

1 个答案:

答案 0 :(得分:0)

也许您正在寻找geom = "label"内的stat_summary选项?

library(ggplot2)

Systems <- c(rep("A", 23), rep("B", 328), rep("C", 85), rep("D", 25))
treatment <- rep(c("TEC-Gensim", "TEC-Matlab"), each = 461)
Performance <- c(rnorm(n = 230, mean = 0.035, sd = 0.02),
                 rnorm(n = 231, mean = 0.252, sd = 0.01),
                 rnorm(n = 230, mean = 0.005, sd = 0.03),
                 rnorm(n = 231, mean = 0.031, sd = 0.01))
Performance <- c(runif(n = 230, max = 0.3),
                 runif(n = 231, max = 0.3),
                 runif(n = 230, max = 0.3),
                 runif(n = 231, max = 0.3))
data <- data.frame(Systems, treatment, Performance)
data$Systems <- factor(data$Systems, levels = c("A", "B", "C", "D"))
colors <- c(rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25), rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25))

scaleFUN <- function(x) sprintf("%.3f", x)

ggplot(data, aes(x = Systems, y = Performance, fill = treatment)) +
  geom_boxplot(alpha = 1, aes(fill = factor(colors))) +
  facet_wrap(~ treatment) +
  stat_summary(fun.y = mean, geom = "point", shape = 21, size = 5, 
               color = "#FFFFFF", fill = "#222222") + 
  stat_summary(fun.y = mean, geom = "label", aes(label = round(..y.., 3)), 
               hjust = -0.3, fill = 'white') + 
  ylab("Average Precision") +
  theme(
    legend.position = "none",
    panel.grid.major = element_line(colour = "#d3d3d3"),
    panel.border = element_blank(),
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5)
  ) +
  scale_fill_brewer(palette = "Greys") +
  coord_cartesian(ylim = c(0.001, 0.3)) +
  scale_y_continuous(breaks = c(0, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256))
```