A plot without an appropriate title does not help people to understand the story that the plot is trying to tell. Even in cases where the plot is so nice, a messy labeling or a wrongly labeled plot will always confuse the reader. A properly aligned and well labeled plot on the principles of CRISP goes a long way to present a more professionally looking plot. Most importantly, the plot will tell the story easily.
Most people have been drawing tables the entire wrong way, including me. A messy table doesn’t help either. A well structured table takes prevents a cumbersome looking presentation.
library(tidyverse)
library(plotly)
library(gapminder)
library(ggrepel)
library(ggtext)
# Load data here
Do the following:
Make a plot. Any kind of plot will do (though it might be easiest
to work with geom_point()).
Make the plot interactive with ggplotly().
Make sure the hovering tooltip is more informative than the default.
Good luck and have fun!
data("gapminder")
gapminder_2007 <- filter(gapminder, year == 2007)
gap_interractive<- gapminder_2007 %>% ggplot(mapping = aes(x = lifeExp, y = gdpPercap)) +
geom_point(aes(col= continent)) +
labs(x = "LIFE EXPECTANCY 2007", y = "GDP PER CAPITAL 2007",
title = "GDP PER CAPITA AND LIFE EXPECTANCY 2007", subtitle =
"African Countries Performed Poorly as Compared to Other Continents", caption =
" Data Source: Gapminder Dataset in R" ) +
geom_vline(xintercept = 80, linetype = "dotted") +
theme_minimal() +
annotate(geom = "rect", xmin = 80, xmax = 85, ymin = 45000, ymax = 50000,
fill = "green", alpha = 0.25)+
annotate(geom = "rect", xmin = 38, xmax = 45, ymin = 0, ymax = 5000,
fill = "red", alpha = 0.25) +
annotate(geom = "text", x = 65, y = 45000,
label = "Highest Ratings in\nboth Gdp per Capita and\n life Expectancy Rate",
hjust = 0, color = "#2ECC40") +
annotate(geom = "text", x = 45, y =20000, label = "Lowest\nPerforming Countries",
hjust = 0.5, vjust = 1, lineheight = 1, color = "#FF851B")+
annotate(geom = "segment", x = 80, xend = 76, y = 45000, yend = 45000, color = "#2ECC40",
arrow = arrow(angle = 15, length = unit(0.5, "lines")))+
annotate(geom = "segment", x = 42, xend = 42, y = 5000, yend = 15000, color = "#FF851B",
arrow = arrow(angle = 15, length = unit(0.5, "lines"))) +
theme(panel.border = element_rect(fill= NA)) + guides(fill = FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
gap_interractive
gap_interractive1<- ggplotly(gap_interractive)
gap_interractive1
gap_interractive2<- gapminder_2007 %>% ggplot(mapping = aes(x = lifeExp, y = gdpPercap)) +
geom_point(aes(col= continent, text = country)) +
labs(x = "LIFE EXPECTANCY 2007", y = "GDP PER CAPITAL 2007",
title = "GDP PER CAPITA AND LIFE EXPECTANCY 2007", subtitle =
"African Countries Performed Poorly as Compared to Other Continents", caption =
" Data Source: Gapminder Dataset in R" ) +
geom_vline(xintercept = 80, linetype = "dotted") +
theme_minimal() +
annotate(geom = "rect", xmin = 80, xmax = 85, ymin = 45000, ymax = 50000,
fill = "green", alpha = 0.25)+
annotate(geom = "rect", xmin = 38, xmax = 45, ymin = 0, ymax = 5000,
fill = "red", alpha = 0.25) +
annotate(geom = "text", x = 65, y = 45000,
label = "Highest Ratings in\nboth Gdp per Capita and\n life Expectancy Rate",
hjust = 0, color = "#2ECC40") +
annotate(geom = "text", x = 45, y =20000, label = "Lowest\nPerforming Countries",
hjust = 0.5, vjust = 1, lineheight = 1, color = "#FF851B")+
annotate(geom = "segment", x = 80, xend = 76, y = 45000, yend = 45000, color = "#2ECC40",
arrow = arrow(angle = 15, length = unit(0.5, "lines")))+
annotate(geom = "segment", x = 42, xend = 42, y = 5000, yend = 15000, color = "#FF851B",
arrow = arrow(angle = 15, length = unit(0.5, "lines"))) +
theme(panel.border = element_rect(fill= NA)) + guides(fill = FALSE)
## Warning in geom_point(aes(col = continent, text = country)): Ignoring unknown
## aesthetics: text
ggplotly(gap_interractive2, tooltip = "text")
gap_gdp<- gapminder_2007 %>% mutate(GDP = gdpPercap * pop) %>% mutate(gdp_name = "GDP") %>% mutate(LABEL1 = paste0(gdp_name, ":" ,GDP))
gdpgap2<- gap_gdp %>% mutate(LABEL2 = paste0(country," ", LABEL1))
gap_interractive3<- gdpgap2 %>% ggplot(mapping = aes(x = lifeExp, y = gdpPercap)) +
geom_point(aes(col= continent, text = LABEL2)) +
labs(x = "LIFE EXPECTANCY 2007", y = "GDP PER CAPITAL 2007",
title = "GDP PER CAPITA AND LIFE EXPECTANCY 2007", subtitle =
"African Countries Performed Poorly as Compared to Other Continents", caption =
" Data Source: Gapminder Dataset in R" ) +
geom_vline(xintercept = 80, linetype = "dotted") +
theme_minimal() +
annotate(geom = "rect", xmin = 80, xmax = 85, ymin = 45000, ymax = 50000,
fill = "green", alpha = 0.25)+
annotate(geom = "rect", xmin = 38, xmax = 45, ymin = 0, ymax = 5000,
fill = "red", alpha = 0.25) +
annotate(geom = "text", x = 65, y = 45000,
label = "Highest Ratings in\nboth Gdp per Capita and\n life Expectancy Rate",
hjust = 0, color = "#2ECC40") +
annotate(geom = "text", x = 45, y =20000, label = "Lowest\nPerforming Countries",
hjust = 0.5, vjust = 1, lineheight = 1, color = "#FF851B")+
annotate(geom = "segment", x = 80, xend = 76, y = 45000, yend = 45000, color = "#2ECC40",
arrow = arrow(angle = 15, length = unit(0.5, "lines")))+
annotate(geom = "segment", x = 42, xend = 42, y = 5000, yend = 15000, color = "#FF851B",
arrow = arrow(angle = 15, length = unit(0.5, "lines"))) +
theme(panel.border = element_rect(fill= NA)) + guides(fill = FALSE)
## Warning in geom_point(aes(col = continent, text = LABEL2)): Ignoring unknown
## aesthetics: text
ggplotly(gap_interractive3, tooltip = "text")
final_plot <- ggplotly(gap_interractive3, tooltip = "text")
htmlwidgets::saveWidget(final_plot, "fancy_plot.html")