With inserting texts into a scatter chart (with ggplot)?

Asked

Viewed 82 times

0

Hello, I have a scatter chart in which I combined 6 variables in the same graphic, now I’m trying to identify each variable in the graph with geom_text, but I can put the text referring to only one of them (Skull). Can anyone help me? how do I name the other straights?

grafico_alometria_turF <- 
  ggplot(
         data = melted_dfF, 
         aes(
             x = alom_F_tur.CRC, 
             y = trait.value, 
             colour=as.factor(trait)
             )
   ) +
   geom_point(size= 2.5) + 
   geom_smooth(
              method = "lm", 
              se=FALSE, 
              aes(color= as.factor(trait)), 
              size= 1.3, 
              linetype= 1
   ) + 
   xlab("SVL") + 
   ylab("LOG Traits") + 
   theme_bw() + 
   ggtitle("D. turgida)") + 
   theme(plot.title = element_text(hjust = 0.5)) + 
   theme(legend.position="none") + 
   geom_text(
             x = 6.2, 
             y = 2.4, 
             label= "Skull",
             colour= "red"
   ) 

inserir a descrição da imagem aqui

  • Can you post a part of your data? It can be with the result of dput(head(melted_dfF)) to facilitate

  • Yes. I used the melt function to create a df where one of the columns, the "traits" column served as the.factor so that I could plot all 6 variables (Skull, Maxilla, Palatine.... ) in the same graph. melted_dfF <- melt(df_alometriaF, id.vars = "alom_F_tur.CRC", variable.name = "trait", value.name = "trait.value")

  • Structure(list(alom_F_tur.CRC = c(5.67675380226828, 5.89164421182577, 6.02586597382531, 5.60947179518496, 5.90536184805457, 5.61312810638807 ), trait = Structure(c(1L, 1L, 1L, 1L, 1L), . Label = c("alom_F_tur.Skull_length", &#xA;"alom_F_tur.Maxilla_length", "alom_F_tur.Palatine_length", "alom_F_tur.Pterygoid_length", &#xA;"alom_F_tur.Mandible_length", "alom_F_tur.Dentary_length"), class = "factor"), &#xA; trait.value = c(2.287471455184, 2.484906649788, 2.53290284805626, 2.32727770558442, 2.44841554120559, 2.43097830776244)), Row.Names = c(NA, 6L), class = "date.frame")

1 answer

1

Maybe the following will solve the problem.
A data.frame is first created with only the line corresponding to the maximum value of x. Then a color vector for each line of this df. E no geom_text, the argument data takes the value of the new df.

library(tidyverse)

text_dfF <- melted_dfF %>%
  group_by(trait) %>%
  slice_max(order_by = alom_F_tur.CRC) %>%
  mutate(trait = str_replace(trait, "^.*\\.([[:alpha:]]+)_.*$", "\\1"))

cores <- rainbow(length(text_dfF$trait))

melted_dfF %>%
  mutate(trait = str_replace(trait, "^.*\\.([[:alpha:]]+)_.*$", "\\1"),
         trait = factor(trait)) %>%
  ggplot(
    aes(
      x = alom_F_tur.CRC, 
      y = trait.value, 
      colour = trait
    )
  ) +
  geom_point(size = 2.5) + 
  geom_smooth(
    formula = y ~ x,
    method = "lm", 
    se = FALSE, 
    aes(colour = trait), 
    size = 1.3, 
    linetype = 1
  ) + 
  geom_text(
    data = text_dfF,
    mapping = aes(
      x = alom_F_tur.CRC, 
      y = trait.value, 
      label= trait,
      colour = trait
    ),
    hjust = 1,
    vjust = -1,
    inherit.aes = FALSE
  ) +
  scale_color_manual(values = cores) +
  xlab("SVL") + 
  ylab("LOG Traits") + 
  ggtitle("D. turgida") + 
  theme_bw() + 
  theme(plot.title = element_text(hjust = 0.5), 
        legend.position = "none")
  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.