I started by sampling 1,000 random rows from the diamonds dataset. I did not use the entire dataset because it is difficult to hover over the points when the dense observations overlap with each other. The graph compares the relationship between the diamonds carat and its price, grouped by the cut. Hovering over a point will display information about the diamond’s carat, price, and cut. I made the points more transparent to show the density of the observations.
library(plotly)
library(dplyr)
library(ggplot2)
set.seed(2)
data("diamonds")
diamonds = diamonds %>%
sample_n(1000)
plot1 = plot_ly(diamonds, x = ~carat, y = ~price, color = ~cut,
text = ~paste("Carat: ", carat, "<br>",
"Price: $", price, "<br>",
"Cut: ", cut),
marker = list(size = 5, opacity = 0.5)) %>%
layout(title = "Relationship Between Carat and Price of Diamonds",
titlefont = list(size = 15),
xaxis = list(title = "Carat"),
yaxis = list(title = "Price"),
hovermode = "closest",
annotations = list(
text = "Carat vs Price Across Cuts with 1000 Random Samples",
x = 0.58,
y = 1,
xref = "paper",
yref = "paper",
showarrow = FALSE
))
plot1
## Warning: The titlefont attribute is deprecated. Use title = list(font = ...)
## instead.
I think when it comes to making static plots for something that will be published on papers (such as reports or journals), it is best to use ggplot2, since there is no interaction element. It is also useful to use ggplot2 for simple EDA for the purpose of understanding the data, and when the goal is not interaction.
However, plotly is the better choice when we want to create visualizations that will be published online or on the web (such as dashboards and applications). This will allow users to interact with our graphs and explore the data themselves. The interaction feature is also helpful in exploring patterns or relationships of certain variables over time, so we do not have to make multiple different graphs in ggplot2 to account for the time change. Since it also allows us to animate the graph and hover over data points, I think plotly is especially useful in visualizing complex dataset, because these features simplify the visual.
This graph compares the GDP per capita and life expectancy of different countries, with the focus being to visualize how the two variables have changed over time. GDP is used to measure a country’s economy and its growth rate. As an indicator of the population’s well-being, life expectancy can also represents a country’s standard of living. By comparing these variables over time and across different countries, we can observe the relationship between a country’s economic prosperity/growth (measured by GDP per capita) and the living standard of its population (measured by life expectancy).
The graph shows that over time, the GDP per capita and life expectancy for most (but not all) countries have increased. The two variables are also positively correlated, meaning longer life expectancy is associated with trends in economic growth over the years. This graph shows how economic growth has contributed to the well-being of a population, as indicated by how countries with higher GDP per capita generally have higher life expectancy as well. However, the relationship between the two variables can vary significantly across countries. Some nations may enjoy rapid economic growth over the years, but struggle to improve the life expectancy of their populations. Hovering over the points will display information about the country’s name, GDP, and life expectancy, which makes identifying specific countries and outliers easier.
library(gapminder)
library(plotly)
data = gapminder[, c("country", "year", "gdpPercap", "lifeExp")]
plot2 = plot_ly(data, x = ~gdpPercap, y = ~lifeExp, color = ~country, frame = ~year,
type = 'scatter', mode = 'markers',
marker = list(size = 10)) %>%
layout(title = "GDP per Capita vs. Life Expectancy of Countries",
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Life Expectancy"),
showlegend = FALSE)
plot2
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors