-1

I'm using ggplot2 in R

This is what my dataset looks like:

| Value | Big_condition | little_condition |
|-------|---------------|------------------|
| 10    | a             | A                |
| 12    | a             | B                |
| 11    | a             | A                |
| 6     | b             | B                |
| 10    | b             | B                |
| 8     | b             | A                |
| 9     | c             | B                |

That's my code:

#Thanks Jordo82 for this part    
dataset <- data.frame(Value = c(10,12,11,6,10,8,9),
                      Big_condition = letters[c(1,1,1,2,2,2,3)],
                      little_condition = LETTERS[c(1,2,1,2,2,1,2)])

# My ggplot code
p <- ggplot(data=dataset, aes(x=dataset$little_condition , y=dataset$value)) + 
  geom_boxplot() + 
  ylim(0, 20) + 
  theme_classic() + 
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0.2) + 
  facet_grid(cols=vars(dataset$big_condition))

Here is what I get :

image from https://imgur.com/a/S3MGXst

I would like to reverse the order of the "little" conditions (B, A), and choose the order of the "big" conditions (for example c, a, b, e, f, d).

How to do this?

thanks!

(this has nothing to do with it but I'm also looking for a way to display only the average value of my points, without the rest of the boxplot being displayed).

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Alex
  • 1
  • 2

1 Answers1

2

To change the order in the plot, you have to reorder the factors. As for your second question, to only plot the average values at each point, summarise the data first, then plot using geom_point.

library(tidyverse)

dataset <- data.frame(Value = c(10,12,11,6,10,8,9),
                      Big_condition = letters[c(1,1,1,2,2,2,3)],
                      little_condition = LETTERS[c(1,2,1,2,2,1,2)])

#calculate the average value for each combination of conditions
dataset_mean <- dataset %>% 
  group_by(Big_condition, little_condition) %>% 
  summarise(MeanValue = mean(Value))

dataset %>%
  #reorder the factors to control the order in which they are plotted
  mutate(Big_condition = factor(Big_condition, levels = c("c", "a", "b")),
         little_condition = factor(little_condition, levels = c("B", "A"))) %>% 
  #create the plot
  ggplot(aes(x=little_condition , y=Value)) + 
  #plot a point for all values
  geom_point() + 
  #plot a line for the mean of values
  geom_point(data = dataset_mean, aes(x=little_condition , y=MeanValue), 
            color = "red", size = 6, shape = 95) +
  ylim(0, 20) + 
  theme_classic() + 
  facet_grid(.~Big_condition)

enter image description here

Jordo82
  • 796
  • 4
  • 14
  • Hi, thank you very much! I'm trying your solution (with all your code) but this is the first time I've seen %>% and R studio doesn't seem to like it. R tells me _"Error in dataset %>% mutate(Big_condition = factor(Big_condition, levels = c("c", : impossible to find function "%>%"_ (For the second question, I actually want all my individual points plus a line for the average. ) – Alex Dec 12 '18 at 14:41
  • Edited my answer to plot all points AND the average. Make sure you install and load the `tidyverse` package to be able to use the piping operator `%>%`. It's an incredibly useful tool, learn more here: https://r4ds.had.co.nz/pipes.html – Jordo82 Dec 12 '18 at 15:44