0

对不起,如果这太基本了。

我正在尝试使用 ggplot2 为我的数据绘制箱线图。但是,我无法将数据转换为正确的格式。

这是我的数据的样子:

头(Final_RMSE_MC)

  N_reg N_var RMSE_MC_1 RMSE_MC_2 RMSE_MC_3 RMSE_MC_4 RMSE_MC_5 RMSE_MC_6 RMSE_MC_7 RMSE_MC_8 RMSE_MC_9 RMSE_MC_10 RMSE_MC_11 RMSE_MC_12 RMSE_MC_13
1     6     5 0.5016800 0.5898132 0.5482860 0.4585713 0.4830320 0.4376286 0.4626646 0.5753290 0.4600453  0.4625784  0.4135086  0.5356082  0.4262005
2    10     5 0.4928764 0.4426350 0.4634775 0.4049509 0.5192989 0.4420706 0.3912822 0.4808609 0.4190173  0.4828170  0.4123871  0.4394507  0.4100748
3     4     4 0.4890946 0.4503532 0.5930480 0.5608510 0.4232696 0.5392966 0.4134308 0.5950408 0.5425955  0.5209573  0.6669176  0.4819051  0.4926042
4     5     4 0.5229090 0.5076377 0.5254299 0.4455789 0.4816532 0.5468765 0.4474718 0.4467224 0.4280381  0.6339686  0.3921858  0.5335065  0.4548194
5     9     4 0.4138625 0.4782089 0.4522069 0.4534526 0.4175361 0.4685324 0.3908619 0.4877251 0.4509520  0.4410600  0.4685804  0.4660575  0.4775753
6     3     3 0.5135749 0.6280533 0.5841148 0.5051640 0.5279784 0.5981735 0.4638461 0.4664253 0.4568787  0.4150206  0.5780827  0.5474891  0.4232878

我想根据 N_var 为除N_reg和之外的所有列融化数据N_var。所以我尝试了

dfm <- melt(Final_RMSE_MC, id.vars = "N_reg")

我得到了

  N_reg variable value
1     6    N_var     5
2    10    N_var     5
3     4    N_var     4
4     5    N_var     4
5     9    N_var     4
6     3    N_var     3

这似乎不对。

我的下一步是使用

ggplot(dfm, aes(x = variable, y = value)) + geom_boxplot()

感谢您的任何建议!

4

1 回答 1

1

You should use both variables N_reg and N_var as id.vars as they are the same for all other variables in one row.

dfm <- melt(Final_RMSE_MC, id.vars = c("N_reg","N_var"))

head(dfm)
  N_reg N_var  variable     value
1     6     5 RMSE_MC_1 0.5016800
2    10     5 RMSE_MC_1 0.4928764
3     4     4 RMSE_MC_1 0.4890946
4     5     4 RMSE_MC_1 0.5229090
5     9     4 RMSE_MC_1 0.4138625
6     3     3 RMSE_MC_1 0.5135749

ggplot(dfm, aes(x = variable, y = value)) + geom_boxplot()
于 2013-05-02T08:08:35.913 回答