【问题标题】:Cleveland dot plot in ggplot2ggplot2中的克利夫兰点图
【发布时间】:2017-04-17 12:50:43
【问题描述】:

在最近碰巧遇到的TIMSSreport中,有一个情节(如下图)在我看来是很有交流性的。我读过这样的图被称为克利夫兰点图,尽管这个图也增加了置信区间。我想知道它是否可以在 ggplot2 或 matplotlib 中复制。欢迎所有提示。
(来源:timss2015.org)

【问题讨论】:

标签: python r matplotlib plot ggplot2


【解决方案1】:

Cleveland [edited] 点图将数据集的所有值显示为在 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 都是美学要求

【讨论】:

【解决方案2】:

使用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()

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

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 2019-12-01
    • 2018-10-10
    • 2018-10-04
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多