'Making bubble chart with R

I have a table with the abundance of species in multiple samples. I want to make a bubble chart where in the y-axis I will have the different species, in the x-axis the different samples in which I found those species, while the radius of the bubbles will suggest the relative size of the species.

My table is something like this:

Samples Sample1 Sample2 Sample3 Sample4 Sample5
Species1    12  25  25  25  25
Species2    12  23  23  23  23
Species3    12  21  21  21  21
Species4    12  19  19  19  19
Species5    12  17  17  17  17
Species6    1   15  15  15  15
Species7    5   13  13  13  13

I want to end up with something like this:

r


Solution 1:[1]

With tidyr and ggplot2:

library(tidyverse)

data <- read.table(text = "Samples Sample1 Sample2 Sample3 Sample4 Sample5 
                   Species1 12 25 25 25 25 
                   Species2 12 23 23 23 23 
                   Species3 12 21 21 21 21 
                   Species4 12 19 19 19 19 
                   Species5 12 17 17 17 17 
                   Species6 1 15 15 15 15 
                   Species7 5 13 13 13 13", header = T)
data %>% 
  gather(sample, value, Sample1:Sample5) %>%
  ggplot(aes(sample, Samples, size = value)) + 
  geom_point()

Solution 2:[2]

Here's a base solution:

df <- read.table(text="Samples Sample1 Sample2 Sample3 Sample4 Sample5 
Species1 12 25 25 25 25 
Species2 12 23 23 23 23 
Species3 12 21 21 21 21 
Species4 12 19 19 19 19 
Species5 12 17 17 17 17 
Species6 1 15 15 15 15 
Species7 5 13 13 13 13", header=T)

df <- reshape2::melt(df)
par(mar=c(4.1,6.1,2.1,4.1), xpd=T)
plot(y=as.integer(df$Samples), x=as.integer(df$variable), pch=20, cex=df$value/10, bty="n", axes = F, xlab="", ylab="")
axis(2, at = unique(as.integer(df$Samples)), labels = levels(df$Samples), line = 0.5, las=2)
axis(1, at = unique(as.integer(df$variable)), labels = levels(df$variable), line = 0.5)
legend(y=max(as.integer(df$Samples)/1.5), x=max(as.integer(df$variable))+0.25, legend = c(1,5,10,15,20,25), pch = 20, bty="n", pt.cex=c(1,5,10,15,20,25)/10)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yeedle
Solution 2 emilliman5