Search code examples
rvisualizationdata-analysisbubble-chart

Programming in R (Bubble Chart visualization)


I have a dataset for passenger travel frequency:

CountryOrigin - has 40 + country names
(INDIA,AUSTRALIA,CHINA,JAPAN,BATAM,BALI,SINGAPORE)
CountryDestination - has 40+ country names (INDIA,AUSTRALIA,CHINA,JAPAN,BATAM,BALI,SINGAPORE)

       IND   AUS   CHI   JAP   BAT   SING
 IND     0     4    10    12    24     89
 AUS    19     0    12     9     7     20
 CHI    34    56     0     2     6     18
 JAP    12    17    56     0     2      2
 SING   56    34     7     3    35      0

I need the Origin location names in x-axis and destination names in y-axis, the frequency should be represented as size for the bubble.


Solution

  • I would use ggplot2 for these (or any kind) of plots. Let's first create some test data:

    countries = c('IND', 'AUS', 'CHI', 'JAP', 'BAT', 'SING')
    frequencies = matrix(sample(1:100, 36), 6, 6, dimnames = list(countries, countries))
    diag(frequencies) = 0
    

    And make the plot. First we have to cast the matrix data to a suitable format:

    library(reshape2)
    frequencies_df = melt(frequencies)
    names(frequencies_df) = c('origin', 'destination', 'frequency')
    

    And use ggplot2:

    library(ggplot2)
    ggplot(frequencies_df, aes(x = origin, y = destination, size = frequencies)) + geom_point()
    

    enter image description here