我有这样的数据集(为简化说明而简化):
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)
Run Code Online (Sandbox Code Playgroud)
我想创建一个具有以下属性的气泡图(好像气泡图):
需要说明的是,每个公司都会出现在结果的每一列中,例如,ExxonMobil将接近INSTIT.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()
Run Code Online (Sandbox Code Playgroud)
结果是:
