ggplot2 boxplot仅显示垂直线

时间:2018-10-19 09:15:09

标签: r ggplot2

我试图将一些索引值作为箱形图与另一个索引的值作图,但是出了点问题,我无法弄清楚是什么。

这是我的数据的样子:

'data.frame':   150 obs. of  11 variables:
 $ method             : chr  "GEMAX" "GEMAX" "GEMAX" "GEMAX" ...
 $ season_year        : chr  "autumn 2016" "autumn 2016" "autumn 2016" "autumn 2016" ...
 $ station            : chr  "1" "1" "1" "1" ...
 $ sample             : chr  "1" "2" "3" "4" ...
 $ method_year        : chr  "GE_2016" "GE_2016" "GE_2016" "GE_2016" ...
 $ method_year_station: chr  "GE_2016_1" "GE_2016_1" "GE_2016_1" "GE_2016_1" ...
 $ sample_id          : chr  "GE2016_st1_1" "GE2016_st1_2" "GE2016_st1_3" "GE2016_st1_4" ...
 $ year_station       : chr  "autumn 2016 1" "autumn 2016 1" "autumn 2016 1" "autumn 2016 1" ...
 $ Index              : Factor w/ 3 levels "H'","BQI","BBI": 1 1 1 1 1 1 1 1 1 1 ...
 $ index_value        : num  0.897 0.712 0.465 0.394 0.618 ...
 $ PLI                : num  1.54 1.54 1.54 1.54 1.54 ...

这是我用来创建箱形图的代码:

colour_palette_stations <- c("1" = "#E69F00", "2" ="#56B4E9", "3" = "#009E73", "4" = "#0072B2", "5" = "#D55E00", "6" = "#CC79A7")
plot <- ggplot() +
  geom_boxplot(data = melted_indices, aes(x = PLI, y = index_value, fill = station)) +
  scale_fill_manual(name = "Stations", values = colour_palette_stations) +
  ylab("index value") +
  facet_wrap(~Index, scales = "free")
plot

这是最终结果: vertical lines "boxplot"

我将代码与我制作的其他箱形图进行了比较,对我来说看起来也一样。谁能发现我做错了什么?

编辑:输出

dput(melted_indices[, c("PLI", "index_value", "station")])

根据要求:

structure(list(PLI = c(1.53772710912303, 1.53772710912303, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 3.18386506979715, 3.18386506979715, 
3.18386506979715, 3.18386506979715, 3.18386506979715, 1.07451906856368, 
1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
1.23289254801444, 1.23289254801444, 1.23289254801444, 1.23289254801444, 
1.23289254801444, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
1.07451906856368, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
3.18386506979715, 3.18386506979715, 3.18386506979715, 3.18386506979715, 
3.18386506979715, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
1.07451906856368, 1.07451906856368, 1.23289254801444, 1.23289254801444, 
1.23289254801444, 1.23289254801444, 1.23289254801444, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.22989566083404, 1.22989566083404, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 1.07451906856368, 1.07451906856368, 
1.07451906856368, 1.07451906856368, 1.07451906856368, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.22989566083404, 1.22989566083404, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 3.18386506979715, 3.18386506979715, 
3.18386506979715, 3.18386506979715, 3.18386506979715, 1.07451906856368, 
1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
1.23289254801444, 1.23289254801444, 1.23289254801444, 1.23289254801444, 
1.23289254801444, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
1.07451906856368, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
1.22989566083404, 1.22989566083404, 1.22989566083404), index_value = c(0.897090619981485, 
0.711951622706353, 0.46468506468643, 0.394140299815468, 0.618342434570252, 
0.732208541750694, 0.771144765343957, 0.738719220626546, 0.714795171885277, 
0.63193436646543, 0.360062440635905, 0.639064876705551, 0.309470371928486, 
0.716747696239186, 0.57088671777535, 0.556100591377527, 0.790546875085912, 
0.212924224619291, 0.655715668736619, 0.122465013397318, 0.560475196336792, 
0.76374480870866, 0.682037160721369, 0.712266111481877, 0.517847981778762, 
0.807939742621269, 0.887918757951212, 0.591563201935165, 0.540286924182427, 
0.613038025183085, 0.552944939512382, 0.436835521475935, 0.444961266186975, 
0.55980402078392, 0.657561454415399, 0.757651760773378, 0.674035613938481, 
0.585690708435859, 0.673453720038359, 0.566623837226683, 0.738754225841618, 
0.781074907362021, 0.717555674113688, 0.676230731907715, 0.544005131138556, 
0.477135189884872, 0.568129212774203, 0.634005198483357, 0.501294889793803, 
0.682679620240528, 7.73028215031384, 6.50736954661017, 4.74815959988244, 
4.76955926408783, 5.78781958599735, 4.34823995020612, 4.36087197047454, 
4.4585785017997, 3.96532366566562, 4.48632544264735, 4.3348816375348, 
6.30916040409802, 3.8126333411174, 6.14455304188506, 5.78697653434632, 
4.67125473741412, 7.19786102820837, 2.95802508008563, 4.73267592832875, 
2.49180383843773, 5.00230342117648, 6.93222660267001, 8.71659382276766, 
6.83380955704985, 4.75420654883516, 8.71942957192637, 9.34483234519417, 
5.92889854043036, 4.62275111044526, 5.442201480734, 4.47217508348135, 
2.82593192834848, 3.40169709876277, 3.90385978126599, 5.21220443365182, 
6.80273064909952, 6.01946935605469, 5.54545399505807, 6.6354167765275, 
4.60553994532868, 7.71006895018938, 8.71942957192637, 6.86484240640667, 
6.05722967766911, 4.5752542267714, 3.64902653901661, 4.5243007445229, 
4.51793843939088, 4.30813146662702, 4.57644818126768, 0.458728491050711, 
0.36401613713986, 0.262134620587481, 0.24041212559615, 0.312077588938048, 
0.288280165129868, 0.298690312388965, 0.300367625322683, 0.270264693071377, 
0.281564392922554, 0.218474429961861, 0.359598593095251, 0.182087114447219, 
0.35701701152291, 0.320122396809523, 0.273271031084653, 0.415755527830253, 
0.137359209124409, 0.285626236719737, 0.0967782960031763, 0.273538929715971, 
0.395717087076169, 0.445772111088459, 0.376803376197474, 0.258164791615629, 
0.480326447116088, 0.527197439725643, 0.322986879409089, 0.263688294229414, 
0.307314073849529, 0.252672106134595, 0.164608843281875, 0.186595762734099, 
0.238811635258751, 0.310869644258428, 0.389815919043416, 0.339249683737036, 
0.298432829669081, 0.363215178362548, 0.259909371410129, 0.422965903343036, 
0.47605906660965, 0.378943560134184, 0.341039687340247, 0.243183101838468, 
0.210234902948755, 0.272947312948655, 0.285890513496704, 0.250377239119871, 
0.293199920696763), station = c("1", "1", "1", "1", "1", "2", 
"2", "2", "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", 
"4", "5", "5", "5", "5", "5", "6", "6", "6", "6", "6", "1", "1", 
"1", "1", "1", "3", "3", "3", "3", "3", "5", "5", "5", "5", "5", 
"6", "6", "6", "6", "6", "1", "1", "1", "1", "1", "2", "2", "2", 
"2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "5", 
"5", "5", "5", "5", "6", "6", "6", "6", "6", "1", "1", "1", "1", 
"1", "3", "3", "3", "3", "3", "5", "5", "5", "5", "5", "6", "6", 
"6", "6", "6", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", 
"3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "5", "5", "5", 
"5", "5", "6", "6", "6", "6", "6", "1", "1", "1", "1", "1", "3", 
"3", "3", "3", "3", "5", "5", "5", "5", "5", "6", "6", "6", "6", 
"6")), .Names = c("PLI", "index_value", "station"), class = "data.frame", row.names = c(NA, 
-150L))

1 个答案:

答案 0 :(得分:1)

请注意:示例数据中缺少“索引”,因此无法创建所需的facet_warp。

可能的解决方案:手动设置箱形图的宽度...

ggplot() +
  geom_boxplot(data = df, aes(x = PLI, y = index_value, fill = station), width = 0.25) 

enter image description here

样本数据

df <- structure(list(PLI = c(1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 3.18386506979715, 3.18386506979715, 
                       3.18386506979715, 3.18386506979715, 3.18386506979715, 1.07451906856368, 
                       1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
                       1.23289254801444, 1.23289254801444, 1.23289254801444, 1.23289254801444, 
                       1.23289254801444, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
                       1.07451906856368, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       3.18386506979715, 3.18386506979715, 3.18386506979715, 3.18386506979715, 
                       3.18386506979715, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
                       1.07451906856368, 1.07451906856368, 1.23289254801444, 1.23289254801444, 
                       1.23289254801444, 1.23289254801444, 1.23289254801444, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 1.07451906856368, 1.07451906856368, 
                       1.07451906856368, 1.07451906856368, 1.07451906856368, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 3.18386506979715, 3.18386506979715, 
                       3.18386506979715, 3.18386506979715, 3.18386506979715, 1.07451906856368, 
                       1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
                       1.23289254801444, 1.23289254801444, 1.23289254801444, 1.23289254801444, 
                       1.23289254801444, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404, 1.53772710912303, 
                       1.53772710912303, 1.53772710912303, 1.53772710912303, 1.53772710912303, 
                       1.07451906856368, 1.07451906856368, 1.07451906856368, 1.07451906856368, 
                       1.07451906856368, 1.17007093273721, 1.17007093273721, 1.17007093273721, 
                       1.17007093273721, 1.17007093273721, 1.22989566083404, 1.22989566083404, 
                       1.22989566083404, 1.22989566083404, 1.22989566083404), index_value = c(0.897090619981485, 
                                                                                              0.711951622706353, 0.46468506468643, 0.394140299815468, 0.618342434570252, 
                                                                                              0.732208541750694, 0.771144765343957, 0.738719220626546, 0.714795171885277, 
                                                                                              0.63193436646543, 0.360062440635905, 0.639064876705551, 0.309470371928486, 
                                                                                              0.716747696239186, 0.57088671777535, 0.556100591377527, 0.790546875085912, 
                                                                                              0.212924224619291, 0.655715668736619, 0.122465013397318, 0.560475196336792, 
                                                                                              0.76374480870866, 0.682037160721369, 0.712266111481877, 0.517847981778762, 
                                                                                              0.807939742621269, 0.887918757951212, 0.591563201935165, 0.540286924182427, 
                                                                                              0.613038025183085, 0.552944939512382, 0.436835521475935, 0.444961266186975, 
                                                                                              0.55980402078392, 0.657561454415399, 0.757651760773378, 0.674035613938481, 
                                                                                              0.585690708435859, 0.673453720038359, 0.566623837226683, 0.738754225841618, 
                                                                                              0.781074907362021, 0.717555674113688, 0.676230731907715, 0.544005131138556, 
                                                                                              0.477135189884872, 0.568129212774203, 0.634005198483357, 0.501294889793803, 
                                                                                              0.682679620240528, 7.73028215031384, 6.50736954661017, 4.74815959988244, 
                                                                                              4.76955926408783, 5.78781958599735, 4.34823995020612, 4.36087197047454, 
                                                                                              4.4585785017997, 3.96532366566562, 4.48632544264735, 4.3348816375348, 
                                                                                              6.30916040409802, 3.8126333411174, 6.14455304188506, 5.78697653434632, 
                                                                                              4.67125473741412, 7.19786102820837, 2.95802508008563, 4.73267592832875, 
                                                                                              2.49180383843773, 5.00230342117648, 6.93222660267001, 8.71659382276766, 
                                                                                              6.83380955704985, 4.75420654883516, 8.71942957192637, 9.34483234519417, 
                                                                                              5.92889854043036, 4.62275111044526, 5.442201480734, 4.47217508348135, 
                                                                                              2.82593192834848, 3.40169709876277, 3.90385978126599, 5.21220443365182, 
                                                                                              6.80273064909952, 6.01946935605469, 5.54545399505807, 6.6354167765275, 
                                                                                              4.60553994532868, 7.71006895018938, 8.71942957192637, 6.86484240640667, 
                                                                                              6.05722967766911, 4.5752542267714, 3.64902653901661, 4.5243007445229, 
                                                                                              4.51793843939088, 4.30813146662702, 4.57644818126768, 0.458728491050711, 
                                                                                              0.36401613713986, 0.262134620587481, 0.24041212559615, 0.312077588938048, 
                                                                                              0.288280165129868, 0.298690312388965, 0.300367625322683, 0.270264693071377, 
                                                                                              0.281564392922554, 0.218474429961861, 0.359598593095251, 0.182087114447219, 
                                                                                              0.35701701152291, 0.320122396809523, 0.273271031084653, 0.415755527830253, 
                                                                                              0.137359209124409, 0.285626236719737, 0.0967782960031763, 0.273538929715971, 
                                                                                              0.395717087076169, 0.445772111088459, 0.376803376197474, 0.258164791615629, 
                                                                                              0.480326447116088, 0.527197439725643, 0.322986879409089, 0.263688294229414, 
                                                                                              0.307314073849529, 0.252672106134595, 0.164608843281875, 0.186595762734099, 
                                                                                              0.238811635258751, 0.310869644258428, 0.389815919043416, 0.339249683737036, 
                                                                                              0.298432829669081, 0.363215178362548, 0.259909371410129, 0.422965903343036, 
                                                                                              0.47605906660965, 0.378943560134184, 0.341039687340247, 0.243183101838468, 
                                                                                              0.210234902948755, 0.272947312948655, 0.285890513496704, 0.250377239119871, 
                                                                                              0.293199920696763), station = c("1", "1", "1", "1", "1", "2", 
                                                                                                                              "2", "2", "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", 
                                                                                                                              "4", "5", "5", "5", "5", "5", "6", "6", "6", "6", "6", "1", "1", 
                                                                                                                              "1", "1", "1", "3", "3", "3", "3", "3", "5", "5", "5", "5", "5", 
                                                                                                                              "6", "6", "6", "6", "6", "1", "1", "1", "1", "1", "2", "2", "2", 
                                                                                                                              "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "5", 
                                                                                                                              "5", "5", "5", "5", "6", "6", "6", "6", "6", "1", "1", "1", "1", 
                                                                                                                              "1", "3", "3", "3", "3", "3", "5", "5", "5", "5", "5", "6", "6", 
                                                                                                                              "6", "6", "6", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", 
                                                                                                                              "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "5", "5", "5", 
                                                                                                                              "5", "5", "6", "6", "6", "6", "6", "1", "1", "1", "1", "1", "3", 
                                                                                                                              "3", "3", "3", "3", "5", "5", "5", "5", "5", "6", "6", "6", "6", 
                                                                                                                              "6")), .Names = c("PLI", "index_value", "station"), class = "data.frame", row.names = c(NA,