先看下整体用法
ggballoonplot(
data,#数据集
x = NULL,x轴向量
y = NULL,y轴向量
size = "value",#气球大小依据
facet.by = NULL,#气球形状选择
size.range = c(1, 10),#气球大小选择范围
shape = 21,#气球形状
color = "black",#气球边框颜色
fill = "gray",#气球填充颜色
show.label = FALSE,#是否显示每个气球代表的具体大小
font.label = list(size = 12, color = "black"),#示每个气球代表的具体大小的字体设定
rotate.x.text = TRUE,#是否旋转标注字体
ggtheme = theme_minimal(),#画板主题
...)
以下是特殊重要形式参数的选择依据原文





以下代码实战
my_cols <- c("#0D0887FF", "#6A00A8FF", "#B12A90FF",
"#E16462FF", "#FCA636FF", "#F0F921FF")#定义色板
data <- read.delim(system.file("demo-data/housetasks.txt", package = "ggpubr"),row.names = 1)
data

首先绘制基本图形
ggballoonplot(data)

更换颜色为固定颜色
ggballoonplot(data,
color = "#0073C2FF",
fill = "#0073C2FF")

以下根据表格的数值填充气球的颜色
ggballoonplot(data, fill = "value")+ #根据数值填充颜色
scale_fill_gradientn(colors = "my_cols") #指定渐变色的色板

以下指定气球的形状
ggballoonplot(data, fill = "value", shape = 23)+
gradient_fill(c("blue", "white", "red"))

以下设定气球固定颜色及大小并显示具体数值
ggballoonplot(data,
fill = "value",
color = "lightgray",#设定气球固定颜色
size = 10, #设定气球固定大小
show.label = TRUE)+#气球显示具体数值
gradient_fill(c("blue", "white", "red"))

数据处理中更多的应用长数据集,以下首先构建长数据集并应用此数据集画图
carnames <- c("bmw","renault","mercedes","seat")
carcolors <- c("red","white","silver","green")
datavals <- round(rnorm(16, mean=100, sd=60),1)#取16个正态分布数,留1位小数
car_data <- data.frame(Car = rep(carnames,4),
Color = rep(carcolors, c(4,4,4,4) ),#用each=4更简略
Value=datavals )
car_data

ggballoonplot(car_data,
x = "Car",
y = "Color",
size = "Value",#大小指定数值变量列名
fill = "Value") +#填充颜色指定数值变量列名
scale_fill_gradientn(colors = my_cols)+
guides(size = F) #不显示气球大小的图例

以下进行分面展示
data("Titanic")
dframe <- as.data.frame(Titanic)
head(dframe)

以下分面画图
ggballoonplot(dframe,
x = "Class",
y = "Sex",
size = "Freq",
fill = "Freq",
facet.by = c("Survived", "Age"),#分面依据,可以设多个组成向量格式
ggtheme = theme_bw())+
scale_fill_gradientn(colors = my_cols)

网友评论