ggplot 中的箱线图给出了意外的输出
boxplot in ggplot gives unexpected output
我想使用 ggplot 绘制分组箱线图。类似于下图:
下面请看我的数据中的示例(10 行):
alpha colsample_bytree best_F1
35 0.00 0.5 0.5825656
78 0.10 0.3 0.4716612
68 0.00 0.3 0.4714286
27 0.40 1.0 0.4786216
49 0.15 0.5 0.4943968
62 0.00 0.3 0.4938805
70 0.00 0.3 0.4849785
73 0.10 0.3 0.4997061
59 0.30 0.5 0.4856369
88 0.20 0.3 0.4552402
sort(unique(data$alpha))
0 0.1 0.15 0.2 0.3 0.4
sort(unique(data$colsample_bytree))
0.3 0.5 1
我的代码如下:
library(ggplot2)
library(ggthemes)
ggplot(data, aes(x= colsample_bytree, y = best_F1, fill = as.factor(alpha))) +
geom_boxplot(alpha = 0.5, position=position_dodge(1)) + theme_economist() +
ggtitle("F1 for alpha and colsample_bytree")
这会产生以下情节:
和以下警告:
Warning message:
"position_dodge requires non-overlapping x intervals"
由于变量 colsample_bytree 取 3 个离散值,而变量 alpha 取 6 个,我希望看到 3 组箱线图——每组由 6 个箱线图组成,对应于不同的 alpa 值,每组定位在 colsample_bytree 的不同值,即0.3、0.5 和 1。
我希望箱线图不会像我引用的示例那样重叠。
在使用 ggplot
命令绘制数据之前,您只需包含 data$colsample_bytree <- as.factor(data$colsample_bytree)
。
我想使用 ggplot 绘制分组箱线图。类似于下图:
下面请看我的数据中的示例(10 行):
alpha colsample_bytree best_F1
35 0.00 0.5 0.5825656
78 0.10 0.3 0.4716612
68 0.00 0.3 0.4714286
27 0.40 1.0 0.4786216
49 0.15 0.5 0.4943968
62 0.00 0.3 0.4938805
70 0.00 0.3 0.4849785
73 0.10 0.3 0.4997061
59 0.30 0.5 0.4856369
88 0.20 0.3 0.4552402
sort(unique(data$alpha))
0 0.1 0.15 0.2 0.3 0.4
sort(unique(data$colsample_bytree))
0.3 0.5 1
我的代码如下:
library(ggplot2)
library(ggthemes)
ggplot(data, aes(x= colsample_bytree, y = best_F1, fill = as.factor(alpha))) +
geom_boxplot(alpha = 0.5, position=position_dodge(1)) + theme_economist() +
ggtitle("F1 for alpha and colsample_bytree")
这会产生以下情节:
和以下警告:
Warning message:
"position_dodge requires non-overlapping x intervals"
由于变量 colsample_bytree 取 3 个离散值,而变量 alpha 取 6 个,我希望看到 3 组箱线图——每组由 6 个箱线图组成,对应于不同的 alpa 值,每组定位在 colsample_bytree 的不同值,即0.3、0.5 和 1。
我希望箱线图不会像我引用的示例那样重叠。
在使用 ggplot
命令绘制数据之前,您只需包含 data$colsample_bytree <- as.factor(data$colsample_bytree)
。