我试图将图例添加到使用R-package dbplot创建的箱线图中。不幸的是我遇到了错误:
"Don't know how to add o to a plot"
这是我的示例代码:(我添加了示例数据,但是从 spark表中加载了数据!)
cars <- c(27,37,9,11,47,29,39,41,18,30)
cars_s <- c(0,99,10,0,0,0,0,0,10,10)
sample_data <- data.frame(cars, cars_s)
sparkTable %>%
filter(cars > -1) %>%
dbplot_boxplot(x = cars_s, var = cars, coef = 1.5) +
ggtitle("Boxplot Title") +
labs(x = "status", y = "cars") +
legend("topright", inset = .05, title = "Status", legend = as.character(c("0", "1", "2"))) +
theme_bw()
我的箱线图的输出看起来像这样,没有图例行
如上所述,我无法添加图例。
答案 0 :(得分:0)
按照FilipW的建议,我离开了功能请求并很快从dbplot的开发人员那里得到了答案。他的回答解决了我的问题。他写信给我说,我应该使用 db_compute_boxplot ,而不是 dbplot_boxplot 。由于db_compute_boxplot返回具有 boxplot计算结果的数据框。因此,可以手动添加boxplot和颜色:
sparkTable %>%
filter(cars > -1) %>%
db_compute_boxplot(x = cars_s, var = cars, coef = 1.5) +
ggplot() +
ggtitle("Boxplot Title") +
labs(x = "status", y = "cars") +
theme_bw() +
geom_boxplot(
aes(
x = cars_s,
ymin = ymin,
lower = lower,
middle = middle,
upper = upper,
ymax = ymax,
color = as.factor(cars_s),
),
stat = "identity"
)
非常感谢!