使用 ggplot2 (facet) 绘制子集数据的线图
Line Plot subset data with ggplot2 (facet)
我有考试成绩数据。我想要 ggline 图在不同年份的阅读中获得 3 个不同的测试分数(3 行)。我希望在相同的年份(4 行)中叠加 4 个不同的测试分数。将所有 7 个都放在一个图上会让人难以阅读。
我可以对数据进行子集化,以便它显示读取或写入,但我似乎无法将它们整齐地堆叠在一起。
年份、ID、测试、分数、类型
2010, 1, R1, 72.75, R
2010, 1, R2, 68.25, R
2010, 1, R3, 83.75, R
2010, 1, W1, 89.25, W
2010, 1, W2, 58.25, W
2010, 1, W3, 64.25, W
2010, 1, W4, 90.25, W
2011, 1, R1, 52.25, R
2011, 1, R2, 90.25, R
2011, 1, R3, 73.25, R
2011, 1, W1, 79.5, W
2011, 1, W2, 89.25, W
2011, 1, W3, 85.25, W
2011, 1, W4, 78.25, W
…
我有这段代码,只需阅读即可生成一张图:
ggplot(subset(df,skill %in% c("R1", "R2", "R3"))) +
geom_line(aes(year, score, group=skill, colour=skill))
我试过这样做来获得两者,但显然是不正确的:
p1 <- subset(df,skill %in% c("R1", "R2", "R3"))
p2 <- subset(df,skill %in% c("W1", "W2", "W3", "W4"))
ggplot()+ geom_line(data=p1, aes(year, score, group=skill, colour=skill)) +
geom_line(data=p2, aes(year, score, group=skill, colour=skill)) +
facet_grid(~type)
如有任何帮助,我们将不胜感激
如果您要根据类型分面,则不需要将数据子集化为 p1 和 p2。
ggplot(df) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type)
或者,添加 nrow =2
来堆叠绘图,添加 scales = "free"
来给它们独立的轴。
ggplot(df) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, nrow = 2, scales = "free")
但是,您的评论说
This does work but it puts the graph side by side instead of stacked vertically. Is there a way to stack them one on top of the other and perhaps separate the legend so reading and writing are separate?
为了解决单独的图例,我会制作每个图,然后使用包 cowplot
进行组合。使用 gridExtra
执行此操作的其他方法,但 cowplot
使这变得简单。
p1 <- ggplot(subset(df,skill %in% c("R1", "R2", "R3"))) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, scales = "free")
p2 <- ggplot(subset(df,type %in% c("W"))) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, scales = "free")
cowplot::plot_grid(p1, p2, nrow = 2)
我有考试成绩数据。我想要 ggline 图在不同年份的阅读中获得 3 个不同的测试分数(3 行)。我希望在相同的年份(4 行)中叠加 4 个不同的测试分数。将所有 7 个都放在一个图上会让人难以阅读。 我可以对数据进行子集化,以便它显示读取或写入,但我似乎无法将它们整齐地堆叠在一起。
年份、ID、测试、分数、类型
2010, 1, R1, 72.75, R
2010, 1, R2, 68.25, R
2010, 1, R3, 83.75, R
2010, 1, W1, 89.25, W
2010, 1, W2, 58.25, W
2010, 1, W3, 64.25, W
2010, 1, W4, 90.25, W
2011, 1, R1, 52.25, R
2011, 1, R2, 90.25, R
2011, 1, R3, 73.25, R
2011, 1, W1, 79.5, W
2011, 1, W2, 89.25, W
2011, 1, W3, 85.25, W
2011, 1, W4, 78.25, W
…
我有这段代码,只需阅读即可生成一张图:
ggplot(subset(df,skill %in% c("R1", "R2", "R3"))) +
geom_line(aes(year, score, group=skill, colour=skill))
我试过这样做来获得两者,但显然是不正确的:
p1 <- subset(df,skill %in% c("R1", "R2", "R3"))
p2 <- subset(df,skill %in% c("W1", "W2", "W3", "W4"))
ggplot()+ geom_line(data=p1, aes(year, score, group=skill, colour=skill)) +
geom_line(data=p2, aes(year, score, group=skill, colour=skill)) +
facet_grid(~type)
如有任何帮助,我们将不胜感激
如果您要根据类型分面,则不需要将数据子集化为 p1 和 p2。
ggplot(df) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type)
或者,添加 nrow =2
来堆叠绘图,添加 scales = "free"
来给它们独立的轴。
ggplot(df) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, nrow = 2, scales = "free")
但是,您的评论说
This does work but it puts the graph side by side instead of stacked vertically. Is there a way to stack them one on top of the other and perhaps separate the legend so reading and writing are separate?
为了解决单独的图例,我会制作每个图,然后使用包 cowplot
进行组合。使用 gridExtra
执行此操作的其他方法,但 cowplot
使这变得简单。
p1 <- ggplot(subset(df,skill %in% c("R1", "R2", "R3"))) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, scales = "free")
p2 <- ggplot(subset(df,type %in% c("W"))) +
geom_line(aes(year, score, group = skill, colour = skill)) +
facet_wrap(~type, scales = "free")
cowplot::plot_grid(p1, p2, nrow = 2)