KrislilKris
KrislilKris

Reputation: 11

Plot a bubble chart from a table using multiple columns in the x-axis

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:

(http://www.frontiersin.org/files/Articles/141298/fmicb-06-00901-HTML/image_m/fmicb-06-00901-g004.jpg)

Upvotes: 0

Views: 2362

Answers (1)

Richard Telford
Richard Telford

Reputation: 9923

x <- read.table(text="Species 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=TRUE)

require(reshape2)
require(ggplot2)

xm <- melt(x, id.vars = "Species", variable.name="Samples", value.name = "Size")
str(xm)

ggplot(xm, aes(x = Samples, y = Species)) +
  geom_point(aes(size = Size)) + 
  scale_size(range = range(xm$Size)) +
  theme_bw()

note - because the first column names in Samples rather than Species, the axis labels were wrong, so the name of this column had to be changed.

The result looks like this: enter image description here

Upvotes: 2

Related Questions