使用数据框中的 table 绘制多线图
Plot multiple line plot with table from a data frame
我正在尝试绘制一个多线图来表示一个频率 table 以便在沿 x 轴的图形下,它还为我提供了 [= 中每个系列的实际值23=]。
例如:
如果有 3 个值绘制在 x 轴上 - 1,2,3;
A系列的频率分别为1-2次、2-5次、3-7次;
B 系列的频率为 1-7 次、2-5 次、3-3 次。
然后在折线图的 x 轴下形成 table,以便在每个 x 值下列出两个系列的频率。
虽然我可以使用 ggplot 创建多线图,但我不确定如何添加 table。
你可以这样做:
df <- data.frame(group = c("A", "A", "A", "B", "B", "B"),
x = c(1, 2, 3, 1, 2, 3),
y = c(2, 5, 7, 7, 5, 3))
library(ggplot2)
plt <- ggplot(data = df, aes(x = x, y = y, group = group, colour = group)) +
geom_line() +
geom_point()
tbl <- tableGrob(t(df), rows = NULL,
theme = ttheme_minimal())
# Plot and table together
library(gridExtra)
grid.arrange(plt, tbl,
nrow = 2,
as.table = TRUE,
heights = c(3,1))
我正在尝试绘制一个多线图来表示一个频率 table 以便在沿 x 轴的图形下,它还为我提供了 [= 中每个系列的实际值23=]。 例如:
如果有 3 个值绘制在 x 轴上 - 1,2,3;
A系列的频率分别为1-2次、2-5次、3-7次;
B 系列的频率为 1-7 次、2-5 次、3-3 次。
然后在折线图的 x 轴下形成 table,以便在每个 x 值下列出两个系列的频率。
虽然我可以使用 ggplot 创建多线图,但我不确定如何添加 table。
你可以这样做:
df <- data.frame(group = c("A", "A", "A", "B", "B", "B"),
x = c(1, 2, 3, 1, 2, 3),
y = c(2, 5, 7, 7, 5, 3))
library(ggplot2)
plt <- ggplot(data = df, aes(x = x, y = y, group = group, colour = group)) +
geom_line() +
geom_point()
tbl <- tableGrob(t(df), rows = NULL,
theme = ttheme_minimal())
# Plot and table together
library(gridExtra)
grid.arrange(plt, tbl,
nrow = 2,
as.table = TRUE,
heights = c(3,1))