I am able make chart and table in a single grid but unable to make it aligned . if there is any other solution please help me.
basically i am looking for a function or dynamic solution where I can put a table and any chart in one grid output in Rmarkdown.
Reproducible example below
######## Approach 2
tabl <- tab_std(data=df, var = "col1", Name_of_variable = "state")
bplt <- single_bar(df,"col1", SORT = TRUE)
#side_by_side <- function(tbl,chart)
#{
plot_df <- data.frame(x=0:1, y=0:1)
p <- ggplot(plot_df)+
geom_blank()+
annotation_custom(grob = tabl,
xmin = -Inf, xmax = 0.1, ymin = 0.15, ymax = 0.9)+
annotation_custom(grob = ggplotGrob(bplt),
xmin = 0.1, xmax = 1, ymin = -Inf, ymax = 1)
p
#}
DavoWW
January 14, 2021, 7:37am
2
Hi @str_guru ,
To draw a plot and a table side-by-side, you first need to convert the table into a "grob" (graphical object). Here is an example using the gridExtra
package :
library(ggplot2)
plot1 <- ggplot(data=mtcars, aes(x=disp, y=mpg)) + geom_point()
plot2 <- ggplot(data=mtcars, aes(x=hp, y=disp)) + geom_point(col="blue", size=2)
library(gridExtra)
table1 <- head(mtcars[,1:2])
table1
#> mpg cyl
#> Mazda RX4 21.0 6
#> Mazda RX4 Wag 21.0 6
#> Datsun 710 22.8 4
#> Hornet 4 Drive 21.4 6
#> Hornet Sportabout 18.7 8
#> Valiant 18.1 6
table1_grob <- gridExtra::tableGrob(table1)
plot(table1_grob)
gridExtra::grid.arrange(plot1, plot2, nrow=1)
gridExtra::grid.arrange(plot1, table1_grob, nrow=1)
gridExtra::grid.arrange(plot1, plot2, table1_grob, nrow=2)
Created on 2021-01-14 by the reprex package (v0.3.0)
HTH
system
Closed
February 4, 2021, 7:37am
3
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.