Max values for three plots in one graph in software R - r

I have a graph with three plots. I made them with the package ggplot2.
I want to calculate to what time the three plots reach the same value of X C using R programming.
I want also want to mark the point on the graph e.g. with a longitudinal dashed line from the x-axis.
I tried to use which.max, but it only gives me one value for one of the plots that reach plateau first. I want the the time-value where all plots reach 37 C at the same time.

You could do something like this, which gives you the index of the first time they are all equal to 37:
# generate example data
x <- c(3:37, rep(37,3))
y <- c(5:37, rep(37,5))
z <- c(7:37, rep(37,7))
# first instance when all are equal to 37
min(which(x == 37 & y == 37 & z ==37))
#> [1] 35
Created on 2021-03-04 by the reprex package (v1.0.0)

Related

divide not rectangle plot into subplots within spatstat package in R

I have data that contains information about sub-plots with different numbers and their corresponding species types (more than 3 species within each subplot). Every species have X & Y coordinates.
> df
subplot species X Y
1 1 Apiaceae 268675 4487472
2 1 Ceyperaceae 268672 4487470
3 1 Vitaceae 268669 4487469
4 2 Ceyperaceae 268665 4487466
5 2 Apiaceae 268662 4487453
6 2 Magnoliaceae 268664 4487453
7 3 Magnoliaceae 268664 4487453
8 3 Apiaceae 268664 4487456
9 3 Vitaceae 268664 4487458
with these data, I have created ppp for the points of each subplot within a window of general plot (big).
grp <- factor(data$subplot)
win <- ripras(data$X, data$Y)
p.p <- ppp(data$X, data$Y, window = window, marks = grp)
Now I want to divide a plot into equal 3 x 3 sub-plots because there are 9 subplots. The genetal plot is not rectangular looks similar to rombo shape when I plot.
I could use quadrats() funcion as below but it has divided my plot into unequal subplots. Some are quadrat, others are traingle etc which I don't want. I want all the subplots to be equal sized quadrats (divide it by lines that paralel to each sides). Can you anyone guide me for this?
divide <-quadrats(p.patt,3,3)
plot(divide)
Thank you!
Could you break up the plot canvas into 3x3, then run each plot?
> par(mfrow=c(3,3))
> # run code for plot 1
> # run code for plot 2
...
> # run code for plot 9
To return back to one plot on the canvas type
> par(mfrow=c(1,1))
This is a question about the spatstat package.
You can use the function quantess to divide the window into tiles of equal area. If you want the tile boundaries to be vertical lines, and you want 7 tiles, use
B <- quantess(Window(p.patt), "x", 7)
where p.patt is your point pattern.

Overlapping numerical values on Cook's distance plot in R

I want to identify a couple of points with high leverage on the plot below, but unfortunately, their row number is illegible, because there must be a couple of such points, and their id is printed out one on top of the other. They are all the way to the right of the plot:
How can the print out of these labels on the plot be resized and spread out so that they can be legible?
The easiest way to find the cooks distance is the built in function:
LM = lm(speed ~ dist, cars)
cooks.distance(LM)
You can pick out whatever values you want:
> which(cooks.distance(LM) > 0.05)
1 2 23 35 39 49
1 2 23 35 39 49

How to draw the pairwise marginal distribution for each pair of parameters in a grid using ggplot2

Assuming I have the posterior samples for each of the four parameters. My question is how to plot the pairwise marginal distribution on a grid of 4*4=16 with ggplot2?
I would like to creat a plot like the picture below but instead of the scatter plot I will use a pairwise marginal distributions. Organized in the form of this kind of grid.
I am wondering can ggmcmc package achieve my goal?
Thanks in advance, guys!!
After getting help from the previous comments, I post the code below in case other people would like to do the same thing as me.
Below is a simple dataset I create for demonstration.This is the dataset "df" with four variables x, y, z, w. We want to get the pairwise joint kernel density estimation. One easy way I find is to use ggpairs from GGally package based on the comments by user20650. The codes are below: It will create the following plot:
ggpairs(df,upper = list(continuous = "density"),
lower = list(combo = "facetdensity"))
x y z w
1 0.49916998 -0.07439680 0.37731097 0.0927331640
2 0.25281542 -1.35130718 1.02680343 0.8462638556
3 0.50950876 -0.22157249 -0.71134553 -0.6137126948
4 0.28740609 -0.17460743 -0.62504812 -0.7658094835
5 0.28220492 -0.47080289 -0.33799637 -0.7032576540
6 -0.06108038 -0.49756810 0.49099505 0.5606988283
7 0.29427440 -1.14998030 0.89409384 0.5656682378
8 -0.37378096 -1.37798177 1.22424964 1.0976507702
9 0.24306941 -0.41519951 0.17502049 -0.1261603208
10 0.45686871 -0.08291032 0.75929106 0.7457002259
11 -0.16567173 -1.16855088 0.59439600 0.6410396945
12 0.22274809 -0.19632766 0.27193362 0.5532901113
13 1.25555629 0.24633499 -0.39836999 -0.5945792966
14 1.30440121 0.05595755 1.04363679 0.7379212885
15 -0.53739075 -0.01977930 0.22634275 0.4699563173
16 0.17740551 -0.56039760 -0.03278126 -0.0002523205
17 1.02873716 0.05929581 -0.74931661 -0.8830775310
18 -0.13417946 -0.60421101 -0.24532606 -0.1951831558
19 0.11552305 -0.14462104 0.28545703 -0.2527437818
20 0.71783902 -0.12285529 1.23488185 1.3224880574

Identifying data points amongst background noise for binned data R

Not sure whether this should go on cross validated or not but we'll see. Basically I obtained data from an instrument just recently (masses of compounds from 0 to 630) which I binned into 0.025 bins before plotting a histogram as seen below:-
I want to identify the bins that are of high frequency and that stands out from against the background noise (the background noise increases as you move from right to left on the a-xis). Imagine drawing a curve line ontop of the points that have almost blurred together into a black lump and then selecting the bins that exists above that curve to further investigate, that's what I'm trying to do. I just plotted a kernel density plot to see if I could over lay that ontop of my histogram and use that to identify points that exist above the plot. However, the density plot in no way makes any headway with this as the densities are too low a value (see the second plot). Does anyone have any recommendations as to how I Can go about solving this problem? The blue line represents the density function plot overlayed and the red line represents the ideal solution (need a way of somehow automating this in R)
The data below is only part of my dataset so its not really a good representation of my plot (which contains just about 300,000 points) and as my bin sizes are quite small (0.025) there's just a huge spread of data (in total there's 25,000 or so bins).
df <- read.table(header = TRUE, text = "
values
1 323.881306
2 1.003373
3 14.982121
4 27.995091
5 28.998639
6 95.983138
7 2.0117459
8 1.9095478
9 1.0072853
10 0.9038475
11 0.0055748
12 7.0964916
13 8.0725191
14 9.0765316
15 14.0102531
16 15.0137390
17 19.7887675
18 25.1072689
19 25.8338140
20 30.0151683
21 34.0635308
22 42.0393751
23 42.0504938
")
bin <- seq(0, 324, by = 0.025)
hist(df$values, breaks = bin, prob=TRUE, col = "grey")
lines(density(df$values), col = "blue")
Assuming you're dealing with a vector bin.densities that has the densities for each bin, a simple way to find outliers would be:
look at a window around each bin, say +- 50 bins
current.bin <- 1
window.size <- 50
window <- bin.densities[current.bin-window.size : current.bin+window.size]
find the 95% upper and lower quantile value (or really any value you think works)
lower.quant <- quantile(window, 0.05)
upper.quant <- quantile(window, 0.95)
then say that the current bin is an outlier if it falls outside your quantile range.
this.is.too.high <- (bin.densities[current.bin] > upper.quant
this.is.too.low <- (bin.densities[current.bin] < lower.quant)
#final result
this.is.outlier <- this.is.too.high | this.is.too.low
I haven't actually tested this code, but this is the general approach I would take. You can play around with window size and the quantile percentages until the results look reasonable. Again, not exactly super complex math but hopefully it helps.

Plot points for every 15 minutes

I have a text file having the numbers(of float type) which represents time in seconds. I wish to represent the number of occurances every 15 minutes. The sample of my file is:
0.128766
2.888977
25.087900
102.787657
400.654768
879.090874
903.786754
1367.098789
1456.678567
1786.564569
1909.567567
for first 900 seconds(15 minutes), there are 6 occurances. I want to plot that point on y axis first. Then from 900-1800(next 15 minutes), there are 4 occurances. So, i want to plot 4 on my y-axis next. This should go on...
I know the basic plot() function, but i don't know how to plot every 15 minutes. If there is a link present, please guide me to that link.
Use findInterval():
counts <- table(findInterval(x, seq(0, max(x), 900)))
counts
1 2 3
6 4 1
It's easy to plot:
plot(counts)
To build on Andrie's answer. You can add plot(counts, type = 'p') to plot points or plot(counts, type = 'l') to plot a connected line. If you want to plot a curve for the counts you would need to model it using ?lm or ?nls.

Resources