使用ggplot2的气泡图

use*_*455 3 plot r ggplot2

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

我曾经 ggplot2使用这篇文章中的代码创建一个单面气泡图。

这在右侧创建了 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) )    
Run Code Online (Sandbox Code Playgroud)

数据样本:

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")
Run Code Online (Sandbox Code Playgroud)

Z.L*_*Lin 7

将数据帧转换为长格式并将所有用于 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"))
Run Code Online (Sandbox Code Playgroud)

阴谋

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