1

This code prints all of the column labels from both groups on the x - axis which gives 10 labels. It would be nice to have only 5 x-axis labels (for the 5 behaviors), with two data clusters (for the groups) next to each other above each x axis labels. So it should all be on one graph. View the illustration below

library(tidyverse)
library(cowplot)
df <- structure(list(Number_Group1_Behavior1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group1_Behavior2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group1_Behavior3 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group1_Behavior4 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group1_Behavior5 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group2_Behavior1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group2_Behavior2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group2_Behavior3 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group2_Behavior4 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     Number_Group2_Behavior5 = c(1, 2, 3, 4, 5, 6, 7, 8, 9)),
                            class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L))
df_Number <- df %>% 
pivot_longer(
cols = 1:10,
names_to = "names",
values_to = "values"
) %>% 
filter(grepl('Number', names))

plot_Number <- ggplot(df_Number, aes(x = fct_inorder(names), y=values)) +
geom_point(stat = "identity") + 
scale_y_continuous(limits=c(0,220))+
xlab("Number") + 
ylab("Value") +
theme_bw()

plot_grid(plot_Number, labels = "AUTO")

What i am envisioning:

..    ..             .. 
..    ..       ..    ..
               ..    

Behavior1      Behavior2 ....
Ryan Bruno
  • 321
  • 1
  • 8
  • This code returns an empty plot for me. The `filter(grepl('Number', names))` line seems to filter everything out. Please make sure your example is [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Aug 10 '21 at 17:31
  • ah i see. Editting now – Ryan Bruno Aug 10 '21 at 17:36

1 Answers1

1

update: see comments of OP: remove facet_wrap and add position = position_dodge(width = 0.3) code for the plot:

plot_Number <- ggplot(df_Number, aes(x = fct_inorder(Behavior), y=values, color=Group)) +
    geom_point(stat = "identity", position = position_dodge(width = 0.3), size=2) + 
    scale_y_continuous(limits=c(0,20))+
    xlab("Behavior") + 
    ylab("Value") +
    theme_bw()

plot_Number

enter image description here

You could use facet_wrap. First tweak your data with str_remove and separate:

library(tidyverse)
df_Number <- df %>% 
    pivot_longer(
        cols = 1:10,
        names_to = "names",
        values_to = "values"
    ) %>% 
    mutate(names= str_remove_all(names, "Number_")) %>% 
    separate(names, c("Group", "Behavior"), sep = "_")

plot_Number <- ggplot(df_Number, aes(x = fct_inorder(Behavior), y=values)) +
    geom_point(stat = "identity") + 
    scale_y_continuous(limits=c(0,20))+
    xlab("Behavior") + 
    ylab("Value") +
    facet_wrap(~Group)+
    theme_bw()

plot_Number

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • + mutate(names= str_remove_all(names, "Number_")) %>% + separate(names, c("Group", "Behavior"), sep = "_") Warning message: Expected 2 pieces. Additional pieces discarded in 18 rows [9, 10, 19, 20, 29, 30, 39, 40, 49, 50, 59, 60, 69, 70, 79, 80, 89, 90]. – Ryan Bruno Aug 10 '21 at 18:13
  • > plot_grid(plot_Number, labels = "AUTO") Error: `f` must be a factor (or character vector). – Ryan Bruno Aug 10 '21 at 18:13
  • The plot is not working for me. Those are the error messages – Ryan Bruno Aug 10 '21 at 18:15
  • Is there a way to make put it on the same plot? so there would be two sets of datapoints above the same x-axis title. so "Behavior1" will have two scatter clusters side by side, then "Behavior2" would have two scatter clusters side by side, etc.? – Ryan Bruno Aug 10 '21 at 18:28
  • Sorry. I just realized I didn't ask my question very clearly. I have edited it – Ryan Bruno Aug 10 '21 at 18:38