## R中如何实现分组数据和绘制多行数据？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (43)

```School_ID   Year    Value
A           1998    5
B           1998    10
C           1999    15
A           2000    7
B           2005    15```

### 2 个回答

```dd = data.frame(School_ID = c("A", "B", "C", "A", "B"),
Year = c(1998, 1998, 1999, 2000, 2005),
Value = c(5, 10, 15, 7, 15))```

```plot(dd\$Year[dd\$School_ID=="A"], dd\$Value[dd\$School_ID=="A"], type="b",
xlim=range(dd\$Year), ylim=range(dd\$Value))```

```lines(dd\$Year[dd\$School_ID=="B"], dd\$Value[dd\$School_ID=="B"], col=2, type="b")
lines(dd\$Year[dd\$School_ID=="C"], dd\$Value[dd\$School_ID=="C"], col=3, type="b")```

```require(ggplot2)
##The values Year, Value, School_ID are
##inherited by the geoms
ggplot(dd, aes(Year, Value,colour=School_ID)) +
geom_line() +
geom_point()```

```  library(ggplot2)

df = read.table(text = "School_id Year Value
A           1998    5
B           1998    10
C           1999    15
A           2000    7
B           2005    15", sep = "", header = TRUE)

ggplot(data = df, aes(x = factor(Year), y = Value, color = School_id)) +
geom_line(aes(group = School_id)) + geom_point()```