将gridExtra和Facet_wrap/Facet_grid相结合,形成双图表网格
我需要在两个不同的折线图上绘制两个指标(非常不同的比例) 使用将gridExtra和Facet_wrap/Facet_grid相结合,形成双图表网格,r,ggplot2,gridextra,R,Ggplot2,Gridextra,我需要在两个不同的折线图上绘制两个指标(非常不同的比例) 使用gridExtra,我可以将一个放置在另一个之上: chart.top = ggplot(data=df, aes(x=Days, y=Estimated.revenue)) + geom_line() chart.bottom = ggplot(data=df, aes(x=Days, y=Units.sold)) + geom_line() chart = grid.arrange(chart.top,chart.bottom,
gridExtra
,我可以将一个放置在另一个之上:
chart.top = ggplot(data=df, aes(x=Days, y=Estimated.revenue)) + geom_line()
chart.bottom = ggplot(data=df, aes(x=Days, y=Units.sold)) + geom_line()
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2))
现在我想用facet\u wrap
/facet\u grid
创建一个由这些双图表组成的网格,用一个额外的维度分割数据(这里是品牌)。可能吗?下面这样的操作不起作用:
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2)) + facet_wrap(~ Brands, ncol=3)
如果我像下面这样做,我最终会得到每个图表类型一个网格,而不是一个双图表网格:
chart.top = ggplot(data=df, aes(x=Days, y=Estimated.revenue)) + geom_line()
+ facet_wrap(~ Brands, ncol=3)
chart.bottom = ggplot(data=df, aes(x=Days, y=Units.sold)) + geom_line()
+ facet_wrap(~ Brands, ncol=3)
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2))
编辑:
样本数据(dput(df)):
structure(list(Days = structure(c(16685, 16685, 16686, 16686,
16687, 16687), class = "Date"), Brand = structure(c(1L, 2L, 1L,
2L, 1L, 2L), .Label = c("Brand 2", "Brand 3"), class = "factor"),
Units.sold = c(145, 106, 1, 2, 2, 51), Estimated.revenue = c(0.073330174,
0.211338814, 0.000496881, 0.006588271, 0.001008714, 0.047465918
)), .Names = c("Days", "Brand", "Units.sold", "Estimated.revenue"
), row.names = c(NA, -6L), class = "data.frame")
df = read.csv(file="rules_data2.csv", header=TRUE)
df$Estimated.revenue = as.numeric(gsub(",","", df$Estimated.revenue))
df$Units.sold = as.numeric(gsub(",","", df$Units.sold))
df$Days = as.Date(df$Days,"%m/%d/%Y")
#Option 1 - Work for one brand, showing 2 charts on top of each other
df1 = subset(df, Brand == "Brand 2")
chart.top = ggplot(data=df1, aes(x=Days, y=Units.sold)) +
geom_line(size=1)
chart.bottom = ggplot(data=df1, aes(x=Days, y=Estimated.revenue)) +
geom_line(size=1)
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2))
示例代码:
structure(list(Days = structure(c(16685, 16685, 16686, 16686,
16687, 16687), class = "Date"), Brand = structure(c(1L, 2L, 1L,
2L, 1L, 2L), .Label = c("Brand 2", "Brand 3"), class = "factor"),
Units.sold = c(145, 106, 1, 2, 2, 51), Estimated.revenue = c(0.073330174,
0.211338814, 0.000496881, 0.006588271, 0.001008714, 0.047465918
)), .Names = c("Days", "Brand", "Units.sold", "Estimated.revenue"
), row.names = c(NA, -6L), class = "data.frame")
df = read.csv(file="rules_data2.csv", header=TRUE)
df$Estimated.revenue = as.numeric(gsub(",","", df$Estimated.revenue))
df$Units.sold = as.numeric(gsub(",","", df$Units.sold))
df$Days = as.Date(df$Days,"%m/%d/%Y")
#Option 1 - Work for one brand, showing 2 charts on top of each other
df1 = subset(df, Brand == "Brand 2")
chart.top = ggplot(data=df1, aes(x=Days, y=Units.sold)) +
geom_line(size=1)
chart.bottom = ggplot(data=df1, aes(x=Days, y=Estimated.revenue)) +
geom_line(size=1)
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2))
选项1输出
选项2输出
从我的角度来看,
grid.arrange
在这里是多余的。facet\u电网的全功率
用于救援
library(reshape2)
ggplot(data=melt(df, c("Days", "Brand")), aes(x=Days, y=value)) +
geom_line(size=1) +
facet_grid(variable ~ Brand, scales = "free_y")
您能
dput(df)
使其可复制吗?非常感谢您花时间来研究这一点。我添加了示例数据、代码和输出。希望能有帮助。嗨,托尼托诺夫,谢谢你的回答。对于我的示例数据集,您是对的。我错了,只提供了一个子集,使它看起来比实际更容易。我实际上有15个品牌,这使得facet_网格数据集非常长(水平或垂直)。我的目标是一种更为矩形的格式,因此我尝试在一个更可定制的网格(facet\u wrap)上找到一个双图表的解决方案,然后facet\u wrap
it并指定nrow/ncol?