ggplot2 and TikzDevice - Removing White Space - r

I am using tikzDevice package to obtain Latex-friendly graphs in R. I am having trouble in removing excessive white spaces from top and bottom of the graph below:
I have tried using par(mar) but it does not seem to work with ggplot2. Also, theme(plot.margins) seems to unresponsive as well. The white space is introduced as I try to alter the aspect ratio of the figure with theme(aspect.ratio).
Any suggestions?
Thanks!
EDIT: Here is a MWE:
library(tikzDevice)
library(reshape2)
x = seq(0,1,0.1)
y1 = x^2+2*x+7
y2= x^+2*x+2
df = data.frame(x,y1,y2)
df <- melt(df, id.vars=c("x"))
names(df) <- c("x","$latex~Name$","value")
plot <- ggplot(df,aes(x=x,y=value,color=`$latex~Name$`,group=`$latex~Name$`)) + geom_line() +
theme(aspect.ratio = 0.4)
plot
tikzDevice(file="mweTex.tex")
plot
dev.off()

The problem seems to be caused by some tikz statements that affect the bounding box of the image. Since there doesn't seem to be any option in tikzDevice for minimizing whitespace, I had to think of something else. I managed to fix the generated tikz file by adding the following R code at the end of your MWE:
# remove all lines that invisibly mess up the bounding box
lines <- readLines(con="mweTex.tex")
lines <- lines[-which(grepl("\\path\\[clip\\]*", lines,perl=F))]
lines <- lines[-which(grepl("\\path\\[use as bounding box*", lines,perl=F))]
writeLines(lines,con="mweTex.tex")
I've tested it on your MWE:
The left image is without the fix, the right image is with the fix.

Related

R patchwork package trouble displaying many plots

I am enjoying the patchwork package quite a bit and I am generally happy with even its default formatting.
However, I cannot figure out how to get a reasonable result when trying to display many plots. The output is unreadable (plots shrunk down and dominated by text) and the saved output is completely blank.
Input:
require(ggplot2)
require(patchwork)
plots <- list()
testfunction <- function(x) {
plot_placeholder <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle(paste("p", x, sep = ""))
plots[[length(plots) + 1]] <<- plot_placeholder
}
mapply(testfunction, c(1:66), SIMPLIFY = FALSE)
patchwork::wrap_plots(plots)
ggsave("test.pdf")
Output:
Is there a way within patchwork to make the page larger or wrap plots across multiple pages of a PDF? Or is there a different package that may help with this?
Update:
Maurits Evers pointed out that this is an issue with my arguments in ggsave(), not patchwork. With these changes the plots look much better in the .pdf output, but were still being truncated at the bottom of page 1. From this post, found the last bit of change needed to generate desired results
Input:
# ...
pdf()
patchwork::wrap_plots(plots)
ggsave("test.pdf", width = 20, height = 20)
dev.off()
Output:

How to remove background of images with the magick package?

Is it possible to remove the background of images with the magick package?
I know how to use edge detection with Gimp/Inkscape to crop out silhouettes; however, I'm looking to automate the process for a large batch of images with R.
My ultimate goal is to use the ggimage package to plot these images as x,y coordinates but the background of these images is currently overlapping the plot (the dog compared to fink)
library("ggplot2")
library("ggimage")
set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
y = rnorm(10),
image = sample(c("http://www.supercoloring.com/sites/default/files/silhouettes/2015/05/cairn-terrier-black-silhouette.svg", "https://jeroenooms.github.io/images/frink.png"),
size=10, replace = TRUE)
)
ggplot(d, aes(x, y)) + geom_image(aes(image=image))
One can trim the edges of an image using image magick's image_trim()
img <- image_read_svg("http://www.supercoloring.com/sites/default/files/silhouettes/2015/05/cairn-terrier-black-silhouette.svg")
image_trim(img)
but this isn't exactly what I would like.
Any ideas?
The image_transparent() function does this, e.g.:
logo <- image_read("logo:")
image_transparent(logo, 'white')
The white parts of the image will be made transparent, which should be enough for the simple image in your example.
see my answer in https://yulab-smu.github.io/treedata-book/image-processing-using-magick-package.html#example-1-remove-background-of-images.
You can pass image processing function provided by magick package to geom_image via the image_fun parameter.

R figure being cropped

Here's a bit of a basic question, you'd think R would automate this always. I'm making a heatmap (basic stuff, png() into heatmap.2() into dev.off()), and my row labels get cleaved off on the right hand side, along with a tiny part of the dendrogram on the left:
Fiddling with the margins manually would eventually lead to a sensible output, but the problem is that this is part of an automated pipeline with biologist users in mind. Surely there's some automated way to do this, right? I mean, in RGui on OSX, a quick reshape of the figure window fixes the layout automatically, so it has to be possible. Thanks, and sorry to bother you.
EDIT: Procedure to reproduce this visualisation:
Input data:
Time,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48
CATMA1A23000,0.21129,0.48322,0.54234,0.93714,0.51816,0.11103,0.38304,0.97183,0.61381,-0.13775,0.32575,0.85221,0.1921,1.8692,0.4117,0.51977,0.74019,0.98932,0.023536,0.8849,0.75141,0.42846,0.03839,0.50295,0.38849,-0.95087,0.55017,-0.14813,0.79036,0.40978,-0.33048,0.81173,0.60585,0.014413,0.86586,0.063947,1.1195,0.25425,-0.090325,1.3035,0.90609,0.62866,0.83168,0.88655,0.4843,0.60098,0.26669,0.66605,0.78468,-0.76709,0.29615,-0.8911,0.5022,-0.93834,-0.065904,0.58003,0.41217,-0.99012,-0.032054,0.69119,1.1791,-0.0059458,0.81002,0.76421,1.302,0.77637,0.7824,1.192,-0.27005,-0.24698,1.8706,0.33513,0.38592,1.1987,0.48569,0.7104,2.4307,0.68148,0.11407,0.65619,0.94468,1.245,0.44146,0.71048,0.96389,1.7647,0.22729,0.96875,-0.2738,1.2975,2.0503,0.52507,2.282,1.0773,0.44489,0.22504
CATMA1A30870,3.9909,5.1844,5.2522,5.5482,5.665,5.4666,5.4456,5.5456,5.0014,5.5408,3.9324,3.9288,4.3624,3.0828,5.4821,5.1446,4.5638,4.6449,5.2604,4.2607,4.0942,3.9892,5.1361,3.8625,3.9454,5.3148,5.188,6.088,5.2085,5.3073,5.5652,4.8358,4.6111,4.9911,3.1497,4.3796,4.2456,3.9712,5.0998,5.4383,5.0908,4.5155,4.6486,5.1233,4.4259,4.7218,3.7928,4.0384,4.1085,6.2787,5.4937,5.9637,5.4085,5.188,5.3057,5.1963,4.3807,4.8352,3.9584,3.5859,3.8535,3.9755,4.5199,5.1093,5.3526,4.1214,4.5787,3.4499,4.1962,4.6929,4.3379,4.6376,4.4774,5.2341,5.3061,5.6769,3.6244,5.1073,4.543,4.9929,4.9341,3.9829,4.4304,4.0021,3.7498,4.2972,5.3338,4.8094,4.5766,5.5171,3.1207,5.0926,3.0253,4.351,4.351,4.5253
CATMA1A65800,0.65615,0.54186,0.93488,0.2205,0.38134,0.95061,0.60157,0.66508,0.83971,1.3625,0.9037,1.1606,1.0143,0.91896,1.3587,1.2468,1.0147,0.44113,1.5196,0.60232,0.52456,0.82557,0.43697,1.3253,0.78614,0.73697,1.0593,0.010271,0.59042,0.58544,0.38307,1.0372,1.071,0.21915,1.1779,0.97341,0.95672,0.92057,1.3866,1.3884,0.99241,1.0546,1.0215,1.0144,1.1606,1.2421,0.89549,0.56747,0.99347,1.0706,1.3403,0.48719,0.30759,1.0215,0.76474,0.87313,0.47597,1.5028,1.2201,1.7923,0.64299,1.3612,1.0079,0.64145,1.0969,0.90254,0.9109,0.85213,0.55241,0.63797,1.5373,0.45586,0.59624,0.5717,0.13919,-0.012128,2.7367,0.83577,0.13422,0.13908,0.32377,0.013345,-0.017209,0.47044,1.034,1.4932,1.0067,0.57515,1.1405,0.95364,3.0214,0.14601,2.1645,1.1704,0.45205,0.83406
CATMA1C71184,4.2879,2.7256,3.306,2.2375,2.6575,4.6438,4.3899,3.8518,5.3494,3.8049,5.4095,4.5437,3.4362,4.0983,2.5059,2.7275,3.9432,3.5177,4.266,4.6446,4.9338,3.5188,3.7903,4.4867,3.9692,3.3382,3.2898,3.4103,3.3025,4.1279,4.3532,5.0625,5.756,4.7799,5.0303,4.8974,3.8698,2.887,2.7541,2.7837,3.0665,4.0168,3.7507,4.6273,5.2384,3.7049,4.5843,3.97,2.7142,3.6205,3.5394,3.3561,3.2672,4.4467,5.2044,5.2177,3.8496,5.7881,4.7594,5.0395,2.8281,2.8751,2.8322,2.6873,1.8326,3.042,3.834,4.7844,3.1947,3.2571,4.0826,4.8398,3.8147,2.7439,2.846,2.64,2.8055,3.421,3.8495,4.9367,4.6413,3.5281,4.0636,3.7144,3.4468,2.3466,1.7452,2.3151,4.022,2.9211,3.89,3.7213,4.1856,3.3613,4.0891,4.2135
CATMA1C71776,-2.0266,-0.77692,-1.3279,-0.40422,-0.38614,-0.34387,-0.44277,0.99775,1.0748,0.17831,-0.14209,-0.98905,-0.88619,0.40548,-0.37731,0.18112,-0.80473,-0.26957,0.49049,0.25413,0.30341,0.20203,-1.0343,-0.17877,-0.46107,-1.202,-0.58841,-1.0995,-1.1079,-0.10977,0.29428,0.33363,0.35537,0.55571,-0.11074,-0.69584,-0.052035,-0.18207,-0.45321,-0.2778,-0.81074,-0.27129,0.087758,0.52214,0.33791,0.51601,-0.30863,0.14386,-0.2922,-0.21013,-0.73268,-1.1732,-0.36599,0.4731,1.0357,0.62377,0.8219,0.6029,-0.27195,1.0677,-0.48423,-0.57601,-0.43393,-2.0519,-0.67433,-0.44234,0.0078619,0.40677,-0.47102,-0.59971,0.30058,-0.4604,-0.56523,-3.3913,-0.52856,-0.54137,1.0418,-0.26115,0.21246,0.37858,0.38716,-0.86708,-0.41297,-0.21536,-0.06004,-0.48526,-1.2651,-0.52592,0.23757,-0.73376,1.0272,0.59383,0.65704,-0.017658,-0.44128,-0.30226
CATMA2A41800,1.5057,1.8357,2.7851,2.9125,2.6421,2.3881,2.4772,2.3367,2.2988,3.0073,2.7659,2.8454,2.5939,2.6327,2.6184,2.3415,3.3473,2.7683,1.1123,1.9614,1.8795,2.2335,2.9227,3.1536,2.6255,2.5264,2.3874,3.3196,1.8572,2.3446,2.3358,1.683,2.1742,2.8082,2.8634,2.8607,2.6755,3.2567,2.0726,3.0746,2.8984,2.5235,2.9399,2.5731,1.9196,2.9845,2.965,2.8055,1.1878,-0.6181,2.9552,2.7814,3.0701,-0.42372,2.5897,2.6331,1.7218,-0.70788,2.9578,1.1506,3.3772,1.6571,3.3166,3.5001,2.6541,2.5367,2.8643,2.1034,2.6389,1.8915,2.4861,3.2271,1.7718,2.7359,2.391,2.7223,1.1594,2.4025,1.1524,2.1405,2.253,2.4368,2.8053,2.2955,2.2877,2.7792,3.4115,3.2464,-0.37629,3.0582,1.682,2.0598,1.8747,2.0158,2.4714,2.1816
CATMA2C47274,2.2048,1.6574,0.96502,0.15092,0.5251,0.40351,0.90106,-0.12415,0.48846,0.03794,0.86292,0.27027,0.71848,1.8535,0.47188,-0.067504,0.68578,0.28113,0.17559,0.29659,0.067618,0.79368,-0.36732,0.80945,2.4297,1.3272,1.3741,0.005024,0.57369,0.6195,0.17845,1.3991,0.71594,-0.76358,0.74421,0.078431,0.97596,0.14151,-0.77085,0.64926,-0.24602,1.3478,0.49879,0.90549,0.78024,0.94685,0.023589,0.07446,1.3597,0.19311,1.1883,-0.20756,0.59042,0.031659,0.3591,1.1757,0.97314,-1.0133,0.92409,1.2264,-0.0095851,-0.47315,1.3107,1.3967,0.80332,0.5494,1.0394,1.4519,0.50253,-0.65282,1.5689,0.17996,1.5938,0.28124,0.93511,0.10344,3.6906,0.56448,-0.40754,0.75218,0.10346,-0.37737,-0.53789,0.57109,0.29602,-0.64821,-0.52805,0.34899,0.50146,0.61456,2.1785,-0.41,2.135,0.7426,0.3307,0.64542
CATMA2C47710,3.9021,3.7272,3.709,3.494,3.7557,3.7883,3.8697,3.4952,3.6365,3.6948,4.0954,3.7629,4.1282,3.6108,4.2769,3.3689,3.5232,3.7488,3.6237,4.0805,3.855,3.8724,4.2706,3.5584,3.9576,4.1324,3.857,3.6508,3.7609,3.7885,3.6256,3.8022,3.7189,3.7417,3.6357,3.6448,4.2841,3.8731,3.6047,3.7792,3.5703,3.8284,3.8098,3.8824,4.2441,3.6127,3.8253,3.529,3.6078,4.0917,3.6794,3.7977,3.8231,3.9369,3.6087,3.7634,3.6419,4.0052,3.7083,4.0191,3.3211,3.9133,3.74,4.0801,4.0999,3.6312,3.7747,3.7768,3.7977,3.9409,3.9423,3.6708,4.1773,3.666,3.8267,3.8832,4.0462,3.8007,3.3932,4.0945,3.9901,4.1036,4.1064,3.8178,3.7936,3.8638,4.3395,3.5859,3.5698,3.7885,4.4263,4.0292,3.6857,4.1197,4.0262,3.7753
CATMA3A13690,5.2022,5.6481,5.7765,4.8253,6.0581,6.2026,5.2711,6.4481,6.0929,6.3395,5.3806,4.9156,5.3634,4.5556,5.3547,5.5214,5.6947,5.9289,5.6269,5.5406,6.0508,5.8498,5.6275,5.4422,5.3293,5.7543,5.1577,6.3626,5.429,5.7245,5.7554,5.4575,5.2244,6.2312,6.0425,5.0317,5.4494,5.5252,5.5124,5.6588,5.0015,5.7454,5.6711,6.132,5.4789,6.1668,5.6978,5.113,4.3572,5.6177,5.9557,6.0099,5.2522,5.5598,6.2427,5.4703,5.4275,5.9159,5.3029,4.1094,5.927,5.9223,5.1257,5.3835,5.6579,5.5849,5.8674,4.9425,5.2144,5.5382,4.7165,5.5688,5.2766,6.4298,5.9438,5.7105,4.757,5.9097,5.3665,5.8336,5.8393,5.6104,5.4702,5.4028,5.0756,5.632,5.9963,5.7889,5.7521,5.5561,4.0685,6.3026,4.9628,6.176,5.7256,4.8529
CATMA3A15560,0.23073,0.32331,0.37953,0.081038,-0.33446,0.18495,0.86901,0.71363,0.33531,1.4741,0.32824,0.18869,-0.34772,0.97628,0.62455,-0.14645,0.42603,-0.00031328,0.92441,0.14199,0.25522,0.55583,0.10067,1.2818,0.64424,-0.24454,0.33039,0.25975,1.1023,-0.5464,0.62174,-0.15397,0.78478,0.2999,-0.063974,0.55156,0.44076,0.17798,0.61707,0.9308,0.19809,1.2378,0.26328,0.19767,0.069484,0.76748,0.45386,0.4495,0.050625,0.99311,-0.10988,-0.070507,0.46274,0.19475,0.36696,-0.16537,1.0566,0.94446,0.85272,1.194,-0.41335,0.17202,0.38729,0.65495,-0.32063,0.60186,0.24444,1.3128,0.48551,0.55998,1.8149,0.36494,0.10693,-0.19085,0.25082,-0.10628,-0.38432,0.50117,0.97923,-0.31536,-0.2634,-0.82767,0.70017,-0.0093473,0.27653,0.67773,0.40978,0.3315,0.5179,0.32044,2.9356,-0.51529,2.7438,0.4153,0.32022,0.74311
CATMA3A21390,-0.54603,-0.32262,-0.29966,-0.22463,-0.49654,-1.0552,-1.0705,-0.93928,-0.71247,-2.7228,-0.67849,-0.24868,-1.5015,-0.22486,-0.26215,-0.34298,-0.73168,-0.33699,-0.3455,-0.5349,-0.68784,-0.47506,-1.2541,0.86942,-0.14503,-2.0603,-0.35647,-0.67731,0.20243,-1.1881,-1.3828,-1.0471,-0.20157,-2.5492,-0.7862,-0.21715,-0.20583,-0.067758,-0.86589,-0.75509,-0.62777,-0.57176,-2.0996,-0.72894,-0.56391,-1.1054,-0.94172,-0.98805,-0.20763,-0.42764,-2.0142,-1.4079,-0.66326,-0.10354,-1.6284,-1.9413,-0.77001,-0.58492,-0.99846,-0.013285,-0.31205,-0.10665,-1.2396,-1.7556,-0.40454,-0.26631,-1.2694,-0.32263,-1.2293,-0.93683,0.97994,-1.8696,-0.26859,-0.25661,-0.33193,-0.83889,-0.25944,-0.63826,0.3387,-1.4443,-0.93123,-0.84915,-0.24606,-0.63109,-0.78621,-0.40802,-0.6463,-0.84319,-1.0379,-1.1471,1.9932,-1.3029,1.5286,-0.19754,0.018446,-0.55479
CATMA3A53880,3.917,3.6689,3.7213,3.8812,3.8253,4.4861,4.258,4.7926,4.9737,4.0869,3.2183,3.5812,3.408,3.3128,3.3915,4.1923,3.9122,4.0586,4.2707,4.3077,4.8926,4.1982,3.4817,3.5267,3.6952,3.6632,3.9304,4.0793,3.9499,4.2175,4.8303,4.9599,4.6336,3.8998,3.7932,3.8572,3.2167,3.7749,4.3971,4.0662,3.9241,4.6407,4.2837,4.9644,4.972,4.2565,3.8796,3.383,3.2382,4.1017,4.058,4.0423,3.6033,4.0774,5.141,4.9736,4.4372,3.8969,3.7386,3.3618,3.539,4.2588,3.4726,3.7424,3.5818,4.9159,4.3567,4.1291,4.1108,4.4035,3.1374,3.4675,3.762,3.9264,3.7319,4.1205,2.0915,4.4192,4.5049,4.5427,4.6205,3.4653,3.6017,3.3376,3.266,3.5561,3.4538,3.761,4.2013,4.1787,4.017,4.7773,3.748,4.4977,3.8685,3.7464
CATMA5A09500,6.2838,5.1076,6.6209,5.7153,5.9395,5.3601,4.2366,5.189,3.3074,4.8701,5.0357,5.0951,6.2843,5.691,5.5054,5.9949,5.0834,5.1484,4.5884,4.805,4.458,5.0816,4.7264,5.3661,5.0645,5.8293,5.2877,5.6602,5.3336,4.7329,4.843,5.1901,3.8431,4.683,5.7106,4.0774,6.5689,5.8061,5.3445,5.2905,3.837,4.6271,4.2277,4.6165,4.0081,4.5176,4.259,4.6363,4.8557,3.9828,5.9728,5.4698,5.4102,4.5802,5.0247,4.8967,4.0116,2.6636,5.4684,4.3454,6.6893,6.5624,6.1464,5.4444,5.3309,5.6682,5.5012,4.3162,2.9028,3.3906,4.6684,4.914,5.3601,5.5138,6.2191,5.8043,5.8165,5.1155,3.8681,4.9268,4.0232,6.1699,4.6327,5.7364,6.1934,6.7188,5.0755,5.7058,5.3703,4.3921,3.2108,4.9503,4.1003,4.8225,4.6701,4.7166
CATMA5A12680,1.7891,1.362,1.4303,0.61973,0.54124,1.2733,0.7775,0.66788,1.7338,0.63125,1.5852,0.91616,1.0712,0.81919,0.90152,0.77799,1.0835,0.32735,2.0868,0.5895,1.1445,0.64545,0.71776,0.33929,1.42,1.0729,1.4879,0.86379,0.95453,1.2928,0.97753,1.1738,0.79397,0.31563,0.86345,0.67595,1.3501,1.079,1.2034,1.2432,1.3696,1.1473,0.72528,0.57199,0.85965,0.65339,0.47614,0.43219,1.363,2.4026,0.98947,0.85204,1.2591,2.3151,1.6775,1.6555,1.0371,2.5586,-0.097071,0.91744,1.068,1.6819,1.0122,0.99911,0.75267,1.2195,0.42175,0.2554,0.4819,0.80025,0.44517,0.32442,1.9863,1.5019,1.2893,1.0669,1.6881,1.7581,1.6505,1.943,2.141,1.018,0.68207,0.84392,1.1452,1.009,1.1376,1.0572,1.9258,1.7039,0.10261,1.4549,0.94693,0.77945,0.049658,0.84178
CATMA5A14990,0.41247,0.51378,0.375,-0.24526,0.027658,-0.015345,-0.028278,-0.2643,0.3905,0.12299,0.94334,0.11482,0.50109,-0.55162,0.10768,-0.26433,-0.31064,-0.76526,1.0147,-0.73178,0.3323,-0.13657,-0.14013,-0.17878,0.31259,0.36723,0.55499,-0.2664,0.31862,0.60225,0.078919,0.18282,0.23014,-0.27835,0.16392,0.58131,1.052,0.062394,0.064929,-0.11918,-0.2167,-0.26018,-0.21649,-0.032523,-0.05763,-0.62775,-0.29148,0.13633,0.72715,1.535,0.08501,0.76137,0.54934,1.5163,-0.26612,0.58127,0.30339,1.8464,-0.17161,0.30309,-0.070279,0.28016,-0.036628,0.67464,0.34753,0.26499,-0.17856,-0.61162,-0.17941,-0.01017,0.21112,0.026389,0.64566,-0.39685,0.31403,-0.21637,1.1253,0.41434,0.77759,0.20421,0.70657,0.53273,-0.67276,-0.65656,0.6997,-0.06428,-0.13546,-0.15055,0.99804,1.4673,-0.47155,-0.056436,0.84609,-0.23068,-0.55114,0.68975
CATMA5A25890,-0.23072,0.82252,0.32512,0.13284,0.14406,0.005071,0.40953,-0.11252,-0.6367,-0.048073,0.21325,-0.046316,-0.35078,1.1023,-0.019882,-0.55825,-0.34621,0.1114,0.030302,-0.08142,0.19157,0.5152,-0.034952,0.20984,1.0547,0.4816,0.31078,0.14731,0.31811,-0.27275,-0.47217,-0.043207,0.29109,0.41192,-1.069,0.23357,-0.17453,0.015661,-0.26869,0.14587,0.19336,0.5926,-0.1583,0.61242,0.18635,0.01207,-0.21772,0.13986,1.9305,0.3349,0.62954,-0.04455,1.121,-0.14155,-0.33772,-0.42633,0.41828,-0.29794,-0.7107,1.4786,-0.37442,-0.45371,0.16175,-0.057764,-0.11803,-0.037838,0.25868,0.88605,0.33225,-0.24331,0.93084,-0.33266,0.9762,0.24755,0.30857,0.14661,1.8351,0.40313,0.45084,-0.52105,-0.31271,-0.37122,-0.28615,-0.0023692,-0.43072,-0.24417,-0.5184,0.13874,-0.32606,-0.1705,2.1396,-0.77657,2.3056,0.32096,0.29337,-0.034746
CATMA5A31940,4.4307,4.8735,4.7911,5.8201,5.9587,5.576,5.9084,5.849,5.2832,5.5423,5.0275,4.5254,4.5054,3.0696,5.6512,4.3986,4.9856,4.9222,4.951,4.5506,4.2185,4.3184,5.4783,4.4515,3.9397,5.6269,5.0988,6.1254,5.7446,5.6146,5.452,5.2047,5.3527,5.93,4.8931,5.5529,3.9876,4.1072,4.0583,5.4324,5.4681,4.392,4.5189,5.0629,4.9347,5.2165,4.6681,4.7428,4.1539,6.3376,5.353,6.2896,5.5844,5.1654,5.4391,5.3933,5.261,5.6974,4.5142,3.9851,3.569,4.0946,4.6662,5.1085,5.0319,4.4598,4.8672,3.7326,5.3032,5.0448,4.7771,4.2209,4.1886,4.284,5.4681,5.8331,4.7683,5.6614,5.3313,5.2017,5.1002,5.0642,4.4867,4.8576,4.1454,4.0971,5.0368,4.845,4.753,5.5496,2.9838,5.1394,3.342,4.1246,5.0121,4.618
Code proper (BHC is from Bioconductor):
library(argparse)
library(RColorBrewer)
library(BHC)
library(parallel)
library(gplots)
#data prep and clustering, to get dendrograms etc as shown in plot
data = read.csv('input.csv',header=TRUE,row.names=1,check.names=FALSE)
genes = rownames(data)
samples = colnames(data)
data = data.matrix(data)
standardisedData = (data-mean(data))/sd(data)
samples = as.numeric(samples)
hc = bhc(standardisedData, genes, timePoints=samples, dataType='time-course', verbose=TRUE)
#the plotting proper
png('heatmap.png',width=6,height=6,units='in',res=300)
heatmap.2(standardisedData, Colv=NA, Rowv=hc, tracecol=NA, scale="none", col=brewer.pal(11,'RdBu'))
#heatmap.2(standardisedData, Colv=NA, Rowv=hc, tracecol=NA, scale="none", col=brewer.pal(11,'RdBu'), margins=c(3,10)) <- this fixes it on a single-case basis
dev.off()
I would suggest just giving it a fairly wide default margin, such that it should be clipped even with longer labels, e.g.:
heatmap.2(standardisedData, Colv=NA, Rowv=hc, tracecol=NA, scale="none", col=brewer.pal(11,'RdBu'), margins=c(14, 14))
The default margin size is (5,5). The above increases the both the column and row margins, but you can increase just one or the other as well.
If you want to be more precise, you could try using max(strwidth(rownames(standardisedData))) to determine the amount of space needed by the largest label, and then convert that into the units expected by par(mar=*) (what heatmap.2() uses to specify plot margins.)
I think this should do the trick:
# determine margins to use
png('/tmp/trash', width=6,height=6,units='in',res=300)
plot.new()
margin_width = max(strwidth(rownames(standardisedData), units='inches')) * par('fin')[1]
dev.off()
#the plotting proper
png('heatmap.png',width=6,height=6,units='in',res=300)
heatmap.2(standardisedData, Colv=NA, Rowv=hc, tracecol=NA, scale="none", col=brewer.pal(11,'RdBu'), margins=c(5, margin_width))
dev.off()

r - Missing object when ggsave output as .svg

I'm attempting to step through a dataset and create a histogram and summary table for each factor and save the output as a .svg . The histogram is created using ggplot2 and the summary table using summary().
I have successfully used the code below to save the output to a single .pdf with each page containing the relevant histogram/table. However, when I attempt to save each histogram/table combo into a set of .svg images using ggsave only the ggplot histogram is showing up in the .svg. The table is just white space.
I've tried using dev.copy Cairo and svg but all end up with the same result: Histogram renders, but table does not. If I save the image as a .png the table shows up.
I'm using the iris data as a reproducible dataset. I'm not using R-Studio which I saw was causing some "empty plot" grief for others.
#packages used
library(ggplot2)
library(gridExtra)
library(gtable)
library(Cairo)
#Create iris histogram plot
iris.hp<-ggplot(data=iris, aes(x=Sepal.Length)) +
geom_histogram(binwidth =.25,origin=-0.125,
right = TRUE,col="white", fill="steelblue4",alpha=1) +
labs(title = "Iris Sepal Length")+
labs(x="Sepal Length", y="Count")
iris.list<-by(data = iris, INDICES = iris$Species, simplify = TRUE,FUN = function(x)
{iris.hp %+% x + ggtitle(unique(x$Species))})
#Generate list of data to create summary statistics table
sum.str<-aggregate(Sepal.Length~Species,iris,summary)
spec<-sum.str[,1]
spec.stats<-sum.str[,2]
sum.data<-data.frame(spec,spec.stats)
sum.table<-tableGrob(sum.data)
colnames(sum.data) <-c("species","sep.len.min","sep.len.1stQ","sep.len.med",
"sep.len.mean","sep. len.3rdQ","sep.len.max")
table.list<-by(data = sum.data, INDICES = sum.data$"species", simplify = TRUE,
FUN = function(x) {tableGrob(x)})
#Combined histogram and summary table across multiple plots
multi.plots<-marrangeGrob(grobs=(c(rbind(iris.list,table.list))),
nrow=2, ncol=1, top = quote(paste(iris$labels$Species,'\nPage', g, 'of',pages)))
#bypass the class check per #baptiste
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
#
for(i in 1:3){
multi.plots<-marrangeGrob(grobs=(c(rbind(iris.list[i],table.list[i]))),
nrow=2, ncol=1,heights=c(1.65,.35),
top = quote(paste(iris$labels$Species,'\nPage', g, 'of',pages)))
prefix<-unique(iris$Species)
prefix<-prefix[i]
filename<-paste(prefix,".svg",sep="")
ggsave(filename,multi.plots)
#dev.off()
}
Edit removed theme tt3 that #rawr referenced. It was accidentally left in example code. It was not causing the problem, just in case anyone was curious.
Edit: Removing previous answer regarding it working under 32bit install and not x64 install because that was not the problem. Still unsure what was causing the issue, but it is working now. Leaving the info about grid.export as it may be a useful alternative for someone else.
Below is the loop for saving the .svg's using grid.export(), although I was having some text formatting issues with this (different dataset).
for(i in 1:3){
multi.plots<-marrangeGrob(grobs=(c(rbind(iris.list[i],table.list[i]))),
nrow=2, ncol=1,heights=c(1.65,.35), top =quote(paste(iris$labels$Species,'\nPage', g,
'of',pages)))
prefix<-unique(iris$Species)
prefix<-prefix[i]
filename<-paste(prefix,".svg",sep="")
grid.draw(multi.plots)
grid.export(filename)
grid.newpage()
}
EDIT: As for using arrangeGrob per #baptiste's comment. Below is the updated code. I was incorrectly using the single brackets [] for the returned by list, so I switched to the correct double brackets [[]] and used grid.draw to on the ggsave call.
for(i in 1:3){
prefix<-unique(iris$Species)
prefix<-prefix[i]
multi.plots<-grid.arrange(arrangeGrob(iris.list[[i]],table.list[[i]],
nrow=2,ncol=1,top = quote(paste(iris$labels$Species))))
filename<-paste(prefix,".svg",sep="")
ggsave(filename,grid.draw(multi.plots))
}

small plot with no margins - border with line width (lwd) equal to 1 not visible

I've been trying to make some very tiny line graphs using base plotting functions, but am coming unstuck when trying to add a thin border.
This is via RGui on Windows 7, saving a png from the plot window.
Here's my code:
dev.new(width=1.3,height=0.3)
par(mar=c(0,0,0,0))
set.seed(13)
x <- 1:10
y <- runif(10)
plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1))
polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
lines(x,y,lwd=1)
Everything is fine until I try to add a box with a line width of 1, giving:
box(lwd=1)
Now I can solve this by increasing the line width to 2, but this seems a bit of a hack.
box(lwd=2)
Using rect like rect(1,0,10,1) doesn't seem to give me an appropriate solution either, with the bottom and right borders not being visible.
Have you considered giving mar a small non-zero value:
dev.new(width=0.3,height=0.3)
par(mar=c(0.01,0.01,0.01,0.01))
set.seed(13)
x <- 1:10
y <- runif(10)
plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1))
polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
lines(x,y,lwd=1)
box(lwd=1)
I admit I haven't quite figured out what the end-game might be, but when I do an interactive "stretch" of that very small screen-object, it does result in an all-around border.
I do recognize that I am on a Mac and saving this to a pdf file and converting it to a png file for SO-inclusion may not be precisely reproducible on a Linux or Windows device.
Another solution base in grid and gridBase package. The idea is to replace the box by grid.rect.
Use gridBase to get the base viewport
Introduce some offset (viewport y ) to show the bottom line
Reduce the width of the viewport to show the right line.
Here my code:
library(gridBase)
sp <- baseViewports()
vp <- sp$plot
vp$width <- unit(0.999,'npc')
vp$y <- unit(0.001,'npc')
pushViewport(vp)
grid.rect(gp=gpar(fill=NA))
upViewport(1)
EDIT thanks to #baptiste, you can simply get the same result using only grid.rect:
library(grid)
grid.rect(width = unit(0.999,'npc'),
y = unit(0.5001, "npc"),
gp=gpar(fill=NA))
To answer my own question thanks to #baptiste's tip-off, this is a device dependent issue due to RGui. If the image is saved directly out to file using png everything works as intended. E.g.:
dev.new(width=1.3,height=0.3)
# repeat from here onwards only for png call below
par(mar=c(0,0,0,0))
set.seed(13)
x <- 1:10
y <- runif(10)
plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1),bty="n")
polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
lines(x,y,lwd=1)
box(lwd=1)
Saving out to png from the "R Graphics" window gives my original stuffed up image:
Going directly to file using png like:
png("textbox_direct.png",width=116,height=27)
# take code block from above
dev.off()
...gives the correct result:

Resources