Problems with plot size in preview window in RNotebook: Only latest plot of chunk is shown in full size, previous plots remain small - window

I have a problem with the preview window of plots from RNotebook.
I am running GAM-models for different tree species and store them in a nested dataframe. When I afterwards run some model diagnostics or plotting function in RNotebook an odd behaviour occurs.
So lets say I want to plot the smooths for each tree species gam model with customized function
# plotting the effects of the model with gratia
gam_plot_gratia_all(gam_base, group_var = group_var)
here my custom-function:
gam_plot_gratia_all = function(gam_input, group_var) {
map2(gam_input$fit_gam, gam_input[[group_var]], ~ show(gratia::draw(.x)))
}
gam_base is a nested dataframe which contains the gam-models in the fit_gam column. The function works and I get three plots:
When I open the plots in the preview window, the last plot (the very right one) is shown properly and after enlarging the preview window, adjusts the single ggplots to the proper size:
However, when selecting one of the previous plots, the single smooth-plots do not adjust to the larger window size, but remain in the default size:
Of course I can create multiple plots and split them on different pages, but for a quick preview, this is quite a nice feature or RNotebook. The example with the gratia-plots is just for demonstration. The behaviour remains the same when I plot other ggplots or use other plotting packages, so maybe there are some options in RNotebook, where I can tackle this problem?
I also tried "print" in my function calls, and also wrapped the print function around my map2-call. I use the latest version of RStudio (2022.07.2 Build 576) and Windows.

Related

Why are my table border look weird in PDF viewers?

I generated a table with iText7 (C#):
var cell = new Cell().Add(new Paragraph(headers[c]).SetFont(font).SetFontColor(ColorConstants.WHITE).SetFontSize(size).SetBold());
cell.SetBackgroundColor(color);
cell.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.SetPadding(0);
cell.SetBorder(new SolidBorder(1));
table.AddCell(cell);
Document has the table, but on certain scalings, it looks weird on the edges:
Taking a closer look on the image above:
If however I change the zoom in the viewer directly, it looks OK:
How do I get rid of these unnecessary parts from the border?
I'm attaching here the resulted PDF for reference:
Download sample PDF
I also noticed that on iText KB pages, there is this kind of behavior:
https://kb.itextpdf.com/home/it7kb/faq/how-do-i-change-the-border-color-of-a-pdfpcell
See the red and blue bars' left edges:
This behaviour is not uncommon in PDF or other print drivers where vectors are printed rather than plotter definitions (often called "Dangles". It would be worse if the definition was rounded or square, rather than butt, and join as "mitre" cannot apply, see below). The overlap is intentional (to ensure both lines are inclusive). In a laser drum print that may be desirable overkill, but disastrous for any inkjet or screen. It looks like the cell is not bordered by a box, but using common straight vectors. Again this is often desirable optimisation but not when the weight is not honoured. Thus it depends if the viewer is using the correct thickness.
All desktop PDF viewers (icluding Chrome and FireFox) I tested showed the lines correctly as clean overlap without "Dangles". Acrobat has a reputation for undesirably thickening or thinning its standard defined lines depending on its user settings.

How to save the data drawn by cuxfilter as an image

I am considering drawing a network.
For example, as shown in the demo, we can use the dashboard to get the chart, but there is no "save button" on the right side, as is often the case.
cux_df = cuxfilter.DataFrame.load_graph((nodes, edges))
chart0 = cuxfilter.charts.dashader.graph(node_pixel_shade_type='linear', unselected_alpha=0.2)
d = cux_df.dashboard([chart0], layout=cuxfilter.layouts.double_feature)
chart0.view()
Since we are using large data, we would like to take advantage of cuxfilter's quick drawings. If holoviews, for example, it takes too long to compute. Screen captures, etc. are possible, but is there any way to save the resulting figure?
Drawing with datashader took too long. I could create a view screen with cuxfilter.
The only way right now is to use the dashboard preview() function, which screen captures the dashboard in it's initial state and saves it as a png file. The way to do that is as follows:
cux_df = cuxfilter.DataFrame.load_graph((nodes, edges))
chart0 = cuxfilter.charts.dashader.graph(node_pixel_shade_type='linear', unselected_alpha=0.2)
d = cux_df.dashboard([chart0], layout=cuxfilter.layouts.double_feature)
await d.preview()
This would only work in a jupyter lab/notebook environment though, and is restrictive in capturing current state.
Based on your suggestion, it was as easy as adding an extra tool to the chart using bokeh, so we ended up adding it as a new feature, for all the bokeh and datashader based charts, the progress can be tracked here. To try it out once the changes are merged, you would have to install the cuxfilter nightly version (23.02). Once the changes are merged, this is how the toolbar would look:

How to display calculation results in namedWindow in opencv (c++)

I used this code to display multiple images in the same namedWindow. Now I want to display some of the calculation results (that I have calculated before with image processing functions).
The results should be displayed in the fourth position of the namedWindow, like I show in the red square in the image below:
Is there any way in opencv that can throw console window in the namedWindow? Can I copy the results from console window in any other way on the named window? Is it possible to create somehow scrolling window with all information and put them on ?
I would suggest creating an new Mat with either ones() or zeros() and then using the function putText to insert your text.
Afterwards show the Mat like the rest before

dc.js heatmap expanding data

I am trying to show machine states over time. Part of this is to reproduce/automate a report that used to be done by hand. It consists of coloring 2minute 'time slices' in Excel based on what the machine is doing.
(Sorry, not enough reputation to post a picture, but it is a classic heatmap where the state drives the color. Some non DC-JS fiddle: http://jsfiddle.net/ww6Lbnc5/4/)
I was able to generate most of what I want in the following jsfiddle:
http://jsfiddle.net/hwhfxz2t/14/
See fiddle for code.
The total state duration (for selected time frame) is shown in the pieChart, followed by the individual state lines and then the heatmap that people are used to. (the ZOOM and date selection buttons do not work in the fiddle but are there to select specific data ranges or zoom in if you like).
The line charts uses the original representation of the states, which consists of a time the state is entered and a duration.
In order to make the heat map work, I had to (I think) take the original data and convert it into individual minute chunks and mark them with a state. So for instance the original data specifying:
RUN state starting 14:30 for 300 seconds
becomes:
14:30=RUN, 14:31=RUN, 14:32=RUN, 14:33=RUN and 14:34=RUN
The code in lines 233-297 loops through the original data and generates a new one that does this. In cases where there is more than one state within a given minute, the last state survives.
This works okay but it seems that this code is exactly what is normally done in group().reduce(add,remove,init). But in this case I need to add multiple timeslots depending on the duration of a state.
Also, because it is now using a different crossfilter, maps do not update each other.
Here are my questions related to this:
Can I display a heatmap without supplying information for all individual
'cells'? (i.e. straddle cells based on a value, similar to rowspan in a table)
Can I add multiple values at once inside group().reduce()?
Is there an easy way to invert the yAxis so 0 is at the top?
When clicking a row in the heatmap, it selects a column and vice-versa?
I'm not sure if this should be in the crossfilter group. If so please ignore my rambling. If someone knows how to keep the charts linked by grouping better, please let me know.
--Nico
Concerning Question 3:
DC.js heatmaps currently do not support custom order functions on axis but there is a pull request that has been merged into the developing branch and should be accessible to the public soon.
You could manually edit the dc.js file to set the sorting in heatmaps to a custom function. In the latest (2.0.0-beta10) version it is the following line:
rowValues.sort(d3.ascending);
and accordingly
colValues.sort(d3.ascending);

Plotting several jpeg images in a single display

I need to plot and display several jpeg images in a single combined display (or canvas?). For example, suppose I have images {a,b,c,d}.jpg, each of different size, and I would like to plot them on one page in a 2x2 grid. It would be also nice to be able to set a title for each subplot.
I've been thoroughly looking for a solution, but couldn't find out how to do it, so any ideas would really help. I would preferably use a solution that is based on the EBImage package.
There are two ways how to arrange several plots with base graph functions, namely par(mfrow=c(rows,columns)) (substitute rows and columns with integers) and layout(mat) where mat is a matrix like matrix(c(1,2,3,4)).
For further info see ?par, ?layout, and especially Quick-R: Combining Plots.
However, as your question is about images I don't know if it helps you at all. If not, I am sorry for misinterpreting your question.
To add to Henriks solution, a rather convenient way of using the par() function is:
jpeg(filename="somefile.jpg")
op <- par(mfrow=c(2,2)
#plot the plots you want
par(op)
dev.off()
This way, you put the parameters back to the old state after you ran the code. Be aware of the fact this is NOT true if one of the plots gave an error.
Be aware of the fact that R always put the plots in the same order. Using mfrow fills the grid row by row. If you use mfcol instead of mfrow in the code, you fill up column by column.
Layout is a whole different story. Here you can define in which order the plots have to be placed. So layout(matrix(1:4,nrow=2) does the same as par(mfcol=c(2,2)). But layout(matrix(c(1,4,3,2),ncol=2)) places the first plot lefttop, the next one rightbottom, the third one righttop, and the last one leftbottom.
Every plot is completely independent, so the titles you specify using the option main are printed as well. If you want to have more flexibility, you should take a look at lattice plots.
If you do not want the images in a regular grid (the different sizes could imply this), then you might consider using the subplot function from the TeachingDemos package. The last example in the help page shows using an image as a plotting character, just modify to use your different images and sizes/locations.
The ms.image function (same package) used with my.symbols is another possibility.

Resources