У меня есть две метрики Мне нужно построить на двух разных линейных диаграммах (очень различный масштаб).Сочетание gridExtra и Facet_wrap/Facet_grid для сетки двойных диаграмм
Используя 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_wrap
/facet_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))
EDIT:
Выборочные данные (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))
#Option 2 - Show charts for my 2 brands but group them by metrics shown rather than Brand
chart.top = ggplot(data=df, aes(x=Days, y=Units.sold)) +
geom_line(size=1) + facet_wrap(~ Brand, ncol=1)
chart.bottom = ggplot(data=df, aes(x=Days, y=Estimated.revenue)) +
geom_line(size=1) + facet_wrap(~ Brand, ncol=1)
chart = grid.arrange(chart.top,chart.bottom, heights = c(1/2, 1/2))
могли бы вы 'dput (ДФ)', чтобы сделать его воспроизводимым? – drmariod
Большое спасибо за то, что нашли время, чтобы посмотреть на это. Я добавил примеры данных, кода и вывода. Надеюсь, это поможет. – xav