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

问题描述 投票:1回答:1

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

我很感谢你的帮助,并附上了Akseer et al的示例图,我想要绘制的图形。

Here is sample data to reproduce this Figure.

图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]
r plot ggplot2 r-plotly
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) 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.