1

我想使用 ggplot 或其他包在 R 中绘制一个图表,显示分类 Y 在数字 X 上的水平。

感谢您的帮助,并附上了Akseer 等人的示例图,其中包含我想要绘制的图表。.

这是重现此图的示例数据。

图 A 的数据:

Interventions<-c("CPR", "ANC 1+","ANC 4+", "SBA","Caesarian section",
          "Early breastfeeding", "Exclusive breastfeeding at 6 months","BCG", "DTP3",
          "OPV3", "Measles vaccine",
          "Fully immunised", "Vitamin A+", "ORT",
          "Pneumonia care", "Improved water",
          "Sanitation")

Poorest<- (1/5)*(sample(1:100, 17, replace=TRUE))
Poorer<-(2/5)*(sample(1:100, 17, replace=TRUE))
Middle<-(3/5)*(sample(1:100, 17, replace=TRUE))
Richer<-(4/5)*(sample(1:100, 17, replace=TRUE))
Richest<-sample(1:100, 17, replace=TRUE)

mydata_A<-data.frame(Interventions, Poorest,Poorer, Middle, Richer, Richest)
rownames(mydata_A) <- mydata_A[,1]
dtFig3A<- mydata_A[,-1]
4

1 回答 1

3

这是第一个图表的示例:首先将数据重新排列为长格式并使用geom_pointgeom_errorbarh进行实际映射。剩下的只是化妆。

library(tidyverse)

dtFig3A %>%
  rownames_to_column() %>% 
  gather(key, value, 2:6) %>% #convert to long format
  mutate(key = factor(key, levels = c("Poorest", #relevel key factor
                                      "Poorer",
                                      "Middle",
                                      "Richer",
                                      "Richest"))) %>%
  group_by(rowname) %>%
  mutate(min = min(value),
         max = max(value)) %>% #calculate min and max in each group for errorbarh
  ggplot()+
  geom_errorbarh(aes(y = rowname,
                     xmin = min,
                     xmax = max,
                     group = key),
                 height = 0) +
  geom_point(aes(y = rowname,
                 x = value,
                 fill = key),
             color = "grey20",
             shape = 21,
             size = 3) +
  theme_classic() +
  theme(legend.position = "top",
        legend.title = element_blank())+
  ylab("Intervention")+
  xlab("Coverage [%]") +
  scale_fill_brewer(type = "seq",
                    palette = 3) 

在此处输入图像描述

于 2018-03-02T10:19:53.040 回答