library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.1.2 ✓ dplyr 1.0.6
## ✓ tidyr 1.1.3 ✓ 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(rio)
library(here)
## here() starts at /work/tutorial
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:rio':
##
## export
## 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
## toupper sqrt plot are base functions (always loaded in R)
## most functions are not preloaded. They comes from libraries (packages)
raw_data <- import(here::here("data/food_meat_data.xlsx"))
## New names:
## * `` -> ...16
tibble_data <- as_tibble(raw_data)
smaller_data <- select(tibble_data, country, beef_cons)
## pipe operator
## the pipe operator will take the output of the last function and pass it to the next function
## makes it easier to write a chain of functions
## AND THEN
## control + shift + M
my_plot <-
import(here::here("data/food_meat_data.xlsx")) %>%
as_tibble() %>%
select(country, beef_cons) %>%
mutate(beef_cons = as.numeric(beef_cons)) %>%
arrange(-beef_cons) %>%
## factor column is an arranged character column
mutate(country = fct_reorder(country, beef_cons)) %>%
head(30) %>%
ggplot() +
geom_col(aes(x = beef_cons, y = country), fill = "dodgerblue2") +
geom_label(aes(x = beef_cons + 3, y = country, label = beef_cons), size =2) +
labs(title = "Top 30 countries for beef consumption",
subtitle = "Kg of beef consumed per person per year",
caption = "Data source: FAO and nu3",
y = "",
x = "kg/person/year") +
ggthemes::theme_wsj() +
theme(plot.title = element_text(size = 14),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 7))
## New names:
## * `` -> ...16
ggplotly(my_plot)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomLabel() has yet to be implemented in plotly.
## If you'd like to see this geom implemented,
## Please open an issue with your example code at
## https://github.com/ropensci/plotly/issues