多个图表中的多个图例,在 R 中分组条形线

问题描述 投票:0回答:1

当我查看我的旧项目时,我注意到我的报告中没有包含图表的代码。 你能帮我如何得到下面的图表吗?

数据表,

分数 变量 实际 泊伊斯
0 离开 0.316 0.314
0 0.228 0.213
1 离开 0.366 0.363
1 0.313 0.329
2 离开 0.205 0.210
2 0.248 0.254
3 离开 0.081 0.081
3 0.137 0.131
4 离开 0.016 0.023
4 0.055 0.050
5 离开 0.009 0.005
5 0.003 0.015
6 离开 0.003 0.001
6 0.006 0.004

我想要得到的图表,

The required graph

谢谢你, 维利

r ggplot2 geom-bar
1个回答
0
投票

这让你相当接近

library(ggplot2)

ggplot(within(df, variable <- factor(variable, c("home", "away"))),
       aes(Score, Actual, fill = variable, color = variable)) +
  geom_col(position = position_dodge(width = 0.9), color = NA) +
  geom_line(aes(y = Pois), position = position_dodge(width = 0.9),
            linewidth = 1.5) +
  geom_point(aes(y = Pois), position = position_dodge(width = 0.9),
             fill = NA, size = 4) +
  scale_fill_manual("Actual", values = c("#ffbca1", "#62c9c3")) +
  scale_color_manual("Poisson", values = c("#cd5c5c", "#006400")) +
  scale_x_continuous(breaks = 0:6) +
  labs(x = "Goals per match", y = "Proportion of matches",
       title = "Number of Goals per Match (TSL 2015-2016 Season)") +
  theme_classic(base_size = 20) +
  theme(text = element_text(face = "bold"),
        legend.position = c(0.75, 0.85),
        legend.box.background = element_rect(color = "black", fill = "white"),
        legend.background = element_blank(),
        legend.box = "horizontal",
        panel.border = element_rect(fill = NA, color = "black"))

© www.soinside.com 2019 - 2024. All rights reserved.