Skip to content

Commit

Permalink
edit code in vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Aug 9, 2022
1 parent 559713b commit 140a063
Showing 1 changed file with 57 additions and 45 deletions.
102 changes: 57 additions & 45 deletions r-package/vignettes/intro_to_geobr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ You can install geobr from CRAN or the development version to use the latest fea

```{r eval=FALSE, message=FALSE, warning=FALSE}
# From CRAN
install.packages("geobr")
install.packages("geobr")
# Development version
utils::remove.packages('geobr')
devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
utils::remove.packages('geobr')
devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
```

Now let's load the libraries we'll use in this vignette.

```{r eval=TRUE, message=FALSE, warning=FALSE, results='hide'}
library(geobr)
library(ggplot2)
library(sf)
library(dplyr)
library(geobr)
library(ggplot2)
library(sf)
library(dplyr)
```


Expand All @@ -66,42 +66,52 @@ The syntax of all *geobr* functions operate one the same logic, so the code to d
Download an specific geographic area at a given year
```{r eval=TRUE, message=FALSE, warning=FALSE, results='hide'}
# State of Sergige
state <- read_state(code_state="SE",
year=2018,
showProgress = FALSE)
state <- read_state(
code_state="SE",
year=2018,
showProgress = FALSE
)
# Municipality of Sao Paulo
muni <- read_municipality(code_muni = 3550308,
year=2010,
showProgress = FALSE)
muni <- read_municipality(
code_muni = 3550308,
year=2010,
showProgress = FALSE
)
```


Download all geographic areas within a state at a given year
```{r eval=FALSE, message=FALSE, warning=FALSE, results='hide'}
# All municipalities in the state of Minas Gerais
muni <- read_municipality(code_muni= "MG",
year=2007,
showProgress = FALSE)
muni <- read_municipality(code_muni= "MG",
year=2007,
showProgress = FALSE)
# All census tracts in the state of Rio de Janeiro
cntr <- read_census_tract(code_tract = "RJ",
year = 2010,
showProgress = FALSE)
cntr <- read_census_tract(
code_tract = "RJ",
year = 2010,
showProgress = FALSE
)
```

If the parameter `code_` is not passed to the function, geobr returns the data for the whole country by default.

```{r eval=TRUE, message=FALSE, warning=FALSE, results='hide'}
# read all intermediate regions
inter <- read_intermediate_region(year=2017,
showProgress = FALSE)
inter <- read_intermediate_region(
year=2017,
showProgress = FALSE
)
# read all states
states <- read_state(year=2019,
showProgress = FALSE)
states <- read_state(
year=2019,
showProgress = FALSE
)
```

Expand All @@ -119,18 +129,18 @@ Once you've downloaded the data, it is really simple to plot maps using `ggplot2

```{r eval=TRUE, message=FALSE, warning=FALSE, fig.height = 8, fig.width = 8, fig.align = "center"}
# Remove plot axis
no_axis <- theme(axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank())
no_axis <- theme(axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank())
# Plot all Brazilian states
ggplot() +
geom_sf(data=states, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) +
labs(subtitle="States", size=8) +
theme_minimal() +
no_axis
ggplot() +
geom_sf(data=states, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) +
labs(subtitle="States", size=8) +
theme_minimal() +
no_axis
```

Expand All @@ -144,17 +154,19 @@ Plot all the municipalities of a particular state, such as Rio de Janeiro:
```{r eval=TRUE, message=FALSE, warning=FALSE, results='hide', fig.height = 8, fig.width = 8, fig.align = "center"}
# Download all municipalities of Rio
all_muni <- read_municipality( code_muni = "RJ",
year= 2010,
showProgress = FALSE)
all_muni <- read_municipality(
code_muni = "RJ",
year= 2010,
showProgress = FALSE
)
# plot
ggplot() +
geom_sf(data=all_muni, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) +
labs(subtitle="Municipalities of Rio de Janeiro, 2000", size=8) +
theme_minimal() +
no_axis
ggplot() +
geom_sf(data=all_muni, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) +
labs(subtitle="Municipalities of Rio de Janeiro, 2000", size=8) +
theme_minimal() +
no_axis
```

```{r munis rio, eval=FALSE, echo=FALSE, message=FALSE, out.width='100%'}
Expand Down Expand Up @@ -185,12 +197,12 @@ states <- dplyr::left_join(states, df, by = c("name_state" = "uf"))
#### Plot thematic map

```{r eval=TRUE, message=FALSE, warning=FALSE, fig.height = 8, fig.width = 8, fig.align = "center" }
ggplot() +
geom_sf(data=states, aes(fill=ESPVIDA2017), color= NA, size=.15) +
labs(subtitle="Life Expectancy at birth, Brazilian States, 2014", size=8) +
scale_fill_distiller(palette = "Blues", name="Life Expectancy", limits = c(65,80)) +
theme_minimal() +
no_axis
ggplot() +
geom_sf(data=states, aes(fill=ESPVIDA2017), color= NA, size=.15) +
labs(subtitle="Life Expectancy at birth, Brazilian States, 2014", size=8) +
scale_fill_distiller(palette = "Blues", name="Life Expectancy", limits = c(65,80)) +
theme_minimal() +
no_axis
```

Expand Down

0 comments on commit 140a063

Please sign in to comment.