How to increase height of image in R? - r

I am using heatmap package and plotting heatmap from 400 rows and 10 column so plot looks very dense. Is there any way to plot png or jpeg image (as need to include in website) where length is 20 times than width. I tried to use
png("heatmap.png",width=10,height=200)
heatmap(some_data);
dev.off();
But height of png image gets changed not the plot size. how can i change plot size?

Related

aspect ration of ggplot2 plot [duplicate]

This question already has answers here:
Save plot with a given aspect ratio
(5 answers)
Closed 6 years ago.
I used coord_fixed to change the aspect ratio of my plot. Unfortunately, as you can see below, the size of the window of the plot is still square, resulting in large amounts of white space above and below my plot. How do I change the size of the window to, say "wrap the content" that's inside of it?
Ok, with the help of Roland, I've figured it out. ggplot2 plots are always displayed in a device and that's where the height has to be set. For instance, I'm on linux and if I want to display a plot on screen, it gets done with x11(). In the R console, simply doing
> x11(width = 7, height = 3.5)
will result in a window with a dimension of 7 inches by 3.5 inches. Subsequent calls to ggplot2 will automatically use this window. The standard size is 7 inches by 7 inches and in my case resulted in the large areas of white space above and below the plot, which of course are smaller when the window has a height of 3.5 inches.
The next step, was getting the height right when rmarkdown uses the png device. This is actually a knitr issue and can be specified in the chunk options of the code block where the plot gets generated.
Here's the relevant code for my case
```{R, fig.width = 7, fig.height = 3.5}
# ggplot2 code goes here
```

How can I set the text size relative to the plot dimension in ggplot2

In ggplot 2, I can set the size of all texts using theme_grey(base_size=16). This font size looks fine if I export the plot in a 400*400 PNG. However, it looks too small if I export the plot in a 800*800 PNG. How can I set the text size relative to the plot dimension?

Adjusting size of plot in R

I have a distribution plot that I want like this:
dev.new(width=5,height=2)
plot(w.count.v, main="Distribution Plot of 'she' in Two Kinds",xlab="Novel Time",ylab=m=c(0,1),yaxt="n")
How do I save it as a .png as the size on the device? When I save it, this is the resulting image.

ggplot2 grid.export plot size SVG

How can I specify the width and height of the final SVG when I do:
print(plot)
saveXML(grid.export()$svg)
I'm producing plots for a web application in SVG. I was using ggplot2 and saving the plot using ggsave which allowed me to specify size in inches for width and height. I worked great. I now needed to perform annotations on the SVG and it seemed that ggsave did not work, so I'm forced to use the grid.export approach presented above. The only aspect I seem to have control is the resolution, which allows me to make plots smaller. However, while ggsave made all the text and point size scale to a bigger size, the grid.export approach changing the resolution can make the plot smaller, but just by makes everything smaller so the plot reads worse.
For instance, setting width and height to 3
ggsave version:
grid.export version:
What I managed to do is define a new base_size on the theme to compensate the resolution scaling:
base.size = 10 * (7/size)
resolution = 72 * (size/7)
if (length(plot$theme) == 0) plot <- plot + theme_gdocs();
plot$theme$text$size = base.size
mysvg <- grid.export(res=resolution)

Saving ggplot as SVG ruins quality

I have a plot generated from the following code:
bars <- ggplot(plottingFrame, aes(x=X, y=as.factor(Y))) +
geom_raster(aes(fill=colour)) + scale_fill_identity())
Where plottingFrame is a load of colour values precomputed. geom_raster is used with scale_fill_identity to make a "heatmap" of different colours:
Ignore the squashed axis and cutoff titles, I made the image smaller so it's not too big.
The above image is what happens when ggsave is used on the bars object and it is saved as a png.
When I try to use ggsave to save as svg however, it blurs the colours between the three coloured rows.
Why does saving a geom_raster based plot blur the three rows - Seq1, Seq2, and Seq3, when png keeps them distinct as in the above image? And how can I stop saving to sag blurring them?
Thanks,
Ben W.

Resources