::p_load(ggiraph, plotly, gganimate, DT, patchwork, gganimate, gifski, gapminder, readxl, tidyverse) pacman
In-class Exercise
Installing and loading R packages
Two packages will be installed and loaded. They are: tidyverse and ggiraph
importing data
<- read_csv("data/Exam_data.csv") exam_data
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot")
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = CLASS,
data_id = CLASS),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") + scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618, options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
) )
$onclick <- sprintf("window.open(\"%s%s\")",
exam_data"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(onclick = onclick),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618)
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
text = ~paste("Student ID:", ID,
"<br>Class:", CLASS),
color = ~RACE,
colors = "Set1") %>%
layout(title = 'English Score versus Maths Score ',
xaxis = list(range = c(0, 100)),
yaxis = list(range = c(0, 100)))
<- highlight_key(exam_data)
d <- ggplot(data=d,
p1 aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
<- ggplot(data=d,
p2 aes(x = MATHS,
y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))
::datatable(exam_data, class= "compact") DT
<- highlight_key(exam_data)
d <- ggplot(d,
p aes(ENGLISH,
+
MATHS)) geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
<- highlight(ggplotly(p),
gg "plotly_selected")
::bscols(gg,
crosstalk::datatable(d),
DTwidths = 5)
<- c("Country", "Continent")
col <- read_xls("data/GlobalPopulation.xls",
globalPop sheet="Data") %>%
mutate_each_(funs(factor(.)), col) %>%
mutate(Year = as.integer(Year))
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) + #<<
ease_aes('linear') #<<