如果你提供一个可重复的例子并展示你做了什么以及哪里出了问题,你会在这里找到很多朋友。
数据
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} 最适合长数据。这里 tidyr 是你的朋友,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)
)
这会给你一个像这样的小标题:
# 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
最后一列为您提供类别和持续时间百分比,分组是使用 GROUP 变量完成的。
然后就可以用ggplot打印出来了。
ds %>%
ggplot() +
geom_col(aes(x = GROUP, y = share, fill = intensity), position = position_stack()) +
scale_y_continuous(labels=scales::percent)
然后您可以“美化”情节,选择所需的主题、颜色、图例等。
希望这能让你开始!