Me gustaíra realizar un Dumbbell gráfico pero no logro ni añadirle la leyenda ni ajustar a la derecha o izquierda los valores que corresponde a cada categoría.
Parto de este dataframe:
groups_COPD_1 <- structure(data.frame(Episcan_I = c("21.5", "24.1", "10.1", "25.3", "0.0", "2.5", "21.5", "20.3", "68.4", "65.8"),
gr = c("Degree of dyspnea Grade 2", "Expectoration","Degree of dyspnea Grade 3", "Chronic cough",
"Degree of dyspnea Grade 5", "Degree of dyspnea Grade 4", "Asthma",
"Chronic Bronchitis", "Wheezing", "Degree of dyspnea Grade 1"),
Episcan_II = c("59.5", "38.1", " 16.7", " 28.6", " 1.2", " 3.6", " 15.5", " 9.5", " 51.2", " 19.0")))
El cual le he dado formato a cada una de las columnas:
groups_COPD_1$Episcan_I<-as.numeric(groups_COPD_1$Episcan_I)
groups_COPD_1$Episcan_II<-as.numeric(groups_COPD_1$Episcan_II)
groups_COPD_1$diff=sprintf("%f", as.numeric((groups_COPD_1$Episcan_II-groups_COPD_1$Episcan_I)))
groups_COPD_1$diff<-as.numeric(groups_COPD_1$diff)
groups_COPD_1 <- arrange(groups_COPD_1, desc(diff))
groups_COPD_1$diff<-paste(groups_COPD_1$diff,"%")
groups_COPD_1$gr <- factor(groups_COPD_1$gr, levels=rev(groups_COPD_1$gr))
Y he realizaro dicho gráfico, especificando que quiero la leyenda y que quiero los números a la derecha e izquierda de la linea de cada categoría
g1<-groups_COPD_1 %>%
ggplot(aes(x=Episcan_I,xend=Episcan_II,y=gr, group=gr))+
geom_dumbbell(
colour="#b2b2b2",
colour_x ="#9fb059",
colour_xend = "#edae52",
size=5.0,
dot_guide = TRUE,
dot_guide_size = 0.15,
dot_guide_colour = "#b2b2b2",
show.legend = TRUE
)
percent_first <- function(x) {
x <- sprintf("%0.1f%%", round(x, digits = 1))
x
}
g1 + geom_text(data=groups_COPD_1, aes(x=Episcan_I, y=gr, label=percent_first(Episcan_I)),
color="#9fb059", size=5, vjust=2.5,hjust=0.5)+
geom_text(data=groups_COPD_1, color="#edae52", size=5, vjust=2.5,hjust=0.5,
aes(x=Episcan_II, y=gr, label=percent_first(Episcan_II)))+
theme(plot.title=element_text(face="bold"))+
geom_rect(data=groups_COPD_1, aes(xmin=70, xmax=75, ymin=-Inf, ymax=Inf), fill="#efefe3")+
geom_text(data=groups_COPD_1, aes(label=diff, y=gr, x=72.5), fontface="bold", size=5)+
geom_text(data=filter(groups_COPD_1, gr=="Degree of dyspnea Grade 2"), aes(x=72.5, y=gr, label="DIFF"),
color="#7a7d7e", size=5, vjust=-2, fontface="bold")+
scale_x_continuous(expand=c(0,0), limits=c(-3, 76))+
scale_y_discrete(expand=c(0.100,0))+
geom_segment(data=groups_COPD_1, aes(y=gr, yend=gr, x=0, xend=1), color="#b2b2b2", size=0.15)+
theme_bw()+
theme(
axis.title.x = element_text(face="bold"),
axis.title.y = element_text(face="bold"),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(),
axis.ticks = element_blank(),
panel.border = element_blank(),
legend.position = "top",
axis.text.y = element_text(size = 10, face = "bold"),
axis.text.x = element_blank())+
labs(
title ="Change of respiratory symptoms between Episcan I and Episcan II in COPD patients",
x="Percentage (%)", y="Respiratory symptoms",cex.lab=1
)
Tras la aplicación de este código me queda esto:
La leyenda no se ha añadido y los números algunos aparecen a los lados pero otros se solapan. ¿Como puedo solucionar estos problemas? Gracias de antemano