Tick marks inside with image function with R - r

I am trying to have the tick marks inside my plot with R image function.
I red a fits image and I represented it into a PDF file using the "image" function in R. The result is fine, but the tick marks are outside the plot and I want them pointing inside.
image(1:dim(imn2223J)[1], 1:dim(imn2223J)[2], imn2223J, zlim=c(-8,15), asp=1, col=rev(rainbow(100)), xlab= "X - Arcsec", ylab="Y - Arcsec",cex.lab=2.5, cex.axis=2.5)
I have succeeded to have other tick marks inside, but the original ones from image are still there pointing outside. Someone encountered this prb ? Thanks.

Most base graphics functions like image() accept graphical parameters in the ... arguments. Those are documented in ?par. The one you want is tcl; positive values point into the plot. The default is tcl = -0.5, half a line of text pointing outwards. For the same length pointing inwards, use tcl = 0.5.

Related

Labelling X and Y axis in Dymola plot

I have drawn a plot using "plotArrays" function in Dymola. I would like to label X-axis in meters(Ideally, I need it to be flexible so that I can later change it to millimeters, micrometers, etc..). I want to label Y-axis similarly in volts which I have done already using
plotArrays(x_axis,phie,legend=names,units=fill("V",size(phie,2)));
and I can read the values in volts and also change to mV etc.. However, its not 'visible' in Y-axis as you can see in the plot below. So, How can I label the axes separately?
Thanks a lot!
I don't know any more efficient possibility than this:
createPlot(id=1, erase=false, grid=true, leftTitleType=2, leftTitle="myLabelY", bottomTitleType=2, bottomTitle="myLabelX")
with:
id being the number shown in the original plot
erase=false to ensure that the content is not modified
grid=true (re-)enabling the grid
*TitleType=2 saying that there is a custom title
*Title being the string to put there
This will result in the plot being resized to the default size. You can use plotSetup() to get the current setup, including position, which you can then pass (manually) to the createPlot() command to result in the original size again.
Not very elegant, but I don't know any other possibility...

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:

How to draw abline() that doesn't intersect the Y-axis in R?

I have a location quotient plot drawn in R and want to draw a horizontal line along the plot where Y = 1. I have the code abline(h=1, col="black") but when the line is drawn, it intersects the Y-axis and crosses out my Y-axis labels.
Does anyone know how to terminate the line at the Y-axis rather than having it intersect?
Many thanks.
As mentioned in the comments, it looks like the parameter xpd has been changed, so one option is to change it back to FALSE, see ?par. you can control the clipping region using the clip function to further limit the range that abline and other functions plot within. This may also be affected by you plotting device (different devices can deal with clipping differently).

How to put legend marker on the right side of the word in R plot [duplicate]

The legend that R creates when you call legend() has the symbols (or line types etc) on the left and the labels on the right. I'd like it the other way round, i.e. labels on the left (right-aligned) and the symbols on the right.
I know that I can use adj to adjust the position of the labels, but with this they are not aligned properly anymore. If I set adj=2 for example, the labels are to the left of the symbols, but the end of the text is not aligned with the symbols.
Any pointers on how to do this using either the standard legend() function or a package would be appreciated.
If you set trace = TRUE and then save the output, you can draw the legend and then add the labels with a call to text() using the coordinates given by trace, setting pos = 2 for right alignment. Here's an example:
set.seed(1)
plot(1:10,runif(min=0,max=10,10),type='l',ylim=c(0,10),xlim=c(0,10),col=1)
lines(1:10,runif(min=0,max=10,10),col=2,lty=2)
lines(1:10,runif(min=0,max=10,10),col=3,lty=2)
a <- legend(1,10,lty=1:3,col=1:3,legend=c("","",""),bty="n",trace=TRUE)
text(a$text$x-1,a$text$y,c("line 1","line 2","line 3"),pos=2)

Plotting a box within filled.contour plots in R?

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps

Resources