您需要一个堆积柱形图,它在 y 轴上显示每人工作时间的绝对值(总计),每种活动类型。
首先,像这样组织您的数据集:
person hours.worked activity
1 18 1
1 20 2
...
3 11 3
3 28 4
...
1 4 5
1 8 6
然后,执行以下操作:
#reproducible example (same as OP's data)
df = structure(list(person = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L),
hours.worked = c(18L, 20L, 32L, 75L, 64L,18L, 40L, 25L, 2L, 4L, 17L, 20L, 58L, 45L, 32L, 75L, 64L, 18L, 10L, 15L, 11L, 28L, 15L, 92L, 11L, 11L, 2L, 5L, 4L, 8L),
activity = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L)),
.Names = c("person", "hours.worked", "activity"),
class = "data.frame",
row.names = c(NA, -30L))
df$person = factor(df$person,levels=c(1,2,3)) #control person order in x axis (left to right)
df$activity = factor(df$activity,levels=c(6,5,4,3,2,1)) #control order of stacks within column (top to base)
library(ggplot2)
ggplot(data=df, aes(x=person,y=hours.worked,fill=activity)) +
geom_col(position="stack") +
scale_fill_manual(breaks = c(1,2,3,4,5,6), #control order of legend keys (top to bottom)
values = c("#F564E3","#619CFF","#00BFC4","#00BA38","#B79F00","#F8766D")) #control fill of legend keys and columns stacks (surprisingly, this will honor the order of factor levels instead of order of 'breaks')