我有一个这样的数据集(为了说明目的而简化):
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)
我想创建一个具有以下属性的气泡图(好吧,类似于气泡图):
需要明确的是,每家公司都会出现在结果的每一列中,例如 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/