Title in plot_grid in rstudio - rstudio

I am using the following code to set up 9 plots in one figure, and to save it
plotpm2.5 <- plot_grid(p1,p2,p3,p4,p5,p6,p7,p8,p9, ncol = 3, nrow = 3, align
= "h")
save_plot("plotpm2.5.png", plotpm2.5,
ncol = 3,
nrow = 3,
base_aspect_ratio = 1.5)
how can I add a main title to this?

I'm not too sure about is but here us something that might help. For example:
title <- ggdraw() + draw_label("label", fontface='bold')
plot_grid(title, x, ncol=1, rel_heights=c(0.1, 1))
rel_heights controls the margin values for the title. Play with the arguments inside plot_grid to finally get a title that suits you.

Related

How to change font family of a heatmap created by the pheatmap package

I would like to change the font family of the following graph to Times New Roman,
but couldn't figure out how. Any help will be greatly appreciated!
A reproducible example:
d <- data.frame ( c(runif(5, min=0, max = 5)), c(runif(5, min=0, max = 5)), c(runif(5, min=0, max = 5)), row.names = c("gene1", "gene2", "gene3", "gene4", "gene5"))
colnames(d) <- c("Day 1", "Day 2", "Day3")
pheatmap(d)

Making a matrix in a for loop

I am currently working with mathematica and I got stuck on some technicalities.
Rvec[R_] := UnitVector[Length[R], RandomInteger[{1, Length[R]}]]
Fvec[R_] := R - Rvec[R] + Rvec[R]
vec[R_] := Module[{S = Fvec[R]}, If[Count[S, -1] > 0, R, S]]
Loop[R_, n_] := For[i = 1; L = R, i < n + 1, i++, L = vec[L]; Print[L]]
The idea is that I now have a loop going that will randomly subtract one number from one entry in a set and add it to another in the next iteration, but with the catch that no entry can drop below zero. The output I then get is a set of outcomes put beneath each other.
Having done that I would like to know how I could put the entire output in the form of one matrix:
https://i.gyazo.com/a4ef70ba5670fd53003e0ac5ec1e434e.png
Instead of having the output like that, I would like to have it in matrix form, as in having this set of outputs placed in a larger set containing those sets as elements. This would greatly help me, as I would be able to manipulate and work with the entire output.
If you need to make matrix by consequently adding vector by vector, you can do like this:
vector = {1, 2, 3, 4, 5};
matrix = {}; (* Initialize matrix *)
Do[matrix = Append[matrix, vector], 5]; (* Construct matrix by adding line by line*)
MatrixForm[matrix] (* Print matrix *)
Please tell me If I didn't understanf youy problem properly.

Is there any way to control the concatenation of the blockproc output?

This is a follow up to the question: Overlapping sliding window over an image using blockproc or im2col?
So by using the code :
B = blockproc(A, [1 1], #block_fun, 'BorderSize', [2 2], 'TrimBorder', false, 'PadPartialBlocks', true)
I was able to create an overlapping sliding window over my image and calculate the dct2 for each window. But the problem is that blockproc concatenates the output in a way that I cannot use. The output greatly depends on the block size and the size of the output matrix is different because of it every time.
My dct2 function creates a 1 x 200 vector for every block or window. So I assumed that if there are 64 blocks I should get something like 64 x 200 or 200 x 64 output, but I get something like 64 x 1600 or in case of larger blocks I get 15 x 400.
Looking into the blockproc function the problem is caused by
% write 4 corner blocks
b(1:ul_output_size(1),1:ul_output_size(2),:) = ul_output;
if ll_processed
last_row_start = final_rows - size(ll_output,1) + 1;
last_row_width = size(ll_output,2);
b(last_row_start:end,1:last_row_width,:) = ll_output;
end
if ur_processed
last_col_start = final_cols - size(ur_output,2) + 1;
last_col_height = size(ur_output,1);
b(1:last_col_height,last_col_start:end,:) = ur_output;
end
if lr_processed
last_row_start = final_rows - size(ll_output,1) + 1;
last_col_start = final_cols - size(ur_output,2) + 1;
b(last_row_start:end,last_col_start:end,:) = lr_output;
end
Apparently, blockproc further divides the blocks into upper left, upper right, lower left and lower right and concatenates that result. And that is why I am getting all this mixed outputs.
I need the output of each block in its each row, for each window. Each window should just give me a 1x200 output, that I can feed into my classifier.
Can I force the output of blockproc in the way that I want it, just give the output of each block.
If not, I would really appreciate an alternative solution to have an overlapping sliding window over the image.
edit: would it be possible to save the blocks data using block_struct.data for every block into a cell array inside the function block_fun and then use that array to extract my features?
Thank you
edit:
B = blockproc(images_m{1}, [64 64], #(x)reshape(x.data(:),[1 1 numel(x.data)]), 'BorderSize', [10 10], 'TrimBorder', false, 'PadPartialBlocks', true, 'PadMethod', 'replicate');
imgs = {};
for i = 1:size(B,1)
for j = 1:size(B,2)
tempy = squeeze(B(i,j,:));
tempy2 = reshape(tempy, [84 84]);
feats{end+1} = block_dct2(tempy2); %calculates dct2 for the block and returns a 1*200 vector
end
end
Maybe reshape you data in the third dimension?
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> B = blockproc(A, [1 1], #(x)reshape(x.data(:),[1 1 numel(x.data)]), 'BorderSize', [1 1], 'TrimBorder', false, 'PadPartialBlocks', true);
>> whos B
Name Size Bytes Class Attributes
B 3x3x9 648 double
>> squeeze(B(1,1,:))
ans =
0
0
0
0
8
3
0
1
5
>>
An alternate using MAT2CELL:
function extractFeatures
images_m{1} = rand(128);
B = blockproc(images_m{1}, [64 64], #processBlock,...
'BorderSize', [10 10], 'TrimBorder', false,...
'PadPartialBlocks', true, 'PadMethod', 'replicate');
%B is 2x400 i.e 2x2 blocks of each block being a 1x200 feature vector
m = ones(1,size(B,1));
n = 200*ones(1,size(B,2)/200);
% The MAT2CELL help does a good job, just read it carefully and run the
% examples
feats = mat2cell(B,m,n);
feats = feats(:);
end
function feature = processBlock(bstruct)
% I dont know what block_dct2 does:
%feature = block_dct2(bstruct.data);
% So I'll put in a place holder which returns a 1x200 'feature'
% for each overlapping image block
feature = repmat(mean(bstruct.data(:)), [1 200]);
end

For loop inside a for loop?

I wish to write a code that would give me a [5x5] matrix containing values of "ec" for each step. But here I can only return its last value. Could you please help me?
Thanks for your interest
R = [0.13, 0.131, 0.132, 0.133, 0.134];
k = [1, 1.5, 2, 2.5, 3];
a = 3*60*6/1000;
for i=R
ec = 30 * (i*a + i*a*k/100)
endfor
It looks like you want something like
ec = zeros(5);
R = [0.13, 0.131, 0.132, 0.133, 0.134];
k = [1, 1.5, 2, 2.5, 3];
a = 3*60*6/1000;
for i_=1:length(R)
for j_=1:length(k)
ec(i_,j_) = 30 * (R(i_)*a + R(i_)*a*k(j_)/100);
end
end
unless I'm mistaken about your question. This should return a 5x5 matrix ec.
A note about for loops: you should avoid using i as a counter, because this is predefined as equal to sqrt(-1), and if you reassign it there can be problems. Adding an underscore avoids this problem.

Need Mathematica short code like these same from Maple

I have one problem with exportation results from Mathematica. Two matrices A and B have to be exported in special form.
These two codes make a list of data exported from Maple.
It is important that exported file opened with wordpad looks like column (File attached).
Please, just if you already checked that it is working, write me answer, thank you! You can check your answer comparing with files down.
Codes are here
Matrices A and B with code in Maple and exported file
http://www.2shared.com/file/49wW8Z0-/EXAMPLE_EXPORT_MAPLE_FINAL.html
And also I will present it here to everybody can see easy
Code 1)
A := Matrix(2, 2, {(1, 1) = (455200000000/6133413)*w(1), (1, 2) = -(1792000000000/116534847)*w(1), (2, 1) = (455200000000/6133413)*w(2), (2, 2) = -(1792000000000/116534847)*w(2)})
precision := double: writeto(`Aexport.for`):
for i from 1 to 2 do:for j from 1 to 2 do:
if A[i,j]<>0 then codegen[fortran]([A00[i,j]=A[i,j]],optimized):
fi:od:od:writeto(terminal):
Code 2)
B := Matrix(2, 2, {(1, 1) = 6436781.609, (1, 2) = 0, (2, 1) = 0, (2, 2) = 3862068.966})
writeto(Bexport);
for i to 2 do
for j to 2 do
printf("%016.15E\n", B[i, j])
end do:
end do:
writeto(terminal)
This is a translation of the (B) part only:
matrix = {{6436781.609, 0}, {0, 3862068.966}}
Export["Bexport", Map[FortranForm, N#Flatten[matrix]], "Table"]
Please test it and let me know if it works for you.
Differences compared to the Maple version: the E is written as lowercase and the number of digits that are output is not fixed (but, as you can see, all significant digits are preserved). Will these differences cause problems in your application?
I believe this does what you want for matrix B:
b = {{6436781.609, 0}, {0, 3862068.966}}
bformatted =
NumberForm[
Flatten#b,
{16, 15},
NumberFormat -> (Row[{#, "E+", StringTake["00" <> #3, -2]}] &)
];
bstring =
StringReplace[
ToString#bformatted,
{"{"|"}"|" " -> "", "," -> "\n"}
];
WriteString["Bexport.dat", bstring, "\n"]
Close["Bexport.dat"]

Resources