![](/img/trans.png)
[英]In R, how do I create a bar graph using a categorical x and the average of a numeric y with ggplot2?
[英]How to reproduce a graph plotting numerical X over a categorical Y with multiple levels using ggplot or plotly?
我想在R中使用ggplot或其他包繪制一個圖表,顯示分類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]
以下是第一個圖的示例:首先將數據重新geom_point
為長格式,然后使用geom_point
和geom_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.