How to specify titles for tableGrobs in grid.arrange (from gridExtra)? - r

It looks like grid.arrange() ignores the name attribute of tableGrob. Let's say I want to plot tables with titles...
baz<-lapply(seq(3),function(ii)
tableGrob(format(matrix(runif(8),nrow=2,
dimnames=list(LETTERS[1:2],letters[1:4])),digits=3),
name=paste0('n',ii)));
This plots them but without titles: do.call(grid.arrange,c(baz,nrow=1))
It seems like overkill to screw around with creating lists of textGrobs, calculating their height, etc. I just want to force there to be an extra line above the header of each table, displaying the name I gave to that table. Or some built-in argument to do so.

Related

How to break one table row into multiple lines in RMD

summary <- summary(data)
knitr::kable(summary)
I have the following code in RMD. However, there are more than 20 attributes in the dataset, so that the width of the table generated by summary() is too larger to show the entire table in a row in the page.
Just wondering how could I do to adjust the summary() table?
In addition to r2evans answer, maybe there is another way of reducing the table width :
1- In a table : you should consider transposing the columns into lines. The 6 statistics of summary() should be the columns, and the variables (attributes, according to you) should be the line, e.g.,
summary <- t(summary(data)) # just a basic version, for clarity.
2- In a graphic : if you want all of your attributes renders like columns, maybe you should consider some boxplot (or geom_col and geom_errorbar), with facets or by using an x axis-values for each attribute ? With facet_grid() or facet_wrap(), it's more easy to compare a bunch of attributes than in a big table. If your want to compare things, fix the scales between the facets. If necessary, you can use the grid package, in order to define each facet size and aspect...
Excellent day

How to edit legend labels in google spreadsheet plots?

I'm trying to plot some data in Google spreadsheet:
And as you may see all of the series are in a same column and I can't use the any of the rows as headers. My plot looks like this:
I would appreciate if you could help me know how I can edit/add legend labels.
Unfortunately I don't think the graphs were intended for your data format.
The only way I've been able to work out how to add a header is by reformatting my data so I have the header at the top of my ranges.
So I suggest you split your data into separate columns so you have;
D2:D4, E2:E4, F2:F4 etc with D1, E1, and F1 as your header and then selecting "Use row 1 as your headers." From the data menu.
Alternatively, you could add labels to the series themselves. Although not ideal, it could be quicker than reformatting your data. On the data menu, click on the three dots of a data series to bring up a menu that allows you to add a label. Unfortunately this needs to be a cell so you'll have to have that header on your sheet some where.
I had the same question this year (2021), and it looks like there's support for it now. In short, you just have to double-click on the empty legend item for the series, and it'll give you a place to put text, directly in the chart.
Here's what my chart looked like (with no series titles):
If you double-click on one, you'll get a small text input area.
Type in your series name and then press enter or click outside of the input area.
Repeat this for each series, and you should be good to go.
I had the same problem because I was selecting only the data I wanted to include in the chart and not the entire table, headers and all. When you create a chart in Google Sheets and you want to use a row or column (or both) as strings that are included in the chart's labeling, you must select the entire table before choosing the Chart command from the Insert menu.
After doing so, you may need to do all or some of the following:
Switch rows/columns if your axes are not correct
Eliminate any rows/columns that you don't want represented by editing the Data Range field in the chart editor
Select Use column A as headers or Use row 1 as headers
You could also potentially use the add labels option when clicking on the 3 dots in the series section:
Double-click on the symbols in your legend, then double-click on the symbol for the series you would like to label; you should be able to directly enter your legend label now. You can do further text formatting using the chart editor on the right.
I took them to a new sheet. first selected only the X-axis and one of the columns that will appear as one. label. In this sheet, I made all the "labels" as a header of the sheet. and added them one by one as a series. and this time it. add them as a label.
obviously this is a workaround and the bug should be fixed :) hopefully.

tableGrob not fully shown between two plots using grid.arrange in R

My specific issue is evident when I output 3 plots to a html report using grid.arrange, sometimes the last plot (which is a tableGrob) is cropped.
I am using 3 separate grid.arrange() statements to place a boxplot, a time series plot and a tableGrob in a html report using knitr.
Code being used to output is:
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(over5)>0){
tg=tableGrob(over5,theme=tt3,rows = NULL)
tg$widths = unit(rep(1/4, ncol(tg)), "npc")
grid.arrange(tg)
}
This displays perfectly when the tg object has less than roughly 10 rows of data.
However in testing larger datasets I found that the grid.arrange(tg) outputs in a fixed height "window" in the report which is not desirable.
My question is how can I change the height of the grid.arrange(tg) ouput to match the height of the tg object contained inside (with some top and bottom margin)??
I would appreciate understanding the mechanism by which these properties are changed so I can fine tune my output. I would have a 3x1 grid display all 3 outputs in one line of code if possible but all plots get condensed and unreadable so that is why I have 3 separate grid.arrange() statements. I Guess my real issue is I don't understand how there properties are manipulated here.
Help is greatly appreciated.
I found a workaround to use kable which prints a HTML table to the report instead of grid.arrange() with tableGrob() :
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(tg)>0){
print(knitr::kable(tg))
}
Also as I was running this code within a for loop I needed to use a workaround on this issue page which requires me to wrap the kable in a print() statement.

R - getting box plot to work

boxplot(data=max_urkoma[[2]])
I have a data set max_urkoma[[2]] that I'd like to put into a box plot, but it won't show at all unless I give it the formula max_urkoma[[1]]~max_urkoma[[2]], and then it'll basically just become a set of points on a plot. What am I doing wrong? The plot is acting like every item in the data set is a separate object, I want them grouped together like in this one:
http://onlinestatbook.com/2/graphing_distributions/graphics/figure4.jpg
THis is how mine looks like http://imgur.com/F2fYPMY
To get a single boxplot, you can do boxplot(mtcars$mpg), or with your data it looks like you need boxplot(max_urkoma[[2]]).

putting text next to a plot in R

I have a plot with many "lines" in it (using points with type being l).
I would like to put a small piece of text next to them. I could do that all sort of ways, such as using mtext, but the problem is that there are many plots to be generated automatically, and there could be many lines in a single plot, so I would like the text to be placed automatically close to the line in an empty spot...
Is there a way to do that?

Resources