#Loading Data
library(gapminder)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.4 ✓ purrr 0.3.4
## ✓ tibble 3.1.2 ✓ dplyr 1.0.6
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(ggrepel)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
gapminder_2007 <- gapminder %>%
filter(year == 2007)
#Using a plot I am proud of to compare with plotly()
#adding a colorblind friendly palette
cbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
scale_x_log10("Log of Gross Domestic Product") +
scale_y_continuous("Life Expectancy") +
geom_point() +
geom_text_repel(data = subset(gapminder_2007, pop > 200000000), aes(label = country)) + #annotating a few remarkable data points
scale_colour_manual(name = "Continent", values=cbPalette) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),legend.key=element_blank()) +
labs(title = "How does Gross Domestic Product and Life Expectancy Relate?",
subtitle = "Countries grouped by continent and population size", caption = "Source: http://www.gapminder.org/data/") +
guides(size = "none") + #removing the population legend
guides(color = guide_legend(override.aes = list(size = 3) ) )
##Putting it all together in an interaction plot
interaction_plot <- gapminder_2007 %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
text = ~paste('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,'<br>Pop.:', pop),
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
interaction_plot <- interaction_plot %>% layout(title = 'How are GDP and Life Expectancy Related in 2007?', caption = 'Gapminder Data from the United Nations',
xaxis = list(title = 'Gross Domestic Product per Capita', type = "log", showgrid = FALSE),
yaxis = list(title = 'Life Expectancy', showgrid = FALSE))
interaction_plot
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: 'layout' objects don't have these attributes: 'caption'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'