How to create a complex bubble chart in R

I have a data set like this (simplified to illustrate):

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) 

I would like to create a bubble chart (well, something like a bubble chart) with the following properties:

  • each bubble is a company, the size of the bubble is tied to market capitalization,
  • bubble color industry related
  • with an x ​​axis, which has two categories: Industries.Own and Price.Earnings,
  • and the y axis is a scale of 1-10, each of which corresponds to the norm. (I could, of course, normalize outside of R, but I believe that R makes this possible.)

To be clear, each company will appear in each column of the result, for example ExxonMobil will be at the bottom of the Institutions.Own column and the Price.Earnings column; ideally, the company name will appear in or near both bubbles.

+4
source share
1 answer

I think this applies to all your questions. Note. Your Institutions.Own reads as a factor due to % ... I just deleted it for simplicity of time ... you need to access it somewhere. Once this is done, I will use ggplot and match your aesthetics accordingly. You can play with the names of the axes, etc., if you need.

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

Results in:

enter image description here

+6
source

Source: https://habr.com/ru/post/1481082/


All Articles