Reputation: 55
this is my desired output , my desired output is to get summary table with chart in single output
T1 <- as.data.frame(table(data[[var]]))
bar_chart <- barchart(data[[var]]))
Upvotes: 0
Views: 425
Reputation: 36
I can't seems to reproduce your example, but I understand you want to combine a table and a plot. I used some hypothetical data as example.
You can create the table as grob
object using tableGrob
function from gridExtra
package, convert the ggplot
to a grob
object using ggplotGrob
from ggplot2
package, and then combine them in different ways.
# making the plot
plot_df <- data.frame(x=LETTERS[1:5], y=1:5)
plot <- ggplot(plot_df, aes(x, y))+
geom_col()
# making the table
table_df <- data.frame(x=LETTERS[1:10], y=1:10)
tabl <- tableGrob(d = table_df)
# making the feck plot that will contain the plot and the table
feck_plot_df <- data.frame(x=0:1, y=0:1)
p <- ggplot(feck_plot_df)+
geom_blank()+
annotation_custom(grob = tabl,
xmin = -Inf, xmax = 0.1, ymin = 0.15, ymax = 0.9)+
annotation_custom(grob = ggplotGrob(plot),
xmin = 0.1, xmax = 1, ymin = -Inf, ymax = 1)
# saving the combined plot
ggsave(plot = p, filename = 'combined_plot.png', device = 'png', dpi = 300,
width = 7.04, height = 3.77)
You can also coerce the flexible table to raster flexible::as_raster
, then to grob using grid::rasterGrob
and arrange things in an empty ggplot. Based on your updates, below is an attempt to arrange the 3 plots.
tabl <- tab_std(data=df, var = "col1", Name_of_variable = "state")
chrt <- donut(df,"col1")
tabl <- tab_std(data=df, var = "col1", Name_of_variable = "state")
bplt <- single_bar(df,"col1", SORT = TRUE)
ggplot() +
theme_void()+
annotation_custom(grid::rasterGrob(as_raster(tabl)), xmin=-Inf, xmax=0.5, ymin=0.5, ymax=Inf)+
annotation_custom(ggplotGrob(chrt), xmin=-Inf, xmax=0.5, ymin=-Inf, ymax=0.5)+
annotation_custom(ggplotGrob(bplt), xmin=0.5, xmax=Inf, ymin=-Inf, ymax=Inf)
tabl <- tab_std(data=df, var = "col1", Name_of_variable = "state")
chrt <- donut(df,"col1")
tabl <- tab_std(data=df, var = "col1", Name_of_variable = "state")
bplt <- single_bar(df,"col1", SORT = TRUE)
grid.arrange(grid::rasterGrob(as_raster(tabl)), ggplotGrob(chrt), ggplotGrob(bplt),
layout_matrix = cbind(rbind(1,2), rbind(3,3)))
Once you get the plot saved somewhere, then you can just include it into your rmarkdown using include_graphics
function from knitr
package. You can also directly include it without saving.
Upvotes: 2