I have a data of turning angles for a group of animals separated by occupation areas (breeding ground, migratory route, feeding area).
I need to plot a circular graphic in R for angle values in degrees for each area.
The angle values are like that in the data frame
[1] NA 41.027 -43.410 29.056 18.241 -7.125 -4.702 0.298
[9] 37.846 -7.545 -69.403 -7.376 17.289 7.927 60.752 -85.219
[17] 24.218 -17.482 3.703 -3.901 -8.582 -84.871 38.448 44.028
[25] -150.796 -59.679 -169.927 -6.862 51.130 -1.784 -16.468 -2.356
[33] 5.645 -6.988 4.750 -5.707 2.949 -6.150 -4.129 0.869
[41] -1.935 5.130 0.559 4.686 145.086 14.324 -169.206 1.741
[49] 53.595 15.315 36.892 49.279 21.171 10.739 122.553 -141.081
[57] 3.126 48.323 -7.139 163.742 141.473 47.320 128.430 175.918
[65] 7.447 -16.159 55.957 37.351 -2.703 -25.308 -31.338 NA
[73] NA -16.028 25.110 -31.085 -92.887 88.917 146.903 -148.539
[81] -11.576 41.030 -155.616 -129.368 -32.886 -164.284 -120.785 118.591
[89] 68.335 -98.038 40.347 166.333 19.495 -170.337 -178.322 99.111
can someone help me with this simple question? thank u!
It is not clear what you want, but here are two visualizations that might help. The first just plots points on the unit circle to show the angles. The second version has lines in the directions of turn. BTW, I simply left out your NAs.
Data at the bottom
x = cos(pi*Turns/180)
y = sin(pi*Turns/180)
par(mfrow=c(1,2))
plot(x,y, pch=20, col="#22222266", asp=1)
plot(x,y, pch=20, col="#22222266", asp=1)
N = length(x)
segments(0, 0, x, y)
Data
Turns = c(41.027 -43.410, 29.056, 18.241, -7.125, -4.702, 0.298,
37.846, -7.545 -69.403, -7.376, 17.289, 7.927, 60.752 -85.219,
24.218, -17.482, 3.703, -3.901, -8.582 -84.871, 38.448, 44.028,
-150.796, -59.679 -169.927, -6.862, 51.130, -1.784 -16.468, -2.356,
5.645, -6.988, 4.750, -5.707, 2.949, -6.150, -4.129, 0.869,
-1.935, 5.130, 0.559, 4.686, 145.086, 14.324 -169.206, 1.741,
53.595, 15.315, 36.892, 49.279, 21.171, 10.739, 122.553, -141.081,
3.126, 48.323, -7.139, 163.742, 141.473, 47.320, 128.430, 175.918,
7.447, -16.159, 55.957, 37.351, -2.703 -25.308, -31.338,
-16.028, 25.110, -31.085, -92.887, 88.917, 146.903, -148.539,
-11.576, 41.030, -155.616, -129.368, -32.886, -164.284, -120.785, 118.591,
68.335, -98.038, 40.347, 166.333, 19.495, -170.337, -178.322, 99.111)
Related
I have a output for density estimation.
$x
[1] 0.100001 0.600001 0.500001 0.800001 0.500001 0.100001 0.600001 0.300001
[9] 0.100001 0.400001 0.700001 0.500001 0.000001 0.200001 0.700001 0.500001
[17] 0.000001 0.400001 0.500001 0.400001 0.200001 0.100001 0.600001 0.700001
[25] 0.700001 0.200001 0.800001 0.500001 0.200001 0.200001
$y
[1] 1.2246774 1.1437131 1.3626914 0.6381394 1.3626914 1.2246774 1.1437131
[8] 1.5893983 1.2246774 1.5158009 0.8852983 1.3626914 0.6912818 1.5227328
[15] 0.8852983 1.3626914 0.6912818 1.5158009 1.3626914 1.5158009 1.5227328
[22] 1.2246774 1.1437131 0.8852983 0.8852983 1.5227328 0.6381394 1.3626914
[29] 1.5227328 1.5227328
where x are grid points and y are estimated values. When these are plotted, its graph is very weird with type "l"
. Its a density plot which should have a single line. Please guide me how it can be soughed.
You need to plot them in the correct order (ordered according to the value of x):
plot(sort(x), y[order(x)], type = "l")
Reproducible data
x <- c(0.100001, 0.600001, 0.500001, 0.800001, 0.500001, 0.100001,
0.600001, 0.300001, 0.100001, 0.400001, 0.700001, 0.500001, 1e-06,
0.200001, 0.700001, 0.500001, 1e-06, 0.400001, 0.500001, 0.400001,
0.200001, 0.100001, 0.600001, 0.700001, 0.700001, 0.200001, 0.800001,
0.500001, 0.200001, 0.200001)
y <- c(1.2246774, 1.1437131, 1.3626914, 0.6381394, 1.3626914, 1.2246774,
1.1437131, 1.5893983, 1.2246774, 1.5158009, 0.8852983, 1.3626914,
0.6912818, 1.5227328, 0.8852983, 1.3626914, 0.6912818, 1.5158009,
1.3626914, 1.5158009, 1.5227328, 1.2246774, 1.1437131, 0.8852983,
0.8852983, 1.5227328, 0.6381394, 1.3626914, 1.5227328, 1.5227328)
Note that in your data, for some reason there are multiple points with the same values.
I am trying to turn a vector of length n (say, 14), and turn it into a vector of length N (say, 90). For example, my vector is
x<-c(5,3,7,11,12,19,40,2,22,6,10,12,12,4)
and I want to turn it into a vector of length 90, by creating 90 equally "spaced" points on this vector- think of x as a function. Is there any way to do that in R?
Something like this?
> x<-c(5,3,7,11,12,19,40,2,22,6,10,12,12,4)
> seq(min(x),max(x),length=90)
[1] 2.000000 2.426966 2.853933 3.280899 3.707865 4.134831 4.561798
[8] 4.988764 5.415730 5.842697 6.269663 6.696629 7.123596 7.550562
[15] 7.977528 8.404494 8.831461 9.258427 9.685393 10.112360 10.539326
[22] 10.966292 11.393258 11.820225 12.247191 12.674157 13.101124 13.528090
[29] 13.955056 14.382022 14.808989 15.235955 15.662921 16.089888 16.516854
[36] 16.943820 17.370787 17.797753 18.224719 18.651685 19.078652 19.505618
[43] 19.932584 20.359551 20.786517 21.213483 21.640449 22.067416 22.494382
[50] 22.921348 23.348315 23.775281 24.202247 24.629213 25.056180 25.483146
[57] 25.910112 26.337079 26.764045 27.191011 27.617978 28.044944 28.471910
[64] 28.898876 29.325843 29.752809 30.179775 30.606742 31.033708 31.460674
[71] 31.887640 32.314607 32.741573 33.168539 33.595506 34.022472 34.449438
[78] 34.876404 35.303371 35.730337 36.157303 36.584270 37.011236 37.438202
[85] 37.865169 38.292135 38.719101 39.146067 39.573034 40.000000
>
Try this:
#data
x <- c(5,3,7,11,12,19,40,2,22,6,10,12,12,4)
#expected new length
N=90
#number of numbers between 2 numbers
my.length.out=round((N-length(x))/(length(x)-1))+1
#new data
x1 <- unlist(
lapply(1:(length(x)-1), function(i)
seq(x[i],x[i+1],length.out = my.length.out)))
#plot
par(mfrow=c(2,1))
plot(x)
plot(x1)
I have data called veteran stored in R. I created a survival model and now wish to predict survival probability predictions. For example, what is the probability that a patient with 80 karno value, 10diagtime, age 65 and prior=10 and trt = 2 lives longer than 100 days?
In this case the design matrix is x = (1,0,1,0,80,10,65,10,2)
Here is my code:
library(survival)
attach(veteran)
weibull <- survreg(Surv(time,status)~celltype + karno+diagtime+age+prior+trt ,dist="w")
and here is the output:
Any idea how to predict the survival probabilities?
You can get predict.survreg to produce predicted times of survival for individual cases (to which you will pass values to newdata) with varying quantiles:
casedat <- list(celltype="smallcell", karno =80, diagtime=10, age= 65 , prior=10 , trt = 2)
predict(weibull, newdata=casedat, type="quantile", p=(1:98)/100)
[1] 1.996036 3.815924 5.585873 7.330350 9.060716 10.783617
[7] 12.503458 14.223414 15.945909 17.672884 19.405946 21.146470
[13] 22.895661 24.654597 26.424264 28.205575 29.999388 31.806521
[19] 33.627761 35.463874 37.315609 39.183706 41.068901 42.971927
[25] 44.893525 46.834438 48.795420 50.777240 52.780679 54.806537
[31] 56.855637 58.928822 61.026962 63.150956 65.301733 67.480255
[37] 69.687524 71.924578 74.192502 76.492423 78.825521 81.193029
[43] 83.596238 86.036503 88.515246 91.033959 93.594216 96.197674
[49] 98.846083 **101.541291** 104.285254 107.080043 109.927857 112.831032
[55] 115.792052 118.813566 121.898401 125.049578 128.270334 131.564138
[61] 134.934720 138.386096 141.922598 145.548909 149.270101 153.091684
[67] 157.019655 161.060555 165.221547 169.510488 173.936025 178.507710
[73] 183.236126 188.133044 193.211610 198.486566 203.974520 209.694281
[79] 215.667262 221.917991 228.474741 235.370342 242.643219 250.338740
[85] 258.511005 267.225246 276.561118 286.617303 297.518110 309.423232
[91] 322.542621 337.160149 353.673075 372.662027 395.025122 422.263020
[97] 457.180183 506.048094
#asterisks added
You can then figure out which one is greater than the specified time and it looks to be around the 50th percentile, just as one might expect from a homework question.
png(); plot(x=predict(weibull, newdata=casedat, type="quantile",
p=(1:98)/100), y=(1:98)/100 , type="l")
dev.off()
I attempting a scatter plot with many points (> 150). The goal is to distinguish points at certain areas of the graph. What I'm essentially looking for is a way to have 2 color scales for the x and y axes (1 for each). Essentially, I'm looking for something like this:
Each unique point should be a mix of the colours of the respective scales. What I have tried so far is a scatter plot using ggplot. I've tried setting the colour attribute, but that assigns its own coordinates. It also doesn't work with a limitation I have in that I have to create separate plots of the scatter plot (in short, zoomed in plots of the top-left, top-right, bottom-left, bottom-right). If I set the xlim and ylim to my own liking for the additional plots, all I get is a crop which results in some cutouts of other points and their texts on the edges of the plot. I can't simply create a separate plot as I need the points to be the same colour on my overall plot and the more specific plots (singular colours).
png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0)
ggplotXY
dev.off()
Current overall plot:
Current plot of zoomed in bottom-left:
png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0) +
coord_cartesian(xlim=c(0,100), ylim=c(0, 2.5))
ggplotXY
dev.off()
As you can see, some of the points are clipped and aren't ommitted. In order to leave out the non applicable points, I'll have to create a new data frame with the actual points within the limits, but doing so would alter the colours of the points when I create a new plot. I was thinking about including my own colours for each point as part of my data frame that I'm reading in, but adding and subtracting hex colour codes is not very nice. I tried and got something along these lines:
png("image.png", width = 2000, height = 1500, res = 85);
ggplotXYColor <- ggplot(scatterPlotData, aes(x=x, y=y, label=labels)) +
geom_point(colour=scatterPlotData$scatterPointColour)
ggplotXYColor
dev.off()
In case you are wondering, the scatterPlotData$scatterPointColour is as follows:
[1] "#2276c6" "#224dd0" "#201893" "#22459f" "#21580f" "#219998" "#201893"
[8] "#216871" "#22459f" "#201893" "#2276c6" "#22459f" "#22353d" "#201893"
[15] "#225602" "#21cabe" "#2178d3" "#21eb83" "#21eb83" "#201893" "#201893"
[22] "#22978b" "#2276c6" "#301054" "#201893" "#301054" "#225e33" "#228f59"
[29] "#226664" "#220c47" "#21eb83" "#228f59" "#227ef7" "#227ef7" "#226e95"
[36] "#21c28d" "#22459f" "#228f59" "#223d6e" "#221caa" "#22459f" "#226e95"
[43] "#225602" "#221caa" "#21d2f0" "#222d0c" "#22459f" "#201893" "#2020c4"
[50] "#210623" "#21a1c9" "#201893" "#228f59" "#201893" "#201893" "#221caa"
[57] "#220c47" "#201893" "#22a7ed" "#101893" "#22c080" "#201893" "#2276c6"
[64] "#201893" "#201893" "#21d2f0" "#222d0c" "#21c28d" "#225602" "#226664"
[71] "#226e95" "#201893" "#201893" "#21b22b" "#2020c4" "#21cabe" "#21f3b4"
[78] "#22d0e2" "#201893" "#21c28d" "#21fbe5" "#220c47" "#225602" "#230209"
[85] "#226664" "#210e55" "#211eb7" "#2170a2" "#201893" "#221caa" "#220c47"
[92] "#21f3b4" "#21fbe5" "#201893" "#201893" "#201893" "#224dd0" "#247add"
[99] "#201893" "#23fffc" "#25db1d" "#24188f" "#245a18" "#2449b6" "#24a3d3"
[106] "#201893" "#2451e7" "#24624a" "#24830e" "#2020c4" "#201893" "#201893"
[113] "#25b228" "#25eb80" "#23ced5" "#244185" "#24ed8d" "#243123" "#2449b6"
[120] "#201893" "#273b5e" "#201893" "#264dcd" "#2420c1" "#2578d0" "#264dcd"
[127] "#251eb3" "#22c8b1" "#22c080" "#22f1a7" "#249370" "#251eb3" "#2428f2"
[134] "#2428f2" "#249ba1" "#201893" "#2020c4" "#201893" "#244185" "#2472ac"
[141] "#2449b6" "#247add" "#201893" "#244185" "#243123" "#249370" "#24b435"
[148] "#2020c4" "#248b3f" "#2020c4"
I converted the hex colours to decimal and then added specific decimal colours together and then converted it back to hex. Theoretically, it should be a nice white to yellow on the x-axis and white to blue on the y-axis. As the points increase in x and y, the colours should become more green. As you can see, it's not as simple as that. I haven't come across any libraries that does the 2 axes colours.
To sum up, I need to be able to have the 2 axes colours to give unique colours to the points and a way to create additional plots that will have the exact some colours just on a more zoomed in canvas.
If anyone can help, it would be greatly appreciated.
Here you have a first approach using base graphics for your first problem (mixing two color gradients).
## use white->yellow for the x-axis and white->blue for the y-axis
chooseColors <- function(x, y) {
x <- 1-x/max(x)
y <- 1-y/max(y)
return(rgb(green=y, red=y, blue=x))
}
## example values for the whole range
values <- expand.grid(1:100, 1:100)
## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)
A more realistic toy example:
set.seed(1)
n <- 50
values <- cbind(sample(1:15, size=n, replace=TRUE), sample(1:15, size=n, replace=TRUE))
## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
scale a series between two points in R
Does any know of an R function to perform range standardization on a vector? I'm looking to transform variables to a scale between 0 and 1, while retaining rank order and the relative size of separation between values.
Just to be clear, i'm not looking to standardize variables by mean centering and scaling by the SD, as is done in the function scale().
I tried the functions mmnorm() and rangenorm() in the package 'dprep', but these don't seem to do the job.
s = sort(rexp(100))
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
range01(s)
[1] 0.000000000 0.003338782 0.007572326 0.012192201 0.016055006 0.017161145
[7] 0.019949532 0.023839810 0.024421602 0.027197168 0.029889484 0.033039408
[13] 0.033783376 0.038051265 0.045183382 0.049560233 0.056941611 0.057552543
[19] 0.062674982 0.066001242 0.066420884 0.067689067 0.069247825 0.069432174
[25] 0.070136067 0.076340460 0.078709590 0.080393512 0.085591881 0.087540132
[31] 0.090517295 0.091026499 0.091251213 0.099218526 0.103236344 0.105724733
[37] 0.107495340 0.113332392 0.116103438 0.124050331 0.125596034 0.126599323
[43] 0.127154661 0.133392300 0.134258532 0.138253452 0.141933433 0.146748798
[49] 0.147490227 0.149960293 0.153126478 0.154275371 0.167701855 0.170160948
[55] 0.180313542 0.181834891 0.182554291 0.189188137 0.193807559 0.195903010
[61] 0.208902645 0.211308713 0.232942314 0.236135220 0.251950116 0.260816843
[67] 0.284090255 0.284150541 0.288498370 0.295515143 0.299408623 0.301264703
[73] 0.306817872 0.307853369 0.324882091 0.353241217 0.366800517 0.389474449
[79] 0.398838576 0.404266315 0.408936260 0.409198619 0.415165553 0.433960390
[85] 0.440690262 0.458692639 0.464027428 0.474214070 0.517224262 0.538532221
[91] 0.544911543 0.559945121 0.585390414 0.647030109 0.694095422 0.708385079
[97] 0.736486707 0.787250428 0.870874773 1.000000000
Adding ... will allow you to pass through na.rm = T if you want to omit missing values from the calculation (they will still be present in the results):
range01 <- function(x, ...){(x - min(x, ...)) / (max(x, ...) - min(x, ...))}