Reputation: 29
R version 4.0.5 (2021-03-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042)
I want to create a percent stacked barchart including 2 groups (regional, international) and the means of 4 different numerical variables (ground low-intensity, ground high-intensity, standing low-intensity, standing high-intensity). The latter variables are representing the duration of each time period in seconds.
My data are: dataset
The image below represents an example of what I kind want to make: Time-motion analysis description relative to total fight time, considering modalities and positions of actions Coswig, V. S., Gentil, P., Bueno, J. C., Follmer, B., Marques, V. A., & Del Vecchio, F. B. (2018). Physical fitness predicts technical-tactical and time-motion profile in simulated Judo and Brazilian Jiu-Jitsu matches. PeerJ, 6, e4851.
I have read a lot of guides and watched many YT tutorials, but most of them are using 2 categorical and 1 numerical variable, thus, it does not work in my case.
Any help or guidance would be highly appreciated.
Thank you in advance.
Upvotes: 1
Views: 527
Reputation: 2288
You will find a lot of friends here, if you provide a reproducible example and show what you have done and where things go wrong.
data
ds <- tribble(
~GROUP, ~GLI, ~GHI,~SLI, ~SHI,~GT,~ST,~EFFORT, ~PAUSE, ~HI, ~LI
,"REG", 158, 48, 26, 4, 205, 30, 235, 10, 51, 184
,"INT", 217, 62, 20, 1, 279, 21, 300, 11, 63, 237
)
{ggplot} works best with long data. Here tidyr is your friend and pivot_longer()
ds <- ds %>%
pivot_longer(
cols=c(GLI:SHI) # wich cols to take
, names_to = "intensity" # where to put the names aka intensitites
, values_to = "duration" # where to put the values you want to plot
) %>%
#-------------------- calculate the shares of durations per group
group_by(GROUP) %>%
mutate(share = duration / sum(duration)
)
This gives you a tibble like this:
# A tibble: 8 x 10
# Groups: GROUP [2]
GROUP GT ST EFFORT PAUSE HI LI intensity duration share
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
1 REG 205 30 235 10 51 184 GLI 158 0.669
2 REG 205 30 235 10 51 184 GHI 48 0.203
3 REG 205 30 235 10 51 184 SLI 26 0.110
4 REG 205 30 235 10 51 184 SHI 4 0.0169
5 INT 279 21 300 11 63 237 GLI 217 0.723
6 INT 279 21 300 11 63 237 GHI 62 0.207
7 INT 279 21 300 11 63 237 SLI 20 0.0667
8 INT 279 21 300 11 63 237 SHI 1 0.00333
With the last columns providing you your categories and % durations, the grouping is done with the GROUP variable. And then you can print it with ggplot.
ds %>%
ggplot() +
geom_col(aes(x = GROUP, y = share, fill = intensity), position = position_stack()) +
scale_y_continuous(labels=scales::percent)
You can then "beautify" the plot, chosing desired theme, colours, legends, etc. Hope this gets you started!
Upvotes: 1