gpt4 book ai didi

r - 使用ggplot2的气泡图

转载 作者:行者123 更新时间:2023-12-04 14:41:33 24 4
gpt4 key购买 nike

我需要创建一个类似于此的气泡图:

我使用 ggplot2 使用 this post 中的代码创建了一个单边气泡图.

这在右侧创建了 y 轴和 x 轴,但我需要在两侧都有 x 轴。有什么建议吗?

这是我的代码:

grid <- read.csv("data.csv", sep=",")

grid$Variability <- as.character(grid$Variability)
grid$Variability <- factor(grid$Variability, levels=unique(grid$Variability))

grid$Research <- as.character(grid$Research)
grid$Research <- factor(grid$Research, levels=unique(grid$Research))

grid$Contribution <- as.character(grid$Contribution)
grid$Contribution <- factor(grid$Contribution, levels=unique(grid$Contribution))

library(ggplot2)
ggplot(grid, aes(Research, Variability))+
geom_point(aes(size=sqrt(grid$CountResearch*2 /pi)*7.5), shape=21, fill="white")+
geom_text(aes(label=CountResearch),size=4,hjust=0.5,vjust=0.5)+
scale_size_identity()+
theme(panel.grid.major=element_line(linetype=2, color="black"),
axis.title.x=element_text(vjust=-0.35,hjust=1),
axis.title.y=element_text(vjust=0.35),
axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5) )

数据样本:

structure(list(Variability = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("C",
"R", "D", "A"), class = "factor"),
Research = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Op",
"Maint", "Evol", "Re", ""), class = "factor"),
CountResearch = c(5L, 21L, 12L, 3L, NA, 1L, 1L, 6L, NA, NA,
NA, 16L, 27L, 30L, NA, 22L, 4L, 18L, 4L, NA), Contribution = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L), .Label = c("Struct", "Log", "Func",
"Synt", "Behav"), class = "factor"), CountContribution = c(12L,
27L, 5L, 25L, 13L, 0L, 8L, 1L, 1L, 3L, 59L, 37L, 8L, 71L,
2L, 22L, 5L, 0L, 23L, 22L)), .Names = c("Level", "Research",
"CountResearch", "Contribution", "CountContribution"), row.names = c(NA,
-20L
), class = "data.frame")

最佳答案

将数据框转换为长格式并将用于 x 轴的所有变量放在同一列中可能更有意义。此后,您可以一次性绘制 ggplot 中的所有内容,使用 facet 区分贡献和研究:

library(dplyr)
grid2 <- rbind(grid %>%
select(Variability, Research, CountResearch) %>%
rename(type = Research, count = CountResearch) %>%
mutate(facet = "Research") %>%
na.omit(),
grid %>%
select(Variability, Contribution, CountContribution) %>%
rename(type = Contribution, count = CountContribution) %>%
mutate(facet = "Contribution"))

ggplot(grid2,
aes(x = type, y = Variability,
size = count, label = count)) +
geom_point(shape = 21, fill = "white") +
geom_text(size = 3) +
scale_size(range = c(5, 20), guide = F) +
facet_grid(~facet, scales = "free_x") +
theme(panel.grid.major = element_line(linetype = 2, color = "black"))

plot

(注意:我根据我的图像尺寸看起来合理的尺寸调整了尺寸范围。您的分辨率可能会有所不同。)

关于r - 使用ggplot2的气泡图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46413926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com