挣扎于线图(ggplot)

Struggling with a lineplot (ggplot)

我有以下(示例)数据:

testdata <- data.frame(theft=sample(size=100, c("yes", "no"), replace=T),
                   assault=sample(size=100, c("yes", "no"), replace=T),
                   robbery=sample(size=100, c("yes", "no"), replace=T),
                   agegrp=sample(size=100, c("10-20", "21-40", ">40"), replace=T))

theft <- table(testdata$theft, testdata$agegrp)[2,]
assault <- table(testdata$assault, testdata$agegrp)[2,]
robbery <- table(testdata$robbery, testdata$agegrp)[2,]

table <- rbind(theft, assault, robbery)

我的目标是创建一个线图(使用 ggplot)显示三个不同年龄组的线(针对每种犯罪类型)。我首先必须将它们重新排列成这样吗?

offence  agegrp   count
/--------/--------/---------
theft    >40      22
theft    10-20    11
theft    21-40    22
...      ...      ...

我该怎么做(不是手动)?那么我将如何绘制它?

ggplot(data, aes(x=agegrp, y=count, color=offence) + geom_line()

如果您设法重塑原始数据集然后绘图,则无需创建 table 数据集:

library(tidyverse)

testdata %>%
  group_by(agegrp) %>%                # for each age group
  summarise_all(~sum(.=="yes")) %>%   # count "yes" in all columns
  gather(offence,count,-agegrp) %>%   # reshape data
  mutate(agegrp = factor(agegrp, levels = c("10-20","21-40",">40"))) %>%  # specify order of levels (useful for plotting)
  ggplot(aes(x=agegrp, y=count, color=offence, group=offence)) + 
  geom_line()