克利夫兰点图ggplot2

时间:2016-12-02 14:58:47

标签: python r matplotlib plot ggplot2

在最近碰巧遇到的TIMSS report中,有一个情节(如下所示),在我看来是非常具有沟通性的。我已经读到这样的情节被称为克利夫兰点图,尽管这也增加了置信区间。我想知道它是否可以在ggplot2或matplotlib中重现。欢迎提供所有提示。 plot http://timss2015.org/wp-content/uploads/filebase/science/1.-student-achievement/science-distribution-of-science-achievement-grade-4-table.jpg

2 个答案:

答案 0 :(得分:3)

使用iris数据集:

library(dplyr)
library(ggplot2)

plot_data <- iris %>% 
  group_by(Species) %>% 
  summarise_each(funs(mean, sd, n(), q95=quantile(., 0.95), q75=quantile(., 3/4), q25=quantile(., 1/4),  q5 = quantile(., 0.05)), Sepal.Length) %>% 
  mutate(se = sd/sqrt(n),
         left95 = mean - 2*se,
         right95 = mean + 2*se)


ggplot(plot_data, aes(x = Species, y = mean)) +
  geom_crossbar(aes(ymin = q5, ymax = q95), fill = "aquamarine1",  color = "aquamarine1", width = 0.2) +
  geom_crossbar(aes(ymin = q25, ymax = q75), fill = "aquamarine4",  color = "aquamarine4", width = 0.2) +
  geom_crossbar(aes(ymin = left95, ymax = right95), fill = "black", color = "black", width = 0.2) +
  coord_flip() +
  theme_minimal()

enter image description here

这应该为您提供如何使用ggplot2来完成此任务的要点。您提供的数据可以轻松使用,无需dplyr总结。

答案 1 :(得分:1)

克利夫兰点图将数据集的所有值显示为在x轴上有序的点,并且仅显示数据集中的位置(而不是其他答案中的平均值)。使用ggplot2(再次以iris数据集为例):

ggplot(iris) + geom_point(aes(y=Sepal.Length,x=seq(1,length(Sepal.Length),1))) 

如果每一行都有唯一的ID,则可以使用它代替x=seq(1,length(Sepal.Length),1),因为geom_point的Y和X都是必需的。

Cleveland dot plot