How to make R base plots larger inside boundary box? - r

I am creating two plots in R, arranged with one over another. The issue is that the plot is very small within the boundary box, and I don't know how to "zoom in" essentially. Changing the margins only affects the size of the boundary box, not the little plot on the inside. Any help would be appreciated as I'm very new to this!
par(mfrow=c(2,1), mar=c(1,1,1,1))
plot(G_nwr,axes=FALSE,main="Non-weighted")
plot(boundary,add=TRUE)
points(G$utm_x,G$utm_y,pch=16,cex=0.2)
plot(roads_clip,add=TRUE,col="red",lwd=0.5)
plot(buildings_clip,add=TRUE,col="gray80")
plot(creeks_clip,add=TRUE,col="lightskyblue",lwd=2)
plot(railways_clip,add=TRUE,col="purple1",lwd=2)
plot(water_clip,add=TRUE,col="steelblue1")
legend("topright",inset=0.05, cex=0.8,
c("Canadian Goose","Road","Building","Creek","Railway","Water Body"),pch=c(16,NA,15,NA,15,NA),lty=c(NA,1,NA,1,NA,1),col=c("black","red","gray80","lightskyblue","purple1","steelblue1"))

Related

XMGrace: How to plot independent short lines with labels alongside my data curves?

I need to plot some potential energy curves. I.e. I need to be able to create graph looking like this:
Using the tutorial I'm able to recreate curves, create the legend etc. What I'm not able to do are the dissociation limits (labels on the right side of the picture in the red rectangle). Is there any way to make such "independent labels" at certain points on y-axis or do I have to create them like many little plots?
The easiest (albeit time-consuming) way is to use the "Window -> Drawing objects..." facilities and draw everything yourself by hand. Labels can be added with the "Text" button. Note that your life can be a made a bit easier by selecting "Position in: World coordinates" instead of "Viewport coordinates" and adding the real x and y value of each label.

How to move the legend to outside the plotting area in Plots.jl (GR)?

I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)

Save Filled Area of Polygon in R

I am plotting polygon in R and saving it.Problem, I am facing is that the whole plot is saved as png file but I want to save only the filled area in the polygon.
Is there a way for that ?
x<-c(0.000000000,0.010986328,0.006351471,-0.004634857)
y<-c(0.000000000,0.007232612,0.012841203,0.006199415)
file_name = paste("~/Downloads/Plot", ".png", sep="")
png(file_name,width=1280,height = 720,units="px",res=200)
plot(x,y,axes=FALSE,ylab='',xlab='')+polygon(x,y,col="#FF0000FF")
dev.off()
If you're drawing a monofigure plot (which is the default), then I believe there are three possible sources of spacing that can cause a plot element to not extend to the edges of the graphics device:
1: data coordinate limits that are larger than the extent of the plot element.
2: "internal spacing", which is best thought of as an expansion of the plot area that sits inside the margins.
3: margins. This is normally where axes, ticks, tick labels, axis labels, titles, and sometimes legends are drawn.
All of these sources of spacing can be eliminated with the following customizations:
1: set the xlim and ylim graphics parameters to perfectly fit the target plot element.
2: set xaxs='i',yaxs='i', which can be done with either a preemptive par() call or on the initial plot() call.
3: zero the margins with mar=c(0,0,0,0). This must be done with par() prior to the initial plot() call.
Example:
## generate data
pts <- data.frame(x=c(0.2,0.4,0.9,0.7),y=c(0.5,0.4,0.5,0.6));
## precompute plot parameters
xlim <- range(pts$x);
ylim <- range(pts$y);
## draw plot
par(mar=c(0,0,0,0));
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F);
points(pts$x,pts$y,pch=21L);
polygon(pts$x,pts$y,col='red',pch=21L);
Multifigure plots can incur one additional source of spacing, namely outer margins, but it looks like that doesn't concern you for this problem. In any case, I'm pretty sure outer margins always default to zero anyway.
See par() for the relevant documentation.
It looks like I misunderstood the question. What you want is a transparent background, which is different from simply fitting the image size to the plot element.
You can use the png() function to set the background to be transparent by passing bg='transparent', as explained on the documentation page.
For example, here's my fitted image saved with a transparent background:
Note that not all image viewers will correctly detect and/or clearly depict the transparency of the background. I would highly recommend GIMP, which is basically a free Photoshop knockoff, albeit markedly lighter in features. GIMP depicts transparent regions as a kind of checkerboard of grey squares, which looks like this:

quantmod barChart (or chartSeries) formatting options

I have just started playing with the quantmod package. The documentation is however, quite sparse (perhaps understandably, since it is OSS).
I am currently using barChart() which is a nice wrapper around chartSeries() and does most of what I want, but the default chart it produces are not quite what I want. To be specific, I want to tweak the charts produced by barChart() to suit my needs - however, since I am a newbie, I don't know whether my "tweaks" can be provided as options to the wrapper barChart(), or if I need to call chartSeries() directly, with specific arguments.
I have been tearing my hair out trying to do the following:
replace the horrible {start date}/{end date} text in the top right hand of the chart produced by barChart() with text of my own choosing
specify the formating to be used on the X axis (for example, show only the last two digits of the century. i.e. '98, '99, '00, '01 etc)
'Force' both top chart and the bottom chart to have their Y values printed on the left hand side of the chart
Add an aditional series to the bottom chart
Use different up/down colors for the bottom chart (defaults the using the same up/down colors for both top and bottom charts)
Plot just the top chart (no bottom chart)
Specify X axis, Y axis grid line spacings for top chart, for bottom chart
Write the image to an alternative output (e.g. png image or pdf document) instead of the graphics device
Can anyone help with any (or all) of the above?.
This functionality isn't available (patches welcome).
This functionality isn't available (patches welcome).
This functionality isn't available (patches welcome).
See the sparse documentation for ?addTA, specifically the on argument.
Plot the bottom chart as two separate up/down series, using two different colors, or perhaps chartTheme.
Not sure what you mean; just don't plot the bottom chart...
See the sparse documentation for the major.ticks argument to chartSeries. I don't think you can change the y axis grid line spacings, and the x axis spacing will be the same for the top and bottom chart.
See ?png and ?pdf.
To change or remove the bottom chart,
check the TA argument of chartSeries function
(there is an example in the manual);
to change the colours,
check the theme argument
(there is an example in the manual);
to write to a png or pdf file,
use the png or pdf functions,
as with other plotting functions.
To fine-tune the axes and labels, it is probably easier to bypass
chartSeries altogether and plot the data yourself, with base graphics,
lattice or ggplot2.

How to center an R plot after removing axis labels

I'm working on visualizing a matrix in R (almost exactly like http://reference.wolfram.com/mathematica/ref/MatrixPlot.html), and I've been using
image(<matrix>,axes=FALSE)
to draw the picture. However, I noticed that with the y-axis turned off, the plot isn't centered--the space is still there for the axis ticks + label. I can finagle some centering with
par(oma=c(0,0,0,2.5))
but this seems inefficient and error-prone (if my matrix were to change dimensions/become non-square). Is there a better way to force the graphic to center?
Reference image http://img694.imageshack.us/img694/9891/metropolis.png
The right hand margin is significantly smaller than the left.
Does
par(mar=c(5,2,4,2))+0.1
help?

Resources