Geography
1 Some socioeconomic data of Brazilian municipalities (EM13CHS402, EM13CHS302, EM13CHS304)
library(plotly)
# Fictitious data of the states of Brazil
<- data.frame(
dados_estados State = c("São Paulo", "Rio de Janeiro", "Minas Gerais", "Bahia", "Paraná"),
Acronym = c("SP", "RJ", "MG", "BA", "PR"),
Population = c(46289333, 17366189, 21411923, 14930634, 11947904),
Capital = c("São Paulo", "Rio de Janeiro", "Belo Horizonte", "Salvador", "Curitiba"),
HDI = c(0.783, 0.761, 0.731, 0.714, 0.749),
IDEB = c(4.9, 4.6, 4.7, 4.2, 4.8),
lat = c(-23.55, -22.91, -19.92, -12.97, -25.42),
lon = c(-46.63, -43.20, -43.94, -38.52, -49.27)
)
# Interactive map with information when hovering the mouse
<- plot_ly(
fig data = dados_estados,
type = 'scattergeo',
mode = 'markers',
lat = ~lat,
lon = ~lon,
text = ~paste(
"<b>State:</b>", State, "<br>",
"<b>Capital:</b>", Capital, "<br>",
"<b>Population:</b>", Population, "<br>",
"<b>HDI:</b>", HDI, "<br>",
"<b>IDEB:</b>", IDEB
),hoverinfo = 'text',
marker = list(size = 10, color = 'blue', opacity = 0.7)
%>%
) layout(
title = "Interactive Map of Brazil - Information by State",
geo = list(
scope = 'south america',
resolution = 50,
showland = TRUE,
showcountries = TRUE,
countrycolor = "gray",
showlakes = TRUE,
lakecolor = "lightblue",
showssubunits = TRUE,
subunitcolor = "white"
)
)
fig
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
# Add after line with hoverinfo
= list(
marker size = 15,
color = ~IDH,
colorscale = "Viridis", # Color scale that varies according to the IDH
colorbar = list(title = "IDH"), # Add a legend for the color scale
opacity = 0.8
2 Relationship between area and population in Brazilian municipalities (EM13MAT405, EM13MAT407, EM13CHS101, EM13CHS104, EM13CNT301)
<- "https://raw.githubusercontent.com/turicas/rows/refs/heads/develop/examples/data/brazilian-cities.csv" # define the link to the data
url <- read.csv(url) # read the data file
data
library(plotly)
plot_ly(data, x = ~area, y = ~inhabitants,
type = "scatter",
mode = 'markers',
marker = list(line = list(width = 1)),
text = ~paste("City: ", city, "<br>State: ", state), # Text on hover
hoverinfo = 'text', # Show only the defined text
frame = ~state)%>%
layout(
title = "Area and population distribution in Brazilian cities",
xaxis = list(title = "States", type = "log"),
yaxis = list(title = "Population", type = "log"),
showlegend = FALSE
%>%
) animation_opts(
frame = 500, # Animation speed
transition = 0,
redraw = FALSE
)
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
= ~state, # Added to vary the color according to the state color
3 Data on world energy matrices over the years (EM13CHS106, EM13CHS302, EM13CNT309)
library(plotly)
# Fictitious data on world energy matrices (percentages per year)
<- data.frame(
data Year = rep(2000:2020, each = 4),
Source = rep(c("Renewable", "Coal", "Oil", "Natural Gas"), times = 21),
Percentage = c(
# Fictitious data that simulates the evolution of energy sources
seq(20, 40, length.out = 21), # Renewable
seq(40, 30, length.out = 21), # Coal
seq(30, 20, length.out = 21), # Oil
seq(10, 10, length.out = 21) # Natural Gas
)
)
# Interactive graph
<- plot_ly(
fig data = data,
x = ~Year,
y = ~Percentage,
color = ~Source,
type = 'bar',
colors = c("green", "black", "brown", "blue")
%>%
) layout(
title = "Evolution of World Energy Matrices",
barmode = "stack",
xaxis = list(title = "Year"),
yaxis = list(title = "Percentage (%)"),
legend = list(title = list(text = "Energy Source"))
)
fig
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
= 0.8 # Changed the opacity of the bars opacity