Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to use long data for boxplot in R

$
0
0

I am trying to create 3 boxplots for Depression scores for 3 groups, organised as long data in my table as below:

   Ppt_num Depression_score Group   Age Gender
     <dbl>            <dbl> <dbl> <dbl>  <dbl>
 1       1                8     1    32      1
 2       2                4     1    45      2
 3       3               24     1    18      2
 4       4               11     1    45      1
 5       5               13     1    25      2
 6       6               14     1    67      2
 7       7               16     1    43      2
 8       8               11     1    25      1
 9       9                3     1    55      1
10      10               21     1    70      2
# ... with 80 more rows

When I use:

boxplot <- ggplot(mini, aes(x = Group, y = Depression_score, fill=Group)) + geom_boxplot()

It comes out with 1 boxplot instead of 3 and with the x-axis being labelled as 1.5, 2.0, 2.5.

When I then try:

boxplot<- ggplot(mini, aes(x = Group, y = Depression_score, fill=Group)) + geom_boxplot() + facet_grid(~Group)

I successfully get 3 boxplots but as well as being separated by Group number (1, 2, 3) at the top, on the x-axis of each boxplot there's 1, 2, 3 as well so the first one is to the left above 1, the second in the middle above 2 and the third to the right above 3 which looks a little weird.

How can I get the boxplots to be separated like they are with facet_grid but not be divided twice?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>