我有一个二分因子变量,我想创建一个条形图。
outcome <- as.factor(c("Yes", "Yes", "Yes", "No", "No", "No", "Yes", "Yes", "No", "No", "Yes", "No", "No", "Yes", "Yes", "Yes", "No", "No", "Yes", "No", "No", "Yes", "Yes", "No"))
我有一个连续变量(min = 0,max = 1),我想创建一个
的直方图prediction <- c(.648, .628, .774, .292, .264, .598, .720, .876, .520, .676, .736, .244, .326, .594, .492, .168, .200, .286, .804, .086, .382, .878, .450,.478)
我想弄清楚如何在同一个地块上叠加两个图表。这就是我到目前为止所做的:
df <- data.frame(outcome, prediction)
ggplot(data=df) +
geom_bar(aes(outcome), alpha = .2, color = "red") +
scale_y_continuous() +
geom_histogram(aes(prediction), alpha = .2, color = "blue", bins = 20)
它很接近,但条形图需要向左移动。我希望条形图“No”从0到.5,“是”从.5到1。
答案 0 :(得分:1)
这会将条形图移到左侧。您仍然需要修复标签。如果这是你想要的,我不是100%,但这只是一个开始。
ggplot(data=df) +
geom_bar(aes(outcome), alpha = .2, color = "red", width = 1, position = position_nudge(x=-0.5)) +
scale_y_continuous() +
geom_histogram(aes(prediction), alpha = .2, color = "blue", bins = 20)