1
$\begingroup$

enter image description here

I am trying to create a graph in R that has 7 variables on the x axis, and size on the y axis. Each variable has 2 treatments and sex (drug 1/2 & M/F). I am struggling to understand how to:

  1. manipulate my data and
  2. plot the data.

The above regions are the volumes of 7 body regions & The IDs are my different subjects. The data is non-repetitive; however, subjects with similar "TREATMENT" background colors are from the same litters. Litter 1 (A-G & N-T)
Litter 2 (H-M & U-Z)

I would like to visually show the different body region sizes by treatment and sex. I assume a grouped boxplot would be the nicest visually but I am open to other ideas. A sample of the data is below:

    dput(MRI_hh)
structure(list(ID = c("A", "B", "C", "D", "E", "F", "G", "H", 
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", 
"V", "W", "X", "Y", "Z"), Treatment = c("Blue", "Blue", "Blue", 
"Blue", "Red", "Red", "Red", "Blue", "Blue", "Blue", "Red", "Red", 
"Red", "Blue", "Blue", "Red", "Red", "Red", "Red", "Red", "Blue", 
"Blue", "Blue", "Blue", "Red", "Red"), Sex = c("Female", "Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Female", 
"Female", "Female", "Female", "Female", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male"), `Region 1` = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
), `Region 2` = c(15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40), 
    `Region 3` = c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), 
    `Region 4` = c(8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
    19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33
    ), `Region 5` = c(10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 
    60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 
    125, 130, 135), `Region 6` = c(12, 14, 16, 18, 20, 22, 24, 
    26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 
    56, 58, 60, 62), `Region 7` = c(2, 8, 14, 20, 26, 32, 38, 
    44, 50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 110, 116, 122, 
    128, 134, 140, 146, 152)), row.names = c(NA, -26L), class = c("tbl_df", 
"tbl", "data.frame"))

Here is kind of an idea I was thinking of. In the sample set, I only included one of the two groups. I also have an exposure timing variable in my total dataset. enter image description here

$\endgroup$
8
  • 1
    $\begingroup$ Notice to potential close-voters: I believe the data-visualization aspect predominates here, so this appears to be on-topic to me. $\endgroup$ Commented Jan 26, 2022 at 20:38
  • $\begingroup$ Can you edit your post to include your data, ideally by including the output from dput(yourdataframe)? See stats.meta.stackexchange.com/a/6098/1352. $\endgroup$ Commented Jan 26, 2022 at 20:39
  • 2
    $\begingroup$ Should work now. $\endgroup$ Commented Jan 26, 2022 at 21:33
  • 1
    $\begingroup$ Can you say what the data are? Is each row a different person / company / criter (etc.)? Ie, are these repeated measures? What is the outcome? What are the treatments? $\endgroup$ Commented Jan 26, 2022 at 21:36
  • 1
    $\begingroup$ How many boxplots do you want to make altogether? // What are the constraints? It seems you will have several panels of boxplots; how many boxplots do you want to put in each panel? What comparisons are most important to you? Are you able to include color graphs in your report? $\endgroup$
    – BruceET
    Commented Jan 27, 2022 at 1:25

1 Answer 1

2
$\begingroup$

You need to add litter to your dataset. Then reshape your data into 'long' form, so you have two matching datasets. From there, you can plot the individual variables with boxplots, but not the full combination, because you have only 1 datum per combination. Instead, you could make a line plot for each mouse, using different symbols and colors to indicate what groups they belong to.

d = structure(list(ID = c("A", "B", "C", "D", "E", "F", "G", "H", 
              ... 
              "tbl", "data.frame"))
d$litter = ifelse(d$ID%in%c("A", "B", "C", "D", "E", "F", "G", 
                            "N", "O", "P", "Q", "R", "s", "T"), 
                  "l1", "l2")

dl = reshape(d, varying=names(d)[4:10], v.names="volume", timevar="region",
             idvar="ID", direction="long")

windows()
  layout(matrix(1:4, nrow=2))
  boxplot(volume~Treatment, dl)
  boxplot(volume~Sex, dl)
  boxplot(volume~litter, dl)
  boxplot(volume~region, dl)

window()
  plot(1:7,1:7, xlim=c(1,7), ylim=range(dl$volume), type="n", xlab="region",
       ylab="volume")
  for(i in 1:26){
    lines(1:7, d[i,4:10], lwd=2, col=d$Treatment[i], 
          lty=ifelse(d$litter[i]=="l1",1,3))
    points(1:7, d[i,4:10], lwd=2, col=d$Treatment[i], 
           pch=ifelse(d$Sex[i]=="Male",3,1))
  }

enter image description here

enter image description here

These data look awfully suspicious to me.

$\endgroup$
8
  • $\begingroup$ Thank you for this idea but I was thinking more of something like this.[reverton.net/index/2017/08/03/visualization-of-survey-data/] $\endgroup$ Commented Jan 26, 2022 at 22:48
  • $\begingroup$ That's a bar plot, @ErikaBauer. I'm not really impressed by it. If you wanted to display means, it might be OK... with error bars. But even then, it wouldn't respect the fact that the measures are nested within organisms (mice?). $\endgroup$ Commented Jan 27, 2022 at 1:04
  • $\begingroup$ I like your idea of showing descriptive stats first!! I would like to showcase each of the regions individually instead of on one graph. I attached a better idea of what I am trying to create $\endgroup$ Commented Jan 27, 2022 at 19:12
  • $\begingroup$ You can do that if you want. $\endgroup$ Commented Jan 27, 2022 at 19:29
  • $\begingroup$ How do you code multiple graphs together and then in a larger group and with one legend for the whole thing? $\endgroup$ Commented Jan 27, 2022 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.