如何使用 ggplot 或 plotly 在具有多个级别的分类 Y 上重现绘制数字 X 的图形?

How to reproduce a graph plotting numerical X over a categorical Y with multiple levels using ggplot or plotly?

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

非常感谢您的帮助,并附上了 Akseer et al of what the graph I want to draw. 的示例图。

这是重现此图的样本数据。

图 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]

这是第一个图表的示例:首先将数据重新排列为长格式并使用 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)