挣扎于线图(ggplot)

时间:2018-08-31 10:17:05

标签: r ggplot2 line-plot

我有以下(样本)数据:

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()

1 个答案:

答案 0 :(得分:3)

如果您要重塑原始数据集然后进行绘图,则无需创建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()

enter image description here