0

I am trying to name the boxplots of different groups in my graph using ggplot:

`ggplot(data, aes(y=Values, group= Groups, x=Groups)) +
  stat_boxplot(geom= "errorbar", width= 0.5) +
  geom_boxplot(fill=c("skyblue2", "gold2"), outlier.color = "black", alpha = 1) +
  labs(x="Different groups", y="Frequency", title="Groups in comparison")+
  theme_minimal()+
  theme(axis.title.x = element_text(size =10))+
  theme(axis.title.y = element_text(size =10))+
  theme(plot.title=element_text(size = 16))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(plot.title = element_text(vjust = 2.8))+
  #scale_x_discrete(labels = c("Group A", "Group B"))+ #<- Code in question
  #xlim(labels= c("Group A","Group B")) + #<- Code in question
  stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
                width = .75, linetype = "dashed", col = "red")`

First I tried with scale_x_discrete(labels = c("Group A", "Group B")), however even though a graph is calculated still no labels for the graphs are displayed:

enter image description here

Secondly, I tried using xlim(labels = c("Group A","Group B")), which gave me the unsatisfying result that can be seen here:

enter image description here Besides, I don't understand why the boxplots appear more slender in the first image.

Here's an example of my data:

                        Values     Groups  ID
1                     1.466667          0 FM1
2                     2.666667          1 FM1
3                     1.133333          1 FM1
4                     1.800000          1 FM1
5                     1.866667          1 FM1
6                     2.133333          1 FM1
7                     2.466667          0 FM2
8                     1.666667          1 FM2
9                     2.600000          1 FM2
10                    1.800000          1 FM2
11                    1.266667          1 FM2
12                    1.066667          1 FM2
13                    1.866667          0 FM3
14                    1.200000          1 FM3
15                    1.466667          1 FM3
16                    2.000000          1 FM3
17                    1.200000          1 FM3
18                    0.800000          1 FM3

I'm still very new to R and thankful for any help to solve my problem!

N3st0r
  • 15
  • 6
  • 2
    Hi, welcome to the stack. This code is working as expected on my machine. Could you provide a [minimal, reprodubile example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a minimal example of `data`? – Captain Hat Jun 13 '23 at 10:28
  • Edited my post with some example `data` :) – N3st0r Jun 13 '23 at 11:05
  • 1
    Please provide your data as the output of `dput(your_data)` this makes it easier to copy for testing of answers. – Peter Jun 13 '23 at 11:17
  • 1
    It looks like your `Group` variable is a numeric variable. Ideally, your x axis should be a factor or character variable so that you can use the `labels=` within `scale_x_discrete`. Consider making a new variable called `Group.Factor <- factor(Group)` in your data, and then within your `aes`, change the variable from `Group` to `Group.Factor`. – David Jun 13 '23 at 11:38
  • @David Thanks for your suggestion, however this now gives me the error `Aesthetics must be either length 1 or the same as the data (84): x and group` which I don't really understand in this context either... – N3st0r Jun 13 '23 at 12:37
  • @Peter thanks for your advice, however I don't understand how this is done and `dput()` is used – N3st0r Jun 13 '23 at 12:41
  • Maybe this link helps: https://stackoverflow.com/questions/49994249/example-of-using-dput – Peter Jun 13 '23 at 12:57
  • I'm not sure what to make of that error either @N3st0r, although I did make a mistake with my suggestion; I used `Group` instead of `Groups`. What do you get if you replace your `aes` in Line 1 with this `aes(y=Values, group= factor(Groups), x=factor(Groups))` – David Jun 13 '23 at 15:26
  • @David Using `aes(y=Values, group= factor(Groups), x=factor(Groups))` with `scale_x_discrete` worked out for me! Thanks a lot! – N3st0r Jun 13 '23 at 17:32
  • Great @N3st0r. I think what happened is that your `Groups` column is numeric and so `ggplot` wants to plot it on a continuous x axis from the `aes`. For boxplots, the convention is to have one axis as ordinal or categorical. You may want to read about `factor()` and `as.character()`, which converts a (hopefully integer) numeric variable into an ordinal/categorical variable for plotting. – David Jun 13 '23 at 18:14
  • @David I'm not sure about this, as I used `as.factor` for my `Groups` beforehand and it also did not work with `Group.Factor <- factor(Group)`, which makes me wonder why using `factor(Groups)` in `aes` directly worked out then. However I'm just happy it works for now ^^ – N3st0r Jun 13 '23 at 18:36
  • Hi @N3st0r, I think the `Group.Factor <- factor(Group)` is my error because I should have said "Groups" instead of "Group". I have no clue why your `as.factor` idea didn't work - that is also a good idea. However, I'm happy that you got it. – David Jun 13 '23 at 18:46

0 Answers0