cat cat
cat cat

Reputation: 65

plot different size of circle with different color and label on the figure in R

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)

Upvotes: 0

Views: 333

Answers (2)

br00t
br00t

Reputation: 1614

Base R solution: See below for how to color and change size of points. Regarding positioning of text labels: my suggestion would to replace the text labels with a legend to avoid having to write a lot of code to position and size text labels in a way that is readable, which can get quite tedious. There are some packages such as directlabel and ggrepel that do this for you, but they do not work with ggplot, trellis and not base R plot objects

df <- data.frame(names_of_disease = 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 = factor(df$names_of_disease), # use names_of_disease factor values to set point color 
     pch = 19, 
     cex = 10 * df$group1_north, # use group1_north as a scaling factor for point size
     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,
     bty = 'n'
) 

# replace text labels with a legend
legend('topright', 
       col = factor(df$names_of_disease), 
       legend = df$names_of_disease, 
       pch = 19, 
       bty = 'n')

diseases plot

Upvotes: 2

Seth
Seth

Reputation: 3854

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 reprex v2.0.2

Upvotes: 2

Related Questions