xlab("Year")
# df1 <- read_delim("bib.bib", delim = "=")
# df1 <- df1[,1] %>%
#     na.omit()
# df2 <- read_delim("bib.bib", delim = ",")
# df2 <- df2[,1] %>%
#     na.omit()
title <- which(df == "title")
title <- df[title+1]
title <- str_replace_all(title, "\\{", "")
title <- str_replace_all(title, "\\}", "")
tokens <- data.frame(title = title) %>% unnest_tokens(word, title)
tidy_tokens <- tokens %>%
anti_join(stop_words)
tidy_tokens %>%
count(word, sort = TRUE) %>%
filter(n > 18) %>%
ggplot(aes(x = reorder(word,n), y = n)) + geom_col() + coord_flip() +
theme(axis.title.x=element_blank()) +
xlab("Keywords")
ggsave("keywords.pdf", width = 8, height= 5)
library(babynames)
data("babynames")
babynames$name
author <- which(df == "author")
author <- df[author+1]
tokens <- data.frame(author = author) %>% unnest_tokens(word, author)
tidy_tokens <- tokens %>%
anti_join(stop_words) %>%
anti_join(data.frame(word = tolower(babynames$name)))
tidy_tokens %>%
count(word, sort = TRUE) %>%
filter(n > 4) %>%
ggplot(aes(x = reorder(word,n), y = n)) + geom_col() + coord_flip() +
theme(axis.title.x=element_blank()) +
xlab("Authors")
plot_grid(g1,g2, nrow = 1, rel_widths = c(2, 1))
ggsave("plot.pdf", width = 7, height = 4)
sum(substr(df_book$type,1,4)=="arti")
sum(substr(df_book$type,1,4)=="book")
matrix(1:4,nrow=2) ==3
if(!require(tmap)){install.packages("tmap")}
if(!require(sf)){install.packages("sf")}
if(!require(maps)){install.packages("maps")}
library(tidyverse)
library(readxl)
library(tmap)        # Geographical plotting
library(gapminder)   # Country level data
library(maps)        # Geographical datasets
library(sf)          # Shape files (sf) are a common geographical format
library(plotly)      # Dynamic graphs
data("World")
str(World)           # structure of World
data("gapminder")
head(gapminder)
names(World)[2] <- "country"                    # Change the column name
gapminder %>% left_join(World, by = "country")  # Join the two datasets
nrow(gapminder)                                 # Number of instances
data <- gapminder %>%                     # Join and save via the arrow operator <-
left_join(World, by = "country") %>%
na.omit()                               # Remove rows with missing data
nrow(data)                                # Number of instances in dataset
data %>%
filter(year == 2007) %>%    # Keep only one year (problems otherwise)
sf::st_sf() %>%             # Transform data into shape file (sf) format
tm_shape() +                # Tranforms the sf into a tmap object
tm_polygons("lifeExp")      # Plot!
load("gap2.RData")
head(gap2)
gap2 <- gap2 %>% spread(key = indicator, value = value)
data <- gap2 %>%
left_join(World, by = "country") #%>%
#na.omit()
if(!require(leaflet)){install.packages("leaflet")}                  # Install package
library(leaflet)                                                    # Load package
datamap <- data %>% filter(year == 2011)                            # Keep only one year (recent)
palet <- colorBin("YlGnBu",                                         # Yellow-Orange-Red palette
domain = datamap %>% pull(lifeExp), # LifeExp     # Domain of labels: lifeExp
bins = c(50, 55, 60, 65, 70, 80, 90))             # Age categories
labels <- sprintf(                                                  # Below we define the labels
"<strong>%s</strong><br/>%g Years",                               # Adding text to label
datamap$country,                                                  # We show the country name...
datamap$lifeExp                                                   # ... and the life expectancy
) %>% lapply(htmltools::HTML)                                       # Embedded all into html languge
data %>%
filter(year == 2011) %>%                        # Keep the one year of data
data.frame() %>%                                # Turn into dataframe (technical)
sf::st_sf() %>%                                 # Format in sf
st_transform("+init=epsg:4326") %>%             # Convert in particular coordinate reference
leaflet() %>%                                   # Call leaflet
addPolygons(fillColor = ~palet(lifeExp),        # Create the map (colored polygons)
weight = 2,                         # Width of separation line
opacity = 1,                        # Opacity of separation line
color = "white",                    # Color of separation line
dashArray = "3",                    # Dash size of separation line
fillOpacity = 0.7,                  # Opacity of polygon colors
highlight = highlightOptions(       # 5 lines below control the cursor impact
weight = 2,                       # Width of line
color = "#CBCBCB",                # Color of line
dashArray = "",                   # No dash
fillOpacity = 0.7,                # Opacity
bringToFront = TRUE),
label = labels,                     # LABEL! Defined above!
labelOptions = labelOptions(        # Label options below...
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
) %>%
addLegend(pal = palet,                    # Legend: comes from palet colors defined above
values = ~lifeExp,              # Values come from lifeExp variable
opacity = 0.7,                  # Opacity of legend
title = "Map Legend",           # Title of legend
position = "bottomright")       # Position of legend
if(!require(readxl)){install.packages("readxl")}    # Install package to read excel files
library(readxl)                                     # Activate package
options(scipen=999)                                 # Remove scientific notation
country_LL <- read_excel("country_LL.xlsx")         # Read longitude/latitude data
data2 <- left_join(gap2, country_LL) %>%            # Merge two datasets
na.omit() %>%                                     # Remove missing points
mutate(latitude = as.numeric(latitude),           # 'Numerize' latitude
longitude = as.numeric(longitude),         # 'Numerize' longitude
population = round(population / 10^6,1))   # Scaling population into millions
data2 %>% head()
if(!require(scales)){install.packages("scales")}
if(!require(viridis)){install.packages("viridis")} # Package for color gradient
library(viridis)
library(scales)
world <- map_data("world")
g <- ggplot(data = world) +                               # Data source
geom_polygon(data = world, aes(x = long,                # Dark background of the world
y = lat,
group = group)) +
geom_point(data = data2 %>% filter(year == 2018),       # Most recent data points
aes(x = longitude,                           # Classical ggplot syntax!
y = latitude,
size = population,
color = lifeExp,
label = country)) +                      # Label (for plotly)
scale_color_viridis()                                   # Color scale
ggplotly(g)                                               # Plotly integration
cities <- read_excel("worldcities.xlsx")
cities_short <- cities %>%
filter(country %in% c("Germany", "Italy", "Switzerland", "Austria", "Slovenia"),
population > 250000) %>%
na.omit()
head(cities_short)
cities_short <- cities %>%
filter(country %in% c("Germany", "Italy", "Switzerland", "Austria", "Slovenia"),
population > 250000) %>%
na.omit()
head(cities_short)
world <- map_data("world")
g <- ggplot(data = world) +                                     # Data source
geom_polygon(data = world,                                    # Dark background of the world
aes(x = long, y = lat, group = group),
size = 0.5, color = "grey") +                    # Outer lines of polygons
geom_point(data = cities_short,                               # The points
aes(x = lng,
y = lat,
size = population,
color = country,
label = city)) +
#scale_colour_gradient(low = "#FFFB00", high = "#FF1B00") +  # This line is for color = population
scale_colour_manual(values = c("#FFEC00", "#66E234", "#1BCBF7", "#CC44F8", "#FC2361")) +
coord_sf(xlim = c(5, 18), ylim = c(36, 57), expand = FALSE)             # Zoom on Europe
ggplotly(g)
regions <- sf::st_read("regions-20140306-100m.shp")
str(regions)    # Structure of the variable region
regions %>%
tm_shape() +
tm_polygons("surf_km2")
regions %>%
filter(! (nom %in% c("Guyane", "Mayotte", "Martinique", "Guadeloupe", "La Réunion"))) %>%
tm_shape() +
tm_polygons("surf_km2", border.col = "white") # With the border of polygons
regions %>%
filter(! (nom %in% c("Guyane", "Mayotte", "Martinique", "Guadeloupe", "La Réunion"))) %>%
tm_shape() +
tm_fill("nb_dep", palette = "Spectral")  # Without the border
if(!require(highcharter)){install.packages("highcharter")}
library(highcharter)
if(!require(highcharter)){install.packages("highcharter")}
library(highcharter)
mapdata <- get_data_from_map(download_map_data("countries/fr/custom/fr-all-mainland"))
head(mapdata)
mapdata <- mapdata %>% mutate(value = 1:nrow(mapdata)) # Completely random values
head(mapdata,20)
hcmap("countries/fr/custom/fr-all-mainland",                      # Source for the map
data = mapdata,                                             # Source for the color code
value = "value",                                            # Column for color code
joinBy = c("name"),                                         # Common name & label
name = "Random Plot",                                       # Name of plot
dataLabels = list(enabled = TRUE, format = "{point.name}"), # Allow labels
borderColor = "#FAFAFA", borderWidth = 0.5,                 # Border info
tooltip = list(valueDecimals = 1,                           # Label info
valuePrefix = "Value = ",                    # Before label
valueSuffix = " Units"))                     # After label
tripadvisor <- read_excel("tripadvisor_museum_US.xlsx") %>%    # Importing the data
mutate(Longitude = as.numeric(Longitude),                    # Getting numbers back
Latitude = as.numeric(Latitude),
Rating = as.numeric(Rating),
LengthOfVisit = as.factor(LengthOfVisit)) %>%
filter(Latitude > 40, Latitude < 41, Longitude > (-74.5), Longitude < (-73.5)) # NY museums only
tripadvisor
leaflet() %>%                                                  # An empty map: a good start!
setView(lng = 4.85, lat = 45.75, zoom = 12) %>%
addTiles()
pal <- colorFactor(palette = "Spectral", domain = tripadvisor$LengthOfVisit)
tripadvisor %>%
leaflet() %>%                                                  # A blank sheet...
setView(lng = -73.9772, lat = 40.7808, zoom = 10) %>%
addTiles() %>%
addCircles(lng = ~Longitude, lat = ~Latitude,
radius = ~Rating^4, label =  ~MuseumName,
fillColor = ~pal(LengthOfVisit), fillOpacity = 0.9,  # Circle colors
stroke = TRUE, color = "black", weight = 2) %>%      # Circle stroke
addLegend(pal = pal,                    # Legend: comes from palet colors defined above
values = ~LengthOfVisit,              # Values come from lifeExp variable
opacity = 0.7,                  # Opacity of legend
title = "Map Legend",           # Title of legend
position = "bottomright")       # Position of legend
mapdata2 <- get_data_from_map(download_map_data("countries/us/custom/us-ny-congress-113"))
hcmap("countries/us/custom/us-ny-congress-113") %>%
hc_mapNavigation(enabled = TRUE,
buttonOptions = list(
align = "right",
verticalAlign = "bottom")) %>%
hc_add_series(data = tripadvisor %>% mutate(lon = Longitude, lat = Latitude, name = MuseumName),  # Add correct names
type = "mappoint", name = "Museums")
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
library(leaflet)
library(tmap)
data(World)
load("olympics.RData")
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
library(leaflet)
library(tmap)
data(World)
load("olympics.RData")
setwd("~/Documents/IT/COURS/2021/R4DS/S7_extensions/Geocomputing")
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
library(leaflet)
library(tmap)
data(World)
load("olympics.RData")
setwd("~/Documents/IT/COURS/2021/R4DS/S7_extensions/Geocomputing/steps")
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
library(leaflet)
library(tmap)
data(World)
load("olympics.RData")
data(World)
names(World)[2] <- "Country"  # Change the column name; indeed, it's Country in olympics.RData
load("olympics.RData")
olympics <- olympics %>%
left_join(World %>% select(Country, geometry))
sum(is.na(olympics$geometry))
olympics %>% group_by(Country, geometry) %>%
count()
olympics %>% group_by(Country) %>%
count() %>%
left_join(World %>% select(Country, geometry))
data(World)                   # The first column is the 3 character name, just like in the olympics dataset
names(World)[1] <- "Country"  # Change the column name; indeed, it's Country in olympics.RData
load("olympics.RData")
olympics %>%
group_by(Country) %>%
count() %>%
left_join(World %>% select(Country, geometry))
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
olympics%>% filter(Country %in% filter_country()) %>%
group_by(Country) %>%
count() %>%
left_join(World %>% select(Country, geometry))
olympics%>%
group_by(Country) %>%
count() %>%
left_join(World %>% select(Country, geometry))
data(World)                   # The first column is the 3 character name, just like in the olympics dataset
names(World)[1] <- "Country"
olympics%>%
group_by(Country) %>%
count() %>%
left_join(World %>% select(Country, geometry))
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
runApp('v_leaflet.R')
library(highcharter)
mapdata <- get_data_from_map(download_map_data("countries/fr/custom/world"))
mapdata <- get_data_from_map(download_map_data("custom/world"))
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_leaflet.R')
runApp('v_highchart.R')
hc_data <- get_data_from_map(download_map_data("custom/world")) # Column 11 is the one !!!
colnames(hc_data)[11] <- "Country"
data_map <- olympics %>%
group_by(Country) %>%
count()
data_map <- hc_data %>%
left_join(data_map %>% select(Country, n))
data_map
data_map %>% filter(Country, n)
data_map %>% select(Country, n)
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
runApp('v_highchart.R')
hc_data <- get_data_from_map(download_map_data("custom/world")) # Column 11 is the one !!!
colnames(hc_data)[11] <- "Country"
load("olympics.RData")
data_map <- olympics %>%
group_by(Country) %>%
summarize(value = n())                                          # Counts nb instances, like n()
data_map <- hc_data %>%
left_join(data_map %>% select(Country, value)) %>%                    # Joining the n (counting) column
na.omit()
hcmap("custom/world",                                             # Source for the map
data = data_map,                                            # Source for the color code
value = "value",                                                # Column for color code
joinBy = c("Country"),                                      # Common name & label
name = "Medals per country",                                # Name of plot
dataLabels = list(enabled = TRUE, format = "{point.name}"), # Allow labels
borderColor = "#FAFAFA", borderWidth = 0.5,                 # Border info
tooltip = list(valueDecimals = 0,                           # Label info
valuePrefix = "Value = ",                    # Before label
valueSuffix = " Medals"))
data_map <- olympics %>%
group_by(Country) %>%
summarize(value = n())                                          # Counts nb instances, like n()
data_map <- hc_data %>%
left_join(data_map %>% select(Country, value)) %>%                    # Joining the n (counting) column
na.omit() %>%
data.frame()
hcmap("custom/world",                                             # Source for the map
data = data_map,                                            # Source for the color code
value = "value",                                                # Column for color code
joinBy = c("Country"),                                      # Common name & label
name = "Medals per country",                                # Name of plot
dataLabels = list(enabled = TRUE, format = "{point.name}"), # Allow labels
borderColor = "#FAFAFA", borderWidth = 0.5,                 # Border info
tooltip = list(valueDecimals = 0,                           # Label info
valuePrefix = "Value = ",                    # Before label
valueSuffix = " Medals"))
hcmap("custom/world",                                             # Source for the map
data = data_map,                                            # Source for the color code
value = "value",                                                # Column for color code
joinBy = c("name"),                                      # Common name & label
name = "Medals per country",                                # Name of plot
dataLabels = list(enabled = TRUE, format = "{point.name}"), # Allow labels
borderColor = "#FAFAFA", borderWidth = 0.5,                 # Border info
tooltip = list(valueDecimals = 0,                           # Label info
valuePrefix = "Value = ",                    # Before label
valueSuffix = " Medals"))
runApp('v_highchart.R')
runApp('~/Documents/IT/COURS/2021/R4DS/S7_extensions/APIs/v_text.R')
runApp('~/Documents/IT/COURS/2021/R4DS/S7_extensions/APIs/v_text.R')
if(!require(rtweet)){install.packages("rtweet")}
library(tidyverse)
library(plotly)
library(rtweet)
consumer_key <- "xqMm10Vwwl1XAx31CAzYNiqoi"
consumer_secret <- "DRLcKjqPLbioz2i2VEWnWrAQjYRUFCL4sUnNazCxZuQ8BtTqcd"
access_token <- "3261052891-OAqPjEkUQrgkMOkUyEvyWRDVxa76JFa9e52NdNT"
access_secret <- "4f3skjfnwLavOZGNhtuetIlE4gsx8CGXEi2GMwaRfF7n0"
create_token(app = "Big Doudou",
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_secret = access_secret
)
search_term <- "covid-19"
tweets <- search_tweets(
search_term,          # What to search for
n = 20000,             # Number of tweets to download
include_rts = FALSE   # Exclude re-tweets
)
if(!require(tidytext)){install.packages("tidytext", repos = "https://cloud.r-project.org/")}
library(tidytext)
tokens <- tweets %>%
mutate(id = 1:nrow(tweets)) %>%  # This creates a tweet id
select(id, text) %>%             # Keeps only id and text of the tweet
unnest_tokens(word, text)        # Creates tokens!
tokens
tokens %>%
count(word, sort = TRUE)
tokens %>% mutate(length = nchar(word))
tokens %>%
mutate(length = nchar(word)) %>%
filter(length > 4) %>%             # Keep words with length larger than 4
count(word, sort = TRUE) %>%       # Count words
filter(n > 100 ) %>%               # Keep only words that appear more than 100 times
ggplot(aes(x = reorder(word,n), y = n)) + geom_col() + coord_flip()
data("stop_words")
tidy_tokens <- tokens %>%
anti_join(stop_words)                # Remove unrelevant terms
tidy_tokens %>%
count(word, sort = TRUE) %>%         # Count words
filter(n > 250) %>%                  # Keep only words that appear more than 6 times
ggplot(aes(x = reorder(word,n), y = n)) + geom_col() + coord_flip()
tidy_tokens <- tokens %>%
anti_join(stop_words) %>%                            # Remove unrelevant
mutate(word = iconv(word, from = "UTF-8", to = "ASCII")) %>% # Put in latin format
na.omit() %>%                                        # Remove missing
filter(nchar(word) > 3,                              # Remove small words
!(word %in% c("https", "t.co", search_term))  # search_term defined above
)
tidy_tokens %>%
count(word, sort = TRUE) %>%         # Count words
filter(n > 120) %>%                  # Keep only words that appear more than XX times
ggplot(aes(x = reorder(word,n), y = n)) + geom_col() + coord_flip()
if(!require(wordcloud)){install.packages("wordcloud")}
library(wordcloud)
cloud_data <- tidy_tokens %>% count(word)
wordcloud(words = cloud_data$word,
freq = cloud_data$n, min.freq = 2,
max.words = 100, random.order = FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
if(!require(textdata)){install.packages("textdata", repos = "https://cloud.r-project.org/")}
library(tidytext)
library(textdata)
afinn <- get_sentiments("afinn")
afinn
tokens_time <- tweets %>%
mutate(id = 1:nrow(tweets)) %>%    # This creates a tweet id
select(id, text, created_at) %>%   # Keeps id, text and date of the tweet
unnest_tokens(word, text)          # Creates tokens!
tokens_time
library(lubridate)
sentiment <- tokens_time %>%
inner_join(afinn) %>%
mutate(day = day(created_at),
hour = hour(created_at) / 24,
minute = minute(created_at) / 60 / 24,
time = day + hour + minute)
sentiment
sentiment %>%
group_by(time) %>%
summarise(avg_sentiment = mean(value)) %>%
ggplot(aes(x = time, y = avg_sentiment)) + geom_col()
nrc <- get_sentiments("nrc")
nrc
emotions <- tokens_time %>%
inner_join(nrc) %>%             # Merge data with sentiment
mutate(day = day(created_at),
hour = hour(created_at)/24,
minute = minute(created_at)/24/60,
time = day+hour+minute)   # Create day column
emotions                          # Show the result
g <- emotions %>%
group_by(time, sentiment) %>%
summarise(intensity = n()) %>%
ggplot(aes(x = as.factor(time), y = intensity, fill = sentiment)) + geom_col()
ggplotly(g)
g <- emotions %>%
group_by(time, sentiment) %>%
summarise(intensity = n()) %>%
ggplot(aes(x = as.factor(time), y = intensity, fill = sentiment)) + geom_col(position = "fill")
ggplotly(g)
if(!require(sentimentr)){install.packages(c("sentimentr", "textcat"))}
library(sentimentr)
library(textcat)
tweets_en <- tweets %>%
mutate(language = textcat(text)) %>%
filter(language == "english") %>%
dplyr::select(created_at, text)
tweet_sent <- tweets_en$text %>%
get_sentences() %>%  # Intermediate function
sentiment()          # Sentiment!
search_term <- "covid-19"
tweets <- search_tweets(
search_term,          # What to search for
n = 10000,             # Number of tweets to download
include_rts = FALSE   # Exclude re-tweets
)
if(!require(tidytext)){install.packages("tidytext", repos = "https://cloud.r-project.org/")}
library(tidytext)
tokens <- tweets %>%
mutate(id = 1:nrow(tweets)) %>%  # This creates a tweet id
select(id, text) %>%             # Keeps only id and text of the tweet
unnest_tokens(word, text)        # Creates tokens!
search_term <- "covid-19"
tweets <- search_tweets(
search_term,          # What to search for
n = 10000,             # Number of tweets to download
include_rts = FALSE   # Exclude re-tweets
)
