How to count the number of points below a contour line in R - r

I've created a heatmap in R based on simulations and plotted it using image.plot() and I have added contour lines by using contour(). I also have a data frame that contains a column for observations in first year and trend size that I have plotted on top of the heatmap using base R plot. Is there an easy way to count the number of points below the 0.5 contour line and about the 0.95 contour line?

Assuming you are plotting a variable called z, you can use something like.
table(z>.95)

Related

Split density plot in R ggplot2

How can I do a density plot that is split across variables like the ones done in Figure 1 in this paper ? https://www.pnas.org/content/117/24/13386
Obivously, doing geom_density on filtered data then changes the value of the density towards the border, because it assumes zeroes right after 1.96 if coming from the left.
What I'd need is to compute the whole density, but only plot half of it

How to draw Probability Density Function using line plots in IDL?

I have a vector containing values and I would like to draw a probability density function (PDF) graph for the values contained. Let's say I have a vector given by b=[1,1,3,4,5,2,3,5,1,4,2,4,1,1,4,2,3,5]. I can draw a histogram as follows
cghistoplot, b, binsize=1, xtitle='values', ytitle='freq', /fill
However, I want to draw the pdf using a line plot, i.e. I want y values to be normalized by the number of values in the vector (18 here). I know I can use cgplot to make line plots, but it needs x- and y-values.
You can just use the HISTOGRAM built-in routine like the following:
hist = HISTOGRAM(b,BINSIZE=1,LOCATIONS=locs,MIN=0,MAX=6)
Then you can plot the results doing something like:
PLOT,locs,hist,PSYM=10,XTITLE='values',YTITLE='freq'
This will show a quick and easy histogram form. If you want a line plot, just remove the PSYM keyword setting.

How to create a boxplot with log10 scale and zero values in ggplot2

I am creating several boxplots in ggplot2 with a log10 scale using
coord_trans(y="log10")
It is important that only the scale and not the data itself is log-transformed. One data set includes zero values, which is creating -inf values so that the boxplot cannot be drawn on a log-transformed scale.
I have tried to use
scale_y_continuous(trans=pseudo_log_trans(base=10))
However, this makes changes to the data instead of the scale. Outliers of the boxplot change and the boxplot stats extracted through ggplot_build(examplefig)$data are different from the original data.
Is there any way to create a boxplot in ggplot2 with a log10 scale and data including zero values? There should be no transformation of the data itself and outliers should be displayed like in the boxplot with the original data.
This is the very first question I ask here and I am new to R, so I hope the question is clear.

R plot() - why do my points appear as horizontal lines

I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot

Label outliers in an scatter plot

I've plot this graphic to identify graphically high-leverage points in my linear model.
Given the variable "NOMBRES" of the data set which my model uses, I've tried to plot all the points of my graphic but it gets illegible. Here's the code I ran:
> plot(hatvalues(tmodel),residuals(tmodel))
> text(hatvalues(tmodel),residuals(tmodel),labels=DSET$NOMBRES)
So I would like to plot just the points with leverage(hat value) above 0.05 using the label "DSET$NOMBRES".
Identify high-leverage points according to your definition:
hlev <- which(hatvalues(tmodel)>0.05)
Add numeric labels to the graph:
text(hatvalues(tmodel)[hlev], residuals(tmodel)[hlev],
labels=DSET$NOMBRES[hlev])

Resources