3

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")
user8042455
  • 59
  • 1
  • 4
  • Please provide a reproducible example, e.g. your data. For instance you can paste the output of dput(data), as well as the ggplot2 code you have created.https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Djork Sep 25 '17 at 20:51
  • updated the body as requested. – user8042455 Sep 26 '17 at 14:08
  • @user8042455 no you haven't. Please use `dput` to provide a sample of your data. – Mako212 Sep 26 '17 at 15:18
  • sorry I don't understand this dput.. I am new to use R code and ggplot2 – user8042455 Sep 26 '17 at 15:54
  • Paste the output of `dput(grid)` where grid is your data frame in long format being plotted in ggplot2. If grid is huge, then only provide a subset e.g. `dput(gridsub)` – Djork Sep 26 '17 at 19:40
  • updated the body as requested – user8042455 Sep 27 '17 at 15:38
  • Does this answer your question? [Categorical bubble plot for mapping studies](https://stackoverflow.com/questions/15840926/categorical-bubble-plot-for-mapping-studies) – Matthias A. Eckhart Nov 22 '20 at 18:55

2 Answers2

7

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

plot

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

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Thanks! working good.. but, is there a way to have the y axis in the middle between the two facets? – user8042455 Sep 29 '17 at 05:14
  • @user8042455 Not by ggplot design, but you can look at the workaround [here](https://stackoverflow.com/questions/23318411/faceted-ggplot-with-y-axis-in-the-middle). – Z.Lin Sep 29 '17 at 05:26
  • @user8042455 I had a similar problem before, alternatively you can plot the y-axis labels as a `geom_label` separately, as well as the "Contribution" panel and "Research" panel as individual ggplot objects, then use `cowplot` to arrange them side-by-side. This would allow you to place the y-axis labels in the middle. See below. – Djork Sep 29 '17 at 20:21
3

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

enter image description here

Djork
  • 3,319
  • 1
  • 16
  • 27