我有一个这样的数据集(为了说明目的而简化):

zz <- textConnection("Company   Market.Cap  Institutions.Own    Price.Earnings  Industry
 ExxonMobil 405.69  50% 9.3 Energy
 Citigroup  156.23  67% 18.45   Banking
 Pfizer 212.51  73% 20.91   Pharma
 JPMorgan   193.1   75% 9.12    Banking
 ")
Companies <- read.table(zz, header= TRUE)
close(zz)

我想创建一个具有以下属性的气泡图(好吧,类似于气泡图):
  • 每个泡沫都是一家公司,泡沫的大小与市值相关,
  • 与工业相关的泡沫颜色,
  • ,x 轴有两个类别,Industry.Own 和 Price.Earnings,
  • 和 y 轴是 1-10 的比例,每个公司的值都被标准化为该比例。 (我当然可以在 R 之外进行规范化,但我相信 R 使这成为可能。)

  • 需要明确的是,每家公司都会出现在结果的每一列中,例如 ExxonMobil 将出现在 Institutions.Own 列和 Price.Earnings 列的底部附近;理想情况下,公司名称应出现在其两个气泡中或旁边。

    最佳答案

    我认为这涉及您的所有观点。注意 - 由于 Institutions.Own,您的 % 被作为一个因素读入......为了方便起见,我只是删除了它......你需要在某处解决这个问题。完成后,我将使用 ggplot 并相应地映射您的不同美学。如果需要,您可以摆弄轴标题等。

    #Requisite packages
    library(ggplot2)
    library(reshape2)
    #Define function, adjust this as necessary
    rescaler <- function(x) 10 * (x-min(x)) / (max(x)-min(x))
    #Rescale your two variables
    Companies$Inst.Scales <- with(Companies, rescaler(Institutions.Own))
    Companies$Price.Scales <- with(Companies, rescaler(Price.Earnings))
    #Melt into long format
    Companies.m <- melt(Companies, measure.vars = c("Inst.Scales", "Price.Scales"))
    #Plotting code
    ggplot(Companies.m, aes(x = variable, y = value, label = Company)) +
      geom_point(aes(size = Market.Cap, colour = Industry)) +
      geom_text(hjust = 1, size = 3) +
      scale_size(range = c(4,8)) +
      theme_bw()
    

    结果是:

    关于r - 如何在 R 中创建复杂的气泡图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16578645/

    10-12 23:18