0

I need to modify these codes in order to consider one individual at a time:

plot(breaths$breathing~as.factor(breaths$event))
plot(breaths$breathing~as.factor(breaths$event),
     names=c("control","grooming"),
     xlab="event", ylab="breathing rate (b/m)", pch=19)

In my dataset, these are the following columns: event, breathing, and id. With events being either grooming or control. Each individual (id) falls into both categories (grooming, control). I just want to plot a graph for each individual. Which codes do you think I should add? Thanks in advance!

r2evans
  • 141,215
  • 6
  • 77
  • 149
Mimi
  • 3
  • 1
  • Miranda, it is inherently difficult to help, without a reproducible example. Read up on this or - as a minimum - provide the output of `dput(head(breaths, 20))`. From your code, you plot 2 vectors, i.e. breathing vs event. Plotting each individual, I am assuming this is a reasonable small number of individuals. For this you can subset your data frame breaths per id, e.g. breaths[which(breaths$id == 5)] gives you a dataframe for the 5th individual. Alternatively, you can use colours and keep all together. If you provide the example and tell us what you did and look for ... there will be help! – Ray Aug 09 '21 at 16:48

1 Answers1

3

Sample data:

set.seed(42)
dat <- data.frame(event = sample(c("grooming", "control"), 100, replace=TRUE), id = sample(c("Aa", "Bb", "Cc"), 100, replace=TRUE), breathing = runif(100))
head(dat)
#      event id  breathing
# 1 grooming Bb 0.35110692
# 2 grooming Aa 0.15902238
# 3 grooming Bb 0.30409800
# 4 grooming Aa 0.01754832
# 5  control Bb 0.99655268
# 6  control Cc 0.80439331

One method of vis:

library(ggplot2)
ggplot(dat, aes(breathing)) +
  geom_density(aes(color = event)) +
  facet_wrap(id ~ .)

ggplot2 density plots

or if you prefer points:

ggplot(dat, aes(event, breathing)) +
  geom_point(aes(color = event)) +
  facet_wrap(id ~ .)

ggplot2 points

The key component here is facet_wrap that will split the data up by id.


Side points:

  • When using the ~ formula methods, it is generally preferred to use data= instead of including the breaths$ in each variable; while not required, it does produce a slight aesthetic difference (axis names). (I also find the code easier to read.)

    par(mfrow = c(1,2))
    plot(mtcars$disp ~ mtcars$mpg)
    plot(disp ~ mpg, data = mtcars)
    

    sample plots showing the use of 'data' argument

  • It's not clear what names= is here; lacking sample data, I get warnings about "names" is not a graphical parameter. If you are certain that you are not getting that warning (repeatedly for one plot call), then your data is not simply a data.frame.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    well done give the question. Nice and elaborate answer! – Ray Aug 09 '21 at 16:49
  • Awesome! Thanks for that. Just one further question: what does the value 100 stand for? when you coded "set.seed(42)..."etc. – Mimi Aug 09 '21 at 17:13
  • @MirandaVentrella the 42 is the seed, it is a joke on the answer to everything, any number would work, the 100 is how many random samples he drew with the function `sample()` – Bruno Aug 09 '21 at 17:59
  • Thanks everyone, I really appreciate your help! Sorry if I keep this going but I'm new to R, I'm trying to understand how it works. So, I have another question for you. In my dataset, breathing rates are known: # event id breathing # 1 grooming Bb 35 # 2 grooming Aa 28 # 3 control Bb 32 # 4 control Aa 33 How am I supposed to adapt your code (the data.frame one) in order to get the corresponding breathing rates of each individual mentioned in the code? – Mimi Aug 10 '21 at 11:35
  • Are you trying to summarize the data so that you have one number (perhaps median, average, min/max, percentile, etc) per individual? If so, look at https://stackoverflow.com/q/11562656/3358272 for several methods for doing that. – r2evans Aug 10 '21 at 13:40