R plot tickmarks - r

I have a plot in R with both x and y-axes tick labels looking like this:
-1.5 -1.0 -0.5 0.0 0.5 1.0
How can I remove the -1.5, -0.5, 0.5 tickmarks/labels?
How can I remove the decimal from the remaining labels? (i.e. -1.0 to become -1).

So, a plot like this is what I hear you describing:
plot(seq(-1.5, 1.1, 0.5),seq(-1.5, 1, 0.5))
Adding the xaxp and yaxp vectors will let you fine-tune the labeling:
plot(seq(-1.5, 1.1, 0.5),seq(-1.5, 1, 0.5), xaxp=c(-1,1,2), yaxp=c(-1,1,2))
The input to xaxp is "A vector of the form c(x1, x2, n) giving the coordinates of the extreme tick marks and the number of intervals between tick-marks when par("xlog") is false." See the help page for par{graphics} for more details

Related

How to point a triangle at the tip of a line [R]

I am using a triangle to mark an event on a timeline in R, and I've given the coordinates to the specific position on the line where the event occurs in days. In the points( function, I have supplied pch=25 to create the "filled triangle" shape. However, the positioning of the character is based on the center of the triangle. Is it possible to use an argument like "pos" (i.e. pos=3) so that the triangle is positioned immediately above the line and points to to X coordinate of interest?
Example:
plot.new()
segments(0, 0.5, 1, 0.5)
points(0.5, 0.5, pch=25)
have
want
I dont think there is an inherent function for this (i.e. pos-like function) but in the past, I have added a manual adjustment:
plot.new()
adj <- 0.015
segments(0, 0.5, 1, 0.5)
points(0.5, 0.5 + adj, pch=25)
So with multiple points:
points(seq(0.1, 0.9, 0.1), rep(0.5, 9) + adj, pch = 25)
Since R's interpreter supports a wide variety of encodings, and since the pch is just the text input, you can just paste the down triangle into the text editor and calculate:
strheight('▽') -> l
and change the last line to
points(0.5, 0.5 + l/2, pch=25)
to get the desired
> strheight('▽')
[1] 0.1022132

Squish points to within plot axes in R plot

I am making a volcano plot in R. I have a huge range of pvalues and log2fold changes. I set an xlim and ylim because I want to focus in on the central region of the plot. However, naturally setting my limits excludes some of my data. I would like to have the data outside of my axes limits displayed at my limits. So for example, a fold change of 4 would be displayed as a point just outside of my xlim of 2.
with(mydata, plot(ExpLogRatio, -log10(Expr_p_value), pch=20, main = "Volcano Plot",xlim=c(-2,2),ylim=c(0,40)))
this works but cuts out some of my datapoints (those with fold change above 2 and less than -2 and with pvalue of less than -log10(40)
if I understand correctly, I'd just use pmin and pmax to limit your values, e.g.:
values = seq(-3, 3, len=21)
pmin(pmax(values, -2), 2)
gives back:
[1] -2.0 -2.0 -2.0 -2.0 -1.8 -1.5 -1.2 -0.9 -0.6 -0.3 0.0 0.3 0.6 0.9 1.2
[16] 1.5 1.8 2.0 2.0 2.0 2.0
i.e. it's limited values to the range (-2, +2).
applying this to your data, you'd do something like:
with(mydata, {
lratio <- pmin(pmax(ExpLogRatio, -2.1), 2.1)
pch <- ifelse(ExpLogRatio == lratio, 20, 4)
plot(lratio, -log10(Expr_p_value), pch=pch, ylim=c(0, 40))
})
you'll probably want to set xlab and main to set titles, but I've not included that to keep the answer tidier. also extending this to the y-axis would obviously be easy
note I've also changed the plotting point style to indicate which points were truncated

Contour Plot in Maple

I'm trying to plot contours in Maple, but the 2d contour plot output is not pretty. I tried the following command:
with(plots):
contourplot(-(1/2)*y^2-(1/2)*x^2-(1-.3)/sqrt((x+.3)^2+y^2)+((-1)*.3)/sqrt((x-1+.3)^2+y^2),
x = -1.5 .. 1.5, y = -1.5 .. 1.5, axes = boxed)
and the plot is so much uglier than the 3d one:
contourplot3d(-(1/2)*y^2-(1/2)*x^2-(1-.3)/sqrt((x+.3)^2+y^2)+((-1)*.3)/sqrt((x-1+.3)^2+y^2),
x = -1.5 .. 1.5, y = -1.5 .. 1.5, view = -2 .. -1.3, axes = boxed)
Is there any way I can get the same detail in the 2d one as there is in the 3d one.
Thanks in advance!
By supplying (only a modest number) of specific contour values in a desired range you can get a useful result without incurring too much computational cost.
Note that in your call to contourplot3d above you specified a range of -2 to -1.3, through the view option. Below contours are specified for the range -2.5 to -1.3. (But it would also look as useful as the 3D call for a range from -2 to -1.3.)
The essential problem is that, for the default of a small number of contour levels, the contourplot command is taking them mostly in a range that doesn't produce the nice "even" spread. A more costly solution would be to simply ramp up the number of contours to be something high, eg, contours=100. But the call below only makes 13 contour levels.
plots:-contourplot( -(1/2)*y^2-(1/2)*x^2-(1-.3)/sqrt((x+.3)^2+y^2)
+((-1)*.3)/sqrt((x-1+.3)^2+y^2),
x=-2.25..2.25, y=-2.25..2.25, axes=boxed,
contours=[seq(-2.5..-1.3,0.1)], grid=[80,80],
coloring=["Niagara Azure","Orange"] );

R plot ratio of success vs continuous variable

What is the best way to plot a graph with a continuous variable on the x axis and the ratio of success on the y axis for example with data:
x <- c(.1,.3,.4,.5,.6,.3,.4,.6,.7,.8)
y <- c(0,0,0,0,0,1,1,1,1,1)
df <- cbind(x,y)
plot(x,y)
I want to see values on the y as a ratio instead of 0 and 1. But, need to aggregate x values since the data is continuous and not .1,.2, etc.
For .3 on the x for examle, the point should have a y value of .5 (instead of one on 1, and one on 0).
I want to model success but I don't know what type of model to use, linear or something else. I would like to see the shape of the curve and then find a proper fit.
Thanks!
OK I'm guessing you mean this?
> rowMeans(table(x,y))
0.1 0.3 0.4 0.5 0.6 0.7 0.8
0.5 1.0 1.0 0.5 1.0 0.5 0.5
> R=rowMeans(table(x,y))
> plot(names(R),R, type='h', ylim=c(0,1))

R: heatmap.2 change color key

I have a question about the package gplots. I want to use the function heatmap.2 and therefore I want to change my symmetric point in color key from 0 to 1. Normally when symkey=TRUE and you use the col=redgreen(), a colorbar is created where the colors are managed like this:
red = -2 to -0.5
black=-0.5 to 0.5
green= 0.5 to 2
Now i want to create a colorbar like this:
red= -1 to 0.8
black= 0.8 to 1.2
green= 1.2 to 3
Is something like this possible?
Thank you!
If you look at the heatmap.2 help file, it looks like you want the breaks argument. From the help file:
breaks (optional) Either a numeric vector indicating the splitting points for binning x into colors, or a integer number of break points to be used, in which case the break points will be spaced equally between min(x) and max(x)
So, you use breaks to specify the cutoff points for each colour. e.g.:
library(gplots)
# make up a bunch of random data from -1, -.9, -.8, ..., 2.9, 3
# 10x10
x = matrix(sample(seq(-1,3,by=.1),100,replace=TRUE),ncol=10)
# plot. We want -1 to 0.8 being red, 0.8 to 1.2 being black, 1.2 to 3 being green.
heatmap.2(x, col=redgreen, breaks=c(-1,0.8,1.2,3))
The crucial bit is the breaks=c(-1,0.8,1.2,3) being your cutoffs.

Resources