# Mesmo gráfico que o anterior, mas colocando os gêneros em itálico de uma forma diferentedispersion_wgt <-ggplot(data = roedores_subset_gender, aes(x = meanwgt, y = juvwgt,color = Gênero, size =2)) +geom_point() +labs(x ="Média de peso dos adultos (g)", y ="Média de peso dos juvenis (g)") +theme_classic() +guides(size ="none", colour =guide_legend(override.aes =list(size =4))) +scale_x_continuous(expand =c(0,0), limits =c(0,180), breaks =seq(0,180, by =40)) +theme(axis.text.x =element_text(size =14),axis.title.x =element_text(size =14),axis.text.y =element_text(size =14),axis.title.y =element_text(size =14),legend.text =element_text(size=14, face="italic"),legend.title.align =0,legend.title =element_text(size =16, face ="bold") )dispersion_wgt
# A tibble: 6 x 3
Gênero age peso
<fct> <fct> <dbl>
1 Baiomys meanwgt 9.45
2 Baiomys juvwgt NA
3 Chaetodipus meanwgt 31.9
4 Chaetodipus juvwgt 19.0
5 Chaetodipus meanwgt 30.7
6 Chaetodipus juvwgt 24
ggplot(roedores_subset_gender_final, aes(y = peso, x =reorder(Gênero, -peso), fill = age)) +geom_bar(position="dodge", stat="identity", na.rm =TRUE) +labs(y ="Média de peso dos indivíduos (g)") +scale_fill_rickandmorty(name ="Grupo etário", labels =c("Juvenis", "Adultos")) +theme_classic() +theme(axis.text.x =element_text(size =16, face ="italic"),axis.title.x =element_blank(),axis.text.y =element_text(size =18),axis.title.y =element_text(size =24),legend.text =element_text(size=24),legend.title =element_text(size =24, face ="bold"),legend.position =c(0.7, 0.6))
Resolução opção 8 - Numbats
Como o avistamento de Numbats na Austrália evoluiu historicamente?
# Baixando o conjunto de dados manualmente a partir do GitHubnumbats <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-07/numbats.csv')str(numbats)
spc_tbl_ [805 x 16] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ decimalLatitude : num [1:805] -37.6 -35.1 -35 -34.7 -34.6 ...
$ decimalLongitude: num [1:805] 146 150 118 118 117 ...
$ eventDate : POSIXct[1:805], format: NA "2014-06-05 02:00:00" ...
$ scientificName : chr [1:805] "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" ...
$ taxonConceptID : chr [1:805] "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" ...
$ recordID : chr [1:805] "73830609-3d94-461f-a833-01c0a30c5a0d" "13287c0e-034d-4f05-908b-d3b60c90813d" "1041c2af-7e1f-4344-b79c-9c04aa3a55b5" "c9804b7a-de92-42db-8912-632dc37baa38" ...
$ dataResourceName: chr [1:805] "Queen Victoria Museum Art Gallery provider for OZCAM" "ALA species sightings and OzAtlas" "Western Australian Museum provider for OZCAM" "Western Australian Museum provider for OZCAM" ...
$ year : num [1:805] NA 2014 NA NA NA ...
$ month : chr [1:805] NA "Jun" NA NA ...
$ wday : chr [1:805] NA "Thu" NA NA ...
$ hour : num [1:805] NA 2 NA NA NA NA NA NA NA NA ...
$ day : Date[1:805], format: NA "2014-06-05" ...
$ dryandra : logi [1:805] FALSE FALSE FALSE FALSE FALSE FALSE ...
$ prcp : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmax : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmin : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "spec")=
.. cols(
.. decimalLatitude = col_double(),
.. decimalLongitude = col_double(),
.. eventDate = col_datetime(format = ""),
.. scientificName = col_character(),
.. taxonConceptID = col_character(),
.. recordID = col_character(),
.. dataResourceName = col_character(),
.. year = col_double(),
.. month = col_character(),
.. wday = col_character(),
.. hour = col_double(),
.. day = col_date(format = ""),
.. dryandra = col_logical(),
.. prcp = col_double(),
.. tmax = col_double(),
.. tmin = col_double()
.. )
- attr(*, "problems")=<externalptr>
# Transformando variável "year" em fatornumbats$year <-as.factor(numbats$year)str(numbats)
spc_tbl_ [805 x 16] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ decimalLatitude : num [1:805] -37.6 -35.1 -35 -34.7 -34.6 ...
$ decimalLongitude: num [1:805] 146 150 118 118 117 ...
$ eventDate : POSIXct[1:805], format: NA "2014-06-05 02:00:00" ...
$ scientificName : chr [1:805] "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" ...
$ taxonConceptID : chr [1:805] "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" ...
$ recordID : chr [1:805] "73830609-3d94-461f-a833-01c0a30c5a0d" "13287c0e-034d-4f05-908b-d3b60c90813d" "1041c2af-7e1f-4344-b79c-9c04aa3a55b5" "c9804b7a-de92-42db-8912-632dc37baa38" ...
$ dataResourceName: chr [1:805] "Queen Victoria Museum Art Gallery provider for OZCAM" "ALA species sightings and OzAtlas" "Western Australian Museum provider for OZCAM" "Western Australian Museum provider for OZCAM" ...
$ year : Factor w/ 25 levels "1856","1902",..: NA 16 NA NA NA NA NA NA NA NA ...
$ month : chr [1:805] NA "Jun" NA NA ...
$ wday : chr [1:805] NA "Thu" NA NA ...
$ hour : num [1:805] NA 2 NA NA NA NA NA NA NA NA ...
$ day : Date[1:805], format: NA "2014-06-05" ...
$ dryandra : logi [1:805] FALSE FALSE FALSE FALSE FALSE FALSE ...
$ prcp : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmax : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmin : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "spec")=
.. cols(
.. decimalLatitude = col_double(),
.. decimalLongitude = col_double(),
.. eventDate = col_datetime(format = ""),
.. scientificName = col_character(),
.. taxonConceptID = col_character(),
.. recordID = col_character(),
.. dataResourceName = col_character(),
.. year = col_double(),
.. month = col_character(),
.. wday = col_character(),
.. hour = col_double(),
.. day = col_date(format = ""),
.. dryandra = col_logical(),
.. prcp = col_double(),
.. tmax = col_double(),
.. tmin = col_double()
.. )
- attr(*, "problems")=<externalptr>
# Calculando frequência de cada ano e transformando essa informação em um data.frameyears_numbats <-as.data.frame(table(numbats$year))head(years_numbats)
# Arrumando o novo data.frameyears_numbats$Ano <- years_numbats$Var1years_numbats <-select(years_numbats, -Var1)years_numbats$Freq <-as.numeric(years_numbats$Freq)str(years_numbats)
# Gerando o gráficonumbats_plot <-ggplot(years_numbats) +geom_line(aes(x = Ano, y = Freq, group =1), colour ="black") +geom_point(aes(x = Ano, y = Freq), shape=21, color="black", fill="#69b3a2", size=4) +theme_classic() +ylim(0, 120) +labs(y ="Número de observações por ano") +annotate(geom ="text", x ="2014", y =120, label ="Em 2014 tivemos um pico na observação de Numbats na Austrália", size =5, color ="red") +annotate(geom ="point", x="2014", y =113, size =10, shape =21, fill ="transparent", color="red") +theme(axis.text.x =element_text(size =10),axis.title.x =element_blank(),axis.text.y =element_text(size =10),axis.title.y =element_text(size =14) )# Plotandonumbats_plot
Agora vamos visualizar a quantidade de observações de acordo com os dias da semana e horário
spc_tbl_ [805 x 16] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ decimalLatitude : num [1:805] -37.6 -35.1 -35 -34.7 -34.6 ...
$ decimalLongitude: num [1:805] 146 150 118 118 117 ...
$ eventDate : POSIXct[1:805], format: NA "2014-06-05 02:00:00" ...
$ scientificName : chr [1:805] "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" "Myrmecobius fasciatus" ...
$ taxonConceptID : chr [1:805] "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" "https://biodiversity.org.au/afd/taxa/6c72d199-f0f1-44d3-8197-224a2f7cff5f" ...
$ recordID : chr [1:805] "73830609-3d94-461f-a833-01c0a30c5a0d" "13287c0e-034d-4f05-908b-d3b60c90813d" "1041c2af-7e1f-4344-b79c-9c04aa3a55b5" "c9804b7a-de92-42db-8912-632dc37baa38" ...
$ dataResourceName: chr [1:805] "Queen Victoria Museum Art Gallery provider for OZCAM" "ALA species sightings and OzAtlas" "Western Australian Museum provider for OZCAM" "Western Australian Museum provider for OZCAM" ...
$ year : Factor w/ 25 levels "1856","1902",..: NA 16 NA NA NA NA NA NA NA NA ...
$ month : chr [1:805] NA "Jun" NA NA ...
$ wday : Factor w/ 7 levels "Fri","Mon","Sat",..: NA 5 NA NA NA NA NA NA NA NA ...
$ hour : num [1:805] NA 2 NA NA NA NA NA NA NA NA ...
$ day : Date[1:805], format: NA "2014-06-05" ...
$ dryandra : logi [1:805] FALSE FALSE FALSE FALSE FALSE FALSE ...
$ prcp : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmax : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
$ tmin : num [1:805] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "spec")=
.. cols(
.. decimalLatitude = col_double(),
.. decimalLongitude = col_double(),
.. eventDate = col_datetime(format = ""),
.. scientificName = col_character(),
.. taxonConceptID = col_character(),
.. recordID = col_character(),
.. dataResourceName = col_character(),
.. year = col_double(),
.. month = col_character(),
.. wday = col_character(),
.. hour = col_double(),
.. day = col_date(format = ""),
.. dryandra = col_logical(),
.. prcp = col_double(),
.. tmax = col_double(),
.. tmin = col_double()
.. )
- attr(*, "problems")=<externalptr>
# Calculando frequência de observações de acordo com dia e horário e transformando essa informação em um data.framelibrary(plyr)counts <-ddply(numbats, .(numbats$wday, numbats$hour), nrow)head(counts)
# Reordenando dias da semana de forma mais lógicalevels(counts$WeekDay)
[1] "Fri" "Mon" "Sat" "Sun" "Thu" "Tue" "Wed"
counts$WeekDay <-ordered(counts$WeekDay, levels =c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))counts <-slice(counts, 1:63) # removendo a última linha, que não nos interessava# Criando um tema mais amigáveltheme_heatmap <-theme_classic() +# Tema com fundo brancotheme(panel.grid =element_blank(), # remove as linhas de gradepanel.border =element_blank(), # remove as bordas ao redor do heatmapplot.title =element_text(face ="bold", # título em negritosize =14, # tamanho da fontehjust =0.5), # centraliza o títuloaxis.ticks =element_blank(), # remove os tickmarks dos eixosaxis.title.x =element_blank(), # retira o título do eixo Xaxis.title.y =element_text(size =12), # ajusta o tamanho do título do eixo Yaxis.text.y =element_text(size =10), # ajusta o tamanho do texto do eixo Yaxis.text.x =element_text(size =10), # ajusta o tamanho do texto do eixo Xlegend.position ="none") # retira a legenda# Carregando biblioteca com paletas de cores dos filmes do Wes Andersonlibrary(wesanderson)my_palette <-wes_palette("Moonrise3", type ="continuous")# Criando o gráficogg <-ggplot(counts, mapping =aes(x = WeekDay, y = Hour, fill = Freq, na.rm =TRUE)) +geom_tile(colour="white") +# Isso cria o heatmap ("colour" é o argumento que determina a cor das linhas)scale_fill_gradientn(colours = my_palette) +# Espectro de cores a ser utilizadoscale_y_reverse(breaks=c(24:0), labels=c(24:0), # Colocando 0 no topo do eixo Yexpand =c(0,0)) +# Removendo o preenchimento ao redor do mapa de calorscale_x_discrete(expand =c(0,0), position ="top") +labs(title ="Número de observações de numbats em relação ao horário e dia da semana", y ="Horário") + theme_heatmap # tema definido anteriormente# Plotando o gráficoprint(gg)