add dot to each line based on a separate column using ggplot2 + linerange()

I have a linerange graph that shows sightings of birds and their diet over time.

Using the below example I would like to add a further layer, using a column entitled time_period, which has grouped the data into 3 levels: <10 years, 10-20 years, >20 years. However, I’ve come stuck. I would like to:

  1. Add a little coloured dot next to each line based on the time_period column
  2. Add a legend underneath that shows what each dot means

The dataset is taken from this csv file here, and looks like this:

head(bird_models_traits) # A tibble: 6 x 17   decimal.latitude decimal.longitu~ class species.name id    duration minyear maxyear system              <dbl>            <dbl> <chr> <chr>        <fct>    <dbl>   <dbl>   <dbl> <chr>  1            -54.6             159. Aves  Aptenodytes~ 2283        10    1970    1980 Marine 2            -43.0             147. Aves  Larus domin~ 8990        28    1980    2008 Marine 3            -43.0             147. Aves  Larus novae~ 8992        25    1983    2008 Marine 4            -43.0             147. Aves  Larus pacif~ 8991        28    1980    2008 Marine 5            -42.9             147. Aves  Calidris fe~ 8940        33    1974    2007 Fresh~ 6            -42.9             147. Aves  Calidris ru~ 8942        34    1974    2008 Fresh~ # ... with 8 more variables: common.name <chr>, estimate <dbl>, std.error <dbl>, #   statistic <dbl>, p.value <dbl>, diet <fct>, mean_trend <dbl>, sort <dbl> 

I have created the following graph: timeline graph

Using this code:

library(tidyverse) library(wesanderson)     ggplot() +         geom_linerange(data = bird_models_traits, aes(ymin = minyear, ymax = maxyear,                                                        colour = diet,                                                       x = fct_reorder(id, desc(sort))),                        size = 1) +         scale_colour_manual(values = wes_palette("Cavalcanti1")) +         labs(x = NULL, y = NULL) +         theme_bw() +         coord_flip() +         guides(colour = F) +         theme(panel.grid.minor = element_blank(),               panel.grid.major.y = element_blank(),               panel.grid.major.x = element_line(),               axis.ticks = element_blank(),               legend.position = "bottom",                panel.border = element_blank(),               legend.title = element_blank(),               axis.title.y = element_blank(),               axis.text.y = element_blank(),               axis.ticks.y = element_blank(),               plot.title = element_text(size = 20, vjust = 1, hjust = 0),               axis.text = element_text(size = 16),                axis.title = element_text(size = 20)) 

How to add labels in a contour plot using ggplot2?

I’m doing a contour plot with ggplot2 and I’m trying to add the labels like this tutorial. However, always appears the following error:

Error in geom_text_contour(aes(z = z)) : could not find function "geom_text_contour"

library(ggplot2)  x <- c(0,0,0,1,1,1,2,2,2,3,3,3) y <- c(0,1,2,0,1,2,0,1,2,0,1,2) z <- seq(0,11,1)  df <- data.frame(x,y,z)  ggplot(df, aes(x, y, z = z)) +   geom_tile(aes(fill = z)) +   geom_contour(colour = "white") +   geom_text_contour(aes(z = z)) 

enter image description here

Como posicionar o título no ggplot2 com o tema theme_ipsum?

Tenho os seguintes dados dados

B Freq 1 Feminino 61.54 2 Masculino 38.46

Com o código abaixo gero um gráfico de pizza no `ggplot.

library(ggplot2) library(hrbrthemes)

B Feminino Masculino 8 5

contagem = table(B) nomes = levels(B) porcent =round(contagem/sum(contagem)*100,2) rotulo=paste(porcent,"%",sep="") dados = data.frame(round(contagem/sum(contagem)*100,2)) dados <- within(dados, { B <- factor(B, labels=nomes) }) dados <- dados[order(Freq),] attach(dados)

(graf_B <- ggplot(dados, aes(x ="", y=Freq, fill=B)) + geom_bar(width = 1, stat = "identity") + coord_polar("y", start = 0, direction =1) + theme( axis.title.x = element_blank(), axis.title.y = element_blank(), panel.border = element_blank(), panel.grid=element_blank(), axis.ticks = element_blank(), panel.background = element_blank(), axis.text.x=element_blank(), legend.title = element_blank(), plot.title=element_text(size=14, face="bold")) + geom_text(data = dados, aes(x ="", y=Freq, label = rotulo), position = position_stack(vjust = 0.5)) + labs(title = "Gênero", subtitle = "", x="", y="", fill="")+ theme_ipsum(plot_title_size = 12, axis_title_size = 10)) inserir a descrição da imagem aqui Eu gostei muito desse tema, mas gostaria de saber se existe a possibilidade de centralizar o título. Eu já tentei e não conseguir. Obrigado.

How to efficiently drop missing data from geom_ribbon? Is there a simpler approach from ggplot2?

I am trying to make geom_ribbon leave a gap for missing values. I am trying to implement the solutions discussed here and here, but I fail to see how to get the ribbon removed from the areas of the plot were values are missing.

#preparing sample data

mtmodel <- lm(mpg ~ wt, data = mtcars) mtcars$  Low <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,2] mtcars$  High <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,3] mtcars$  Mean <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,1] new_mtcars<-gather(mtcars, "Variable", "value", Low:Mean) 

#creating groups

new_new_mtcars <- new_mtcars %>%   mutate(grouping = case_when(     between(wt, min(wt), mean(wt) + 0.09) ~ "group1",     wt >= max(wt) - 0.5  ~ "group2"   )) 

#drawing the plot

ggplot(new_new_mtcars,aes(x=wt,y=value,linetype=as.factor(Variable))) +      geom_path(size = 0.71) +          geom_ribbon(aes(fill = new_new_mtcars$  grouping, ymin = min(wt), ymax = max(wt), alpha = .25,  na.rm=TRUE))  

#here is my current output:

enter image description here

Thanks in advance for any help