0

I tested several ways to display summary statistics on a ggplot2 histogram,and found the simpler ways by using code below:

library(ggplot2)
library(gridExtra)

cols <- c("Hello","Hi" )
a <- summary(Test_data[cols])
grid.table(a)

This produces nice summary table on the chart but with two column name and stat values.

Since chart is only for a particular attribute, I want to use only one column (say 2nd column). To access that, I use the code below:

grid.table(a[1:6,2:2])

This also works well except it just gives summary stat but no column name.

The structure of summary table is 'table':

> str(a)
 'table' chr [1:6, 1:2] "Min.   :124.5  " "1st Qu.:125.0  " "Median :125.0  " ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "" "" "" "" ...
  ..$ : chr [1:2] "Hello" "Hi"

Is it possible to display in above case just access one column summary with column name as well on the chart? Thanks.

EDIT I add sample data below:

mtcars
cols <- c("mpg","cyl")
a <- summary(mtcars[cols])

Now simply i make a visual table with two columns:

library(gridExtra)    
grid.table(a)

enter image description here

as you can see column name mpg and cyl are visible in the visual above. even

grid.table(a[1:6,1:2])

also works similar way.

but if I use below code to get summary stat for only one column, then visual table displays correct stat but with no column name:

grid.table(a[1:6,2:2])

enter image description here

My purpose is to get column name as well on this single column visual.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    can you share the test data you are using? – setty Jan 28 '21 at 22:42
  • Please see exact demo of the problem with sample data. – user7256821 Jan 29 '21 at 03:54
  • 1
    Try `grid.table(a[1:6, 2:2, drop = F])` instead of `grid.table(a[1:6, 2:2])`. See [here](https://stackoverflow.com/questions/21025609/how-do-i-extract-a-single-column-from-a-data-frame-as-a-data-frame) – Z.Lin Jan 29 '21 at 09:10
  • It worked well and my purpose served. Can I add other attribute to summary result such as stdev, counts etc? – user7256821 Jan 30 '21 at 08:17
  • @user7256821 You may want to create your own table / dataframe of desired summary statistics; that would give you freedom to include / exclude whatever you need for your purpose. – Z.Lin Jan 31 '21 at 04:02

0 Answers0