0
votes

I want to make a boxplot as follows: on the x axis: the different groups (Healthy, Disease1, Disease2) on the y axis: the brain size , showing 'left brain size' and 'right brain size' side by side in different colors

I am using the ggplot function

data <- df[c("Group", "Left brain size", "Right brain size")]

ggplot(data, aes(x=Group ,y=..))+
  geom_boxplot()

How should I organize my data to have : x = Group , y = brain size , fill = side ?

Thank you! PS: An example of my data is in the following table

Group Left brain size Right brain size
Healthy 0.5 0.9
Healthy 0.4 0.8
Healthy 0.8 0.4
Disease 1 0.7 0.5
Disease 1 0.9 0.3
Disease 1 0.2 0.1
Disease 2 0.3 0.8
Disease 2 0.4 0.54
Disease 2 0.1 0.4
Disease 2 0.3 0.2
1
Hi, you could reshape your data to get a colum "brain size" using, for ex, tidyr::pivot_longer(). It shoul look like: tidyr::pivot_longer(df, cols = c("Left brain size", "Right brain size"), names_to = "side", values_to = "size")Paul
And then, use this new df like this: ggplot(data = df, aes(x = Group, y = size, fill = side)) + geom_....Paul
Does this answer your question? Reshaping data to plot in R using ggplot2Paul

1 Answers

0
votes

This should do it:

df_ %>% 
  rename( # here we rename the columns so things look nice in the graph later
    Left = Left.brain.size,
    Right = Right.brain.size
  ) %>% 
  pivot_longer( # then we collapse the columns for each side of the brain into a single column, with a second column holding size values
    cols = c("Left", "Right"),
    names_to = "Side",
    values_to = "Size"
  ) %>% # then we plot and give it a title
  ggplot(
    aes(
      x = Group,
      y = Size,
      fill = Side
    )
  ) + 
  geom_boxplot() +
  labs(
    title = "Hemisphere Size by Group"
  )

Here is the output:

Box and whisker plot

Is this what you were looking for?