简体   繁体   中英

bubble plot using ggplot2

I need to create a bubble plot similar to this one:

I used ggplot2 to create a one-sided bubble plot using code from this post .

This created the y axis and the x axis on the right side, but I need to have the x axis on both sides. Any suggestions?

This my code:

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) )    

Sample of data:

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")

It probably makes more sense to convert the dataframe to long format & place all the variables meant for the x-axis into the same column. Thereafter, you can plot everything in ggplot in one shot, using facet to distinguish between Contribution & Research:

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"))

情节

(Note: I tweaked the size ranges based on what looked reasonable for my image size. Your resolution may differ.)

Here is one possible solution to replicate the desired plot above. Plot each individual component separately, including the y labels as a geom_label in ggplot2 , then use cowplot to align and arrange them into a single plot.

I used the grid2 long format data and built from the initial code provided by @Z.Lin

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

# Find min and max count sizes to scale bubbles
Research.max <- max(grid2[grid2$facet == "Research", ]$count, na.rm = T)
Contribution.max <- max(grid2[grid2$facet == "Contribution", ]$count, na.rm = T)
Research.min <- min(grid2[grid2$facet == "Research", ]$count, na.rm = T)
Contribution.min <- min(grid2[grid2$facet == "Contribution", ]$count, na.rm = T)

# Plot each component in ggplot2, set similar parameters and theme components for each plot with adjustments to position
library(ggplot2)

Research <- ggplot(grid2[grid2$facet == "Research", ], 
               aes(x = type, y = Level, size = count, label = count)) +
  xlab("Research") + ggtitle("") +
  geom_point(shape = 21, fill = "white") +
  geom_text(size = 3) +
  scale_radius(range = c(4, (Research.max+Research.min+4)/2), guide = F) +
  theme(panel.grid.major = element_line(linetype = 2, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 1),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0.5,b=1,l=0, unit="cm"))

Contribution <- ggplot(grid2[grid2$facet == "Contribution", ], 
                   aes(x = type, y = Level, size = count, label = count)) +
  xlab("Contribution") + ggtitle("") +
  geom_point(shape = 21, fill = "white") +
  geom_text(size = 3) +
  scale_radius(range = c(4, (Contribution.max+Contribution.min+4)/2), guide = F) +
  scale_y_discrete(position = "right") + 
  theme(panel.grid.major = element_line(linetype = 2, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 0),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0,b=1,l=0.5, unit="cm"))

Y_axis <- ggplot(grid2[grid2$facet == "Contribution", ], 
             aes(x = "", y = Level, label = Level)) +
  geom_label(size = 10) +  
  xlab("") + ggtitle("Level") +
  theme(panel.grid.major.y = element_line(linetype = 2, color = "black"),
    panel.grid.major.x = element_line(linetype = 1, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 0.5),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0,b=1,l=0, unit="cm"),
    plot.title = element_text(hjust=0.5))

# Arrange ggplot objects side-by-side using cowplot, define relative widths
library(cowplot)
plot_grid(Contribution, Y_axis, Research, align = "v", axis = "tb", nrow = 1, rel_widths = c(5/10, 1/10, 4/10))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM