Home > Software design >  R: how to create a scatter plot with 3 columns as one continuance x factor?
R: how to create a scatter plot with 3 columns as one continuance x factor?

Time:06-09

I have a dataframe with ID as rows and several parameters for each ID as columns, among those parameters are columns of "weight at age 1", "weight at age 2", "weight at age 3" and "population". For each population, I would like to create its own scatter plot with age as x aes and weight as y aes, ideally, all population are layered on the same final graph. How do I do that? tnx!!

an example of my data:

ID POPULATION weight at age 1 weight at age 2 weight at age 3
1 A 13.37 14.15 17.36
2 A 5.19 15.34 NA
3 B 7.68 6.92 19.42
4 B 6.96 15.12 36.39
5 C 10.35 8.86 26.33

CodePudding user response:

I attempted to interpret your question.


library(tidyverse)
#pivot data into long format

df <- data.frame(
  stringsAsFactors = FALSE,
                ID = c(1L, 2L, 3L, 4L, 5L),
        POPULATION = c("A", "A", "B", "B", "C"),
   weight.at.age.1 = c(13.37, 5.19, 7.68, 6.96, 10.35),
   weight.at.age.2 = c(14.15, 15.34, 6.92, 15.12, 8.86),
   weight.at.age.3 = c(17.36, NA, 19.42, 36.39, 26.33)
) %>% 
  pivot_longer(cols = weight.at.age.1:weight.at.age.3, 
               names_to = 'age', 
               values_to = 'weight') %>% 
  mutate(age = str_remove(age, 'weight.at.age.'))

#plot data
ggplot(data = df, 
       mapping = aes(x = age, 
                     y = weight)) 
  geom_point() 
  facet_wrap(~POPULATION)

sample

CodePudding user response:

You can reshape your dataframe into a long-format, and then use facet_wrap to create one plot for each population:

library(tidyverse)

df <- expand_grid(population = LETTERS[1:3], age = 1:10, id = 1:3) %>% mutate(weight = rgamma(n(), 1) * 10) %>% 
  pivot_wider(names_from = age, names_prefix = "weight at ", values_from = weight) %>%
  mutate(id = row_number())

df_long <- df %>% pivot_longer(starts_with("weight at "), names_to = "age", values_to = "weight") %>% 
  mutate(age = as.integer(str_extract(age, "\\d ")))

ggplot(df_long, aes(age, weight))   geom_point()   facet_wrap(~ population)

Created on 2022-06-09 by the reprex package (v2.0.1)

  • Related