Home > Software design >  plot different size of circle with different color and label on the figure in R
plot different size of circle with different color and label on the figure in R

Time:11-04

I'm facing an issue with the following :

1- I don't know how to control the size of the circle, so the size should get larger when the number of groups in percent is large. For example, group1_north's first point is 97%(0.97) I want that to be a larger circle than 8.6%(0.086).

2- I don't know how to make each circle in a different color.

3- The label on the figure is very difficult to control, especially with long text. How to control the size and the wrap so it can be readable.

df=data.frame(names_of_dissess=c("Hib Disease_type1","Hepatitis_type1","Flu (Influenza)_type1","Ebola_type1",
                                 "Coronaviruses_type1","Japanese Encephalitis_type1"),
              algorithm1=c(0.00,0.29,0.11,0.21,0.25,0.29)
              ,group1_north=c(0.97,0.086,0.34,0.11,0.086,0.11)
             
              ) 

par( mar=c(6, 6, 4, 4),xpd = TRUE )

plot(group1_north ~algorithm1,
     col="lightblue",
     pch=19,
     cex=2,
     data=df,
     xlab = "algorithm1",
     ylab = "group1_north %",
     xlim=c(0.0,0.3),
     ylim=c(0.0,1),   
     main = "algorithm1 behavior",
     font.main=10,
     family = "A",
     cex.main=1.1,
     cex.lab=0.9
)  


text(group1_north -0.02 ~algorithm1, labels=names_of_dissess,data=df, cex.main =.9, font=8)

CodePudding user response:

This uses ggplot2 and an extension. I didn't make the labels wrap, but you could add newline characters ("\n") within specific labels wherever you want them to wrap.

library(ggplot2)
library(ggrepel)

df=data.frame(names_of_dissess=c("Hib Disease_type1","Hepatitis_type1","Flu (Influenza)_type1","Ebola_type1",
                                 "Coronaviruses_type1","Japanese Encephalitis_type1"),
              algorithm1=c(0.00,0.29,0.11,0.21,0.25,0.29)
              ,group1_north=c(0.97,0.086,0.34,0.11,0.086,0.11)
              
) 

ggplot(df, aes(algorithm1, group1_north))  
  geom_point(aes(size = group1_north,
                 color = names_of_dissess),
             show.legend = FALSE)  
  geom_text_repel(aes(label = names_of_dissess))

Created on 2022-11-03 with diseases plot

  •  Tags:  
  • r
  • Related