R,GGPlot2& geom_pointrange

时间:2018-02-07 16:22:05

标签: r ggplot2

我有一些示例数据,我试图将其转换为类似线条的图表。

sample <- data.frame(inst=c('School1', 'School1', 'School1', 'School1', 'School2', 'School2', 'School2', 'School2', 'School3', 'School3', 'School3', 'School3'), variable=c('Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75'), peer_min=c(50, 84, 61, 83, 40, 60, 55, 85, 52, 75, 75, 87), peer_max=c(66, 95, 77, 90, 55, 85, 72, 91, 67, 83, 84, 95), peer_mean=c(58.0, 89.5, 69.0, 86.5, 47.5, 72.5, 63.5, 88.0, 59.5, 79.0, 79.5, 91.0), inst_value=c(55, 93, 65, 95, 60, 70, 65, 80, 60, 85, 77, 89))

从该样本数据中,我构建了这个图

e <- ggplot(sample, aes(x=inst, y=peer_mean, ymin = peer_min, ymax = peer_max, color=variable)) + geom_pointrange() + facet_wrap(~variable)

enter image description here

我的问题是,如何在这些行上添加sample$inst_value作为不同的点?

提前谢谢!

1 个答案:

答案 0 :(得分:2)

只需添加geom_point()的另一个图层及其自身的美学效果:

sample %>%
  ggplot(aes(x=inst, y=peer_mean, ymin = peer_min, ymax = peer_max, color=variable)) + 
  geom_pointrange() + 
  geom_point(aes(x = inst, y = inst_value)) +
  facet_wrap(~variable)