Gapminder example 1
library(gapminder)
library(ggplot2)
library(dplyr)
gm4 <- gapminder %>%
filter(country %in% c("Canada", "United States", "Australia", "United Kingdom")) %>%
mutate( country = droplevels(country))
g1 <- ggplot(gm4, aes(x=year, y=gdpPercap, colour=country)) +
geom_line() +
theme_bw() +
labs(x="Year", y="GDP/capita", colour="Country")
g1
Gapminder example 2
g2 <- ggplot(gm4, aes(x=year, y=gdpPercap)) +
geom_line() +
facet_wrap(~country, nrow=1) +
theme_bw() +
labs(x="Year", y="GDP/capita", colour="Country")
g2
ggplotly 1
library(plotly)
gp1 <- ggplotly(g1, height=375, width=750)
gp1
plotly
library(plotly)
p <- plot_ly(gm4, x= ~year, y = ~gdpPercap, color=~country,
width=750, height=275) %>%
add_lines() %>%
layout(xaxis=list(title="Year"),
yaxis=list(title="GDP/capita"))
p
changing the hover text.
gm4 <- gm4 %>% mutate(text = paste0("Country = ", country, #<<
"\nYear = ", year, #<<
"\nGDP/capita = ", sprintf("$%.2f", gdpPercap), #<<
"\nPopulation = ", sprintf("%.2fM", pop/1000000), sep="")) #<<
p2a <- plot_ly(gm4, height=350, width=600) %>%
add_markers(x= ~year, y = ~gdpPercap, color=~country,
size=~log(pop), text=~text, hoverinfo="text") %>% #<<
layout(xaxis=list(title="Year"),
yaxis=list(title="GDP/capita"))
p2a
3D scatterplots
plot_ly(gm4, width=700, height=500, showlegend=FALSE) %>%
add_markers(x = ~log(pop), z = ~lifeExp,
y = ~gdpPercap, color=~country, showlegend=TRUE) %>%
layout(autosize=FALSE,
scene = list(
xaxis=list(title="log(Population)"),
yaxis=list(title="GDP/capita"),
zaxis=list(title="Life Expectancy")
))
maps
load("counties.rda")
library(sf)
library(stringr)
ohio <- counties %>%
filter(state %in% c("Ohio")) %>%
mutate(text = paste(NAMELSAD, "\n", cases, " cases", sep=""))
ohio_map <- plot_ly(ohio, split = ~ text, color = ~log(cases), alpha = 1,
hoverinfo="text", hoveron="fill", showlegend=FALSE)
ohio_map