【问题标题】:plot different size of circle with different color and label on the figure in R在R中的图形上绘制具有不同颜色和标签的不同大小的圆圈
【发布时间】:2022-11-03 23:39:51
【问题描述】:

我面临以下问题:

1-我不知道如何控制圆的大小,所以当百分比的组数很大时,大小应该变大。例如,group1_north 的第一个点是 97%(0.97) 我希望它是一个比 8.6%(0.086) 更大的圆。

2-我不知道如何使每个圆圈的颜色不同。

3-图上的标签很难控制,尤其是长文本。如何控制大小和换行使其可读。

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)

【问题讨论】:

    标签: r


    【解决方案1】:

    这使用 ggplot2 和扩展。我没有让标签换行,但您可以添加换行符 (" ") 在您希望它们包装的特定标签中。

    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))
    

    创建于 2022-11-03,reprex v2.0.2

    【讨论】:

      【解决方案2】:

      基础 R 解决方案:请参阅下文了解如何着色和更改点的大小。关于文本标签的定位:我的建议是用图例替换文本标签,以避免编写大量代码以可读的方式定位和调整文本标签的大小,这可能会变得非常乏味。有一些软件包,例如 directlabelggrepel 可以为您执行此操作,但它们不适用于 ggplot、trellis 而不是基本 R 绘图对象

      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 = 4 * 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
      ) 
      
      # replace text labels with a legend
      legend('topright', 
             col = factor(df$names_of_disease), 
             legend = df$names_of_disease, 
             pch = 19, 
             bty = 'n')
      # text(group1_north -0.02 ~algorithm1, labels = names_of_disease, data = df, cex.main = .9, font = 8)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-14
        • 1970-01-01
        • 2018-04-26
        相关资源
        最近更新 更多