R: Is it possible to put a text box so many cm/inches above a line in graph? (Whithout changing scale of graph) - r

Is it possible to put a text box so many cm/inches above a line in graph in R? (Whithout changing scale of graph). So im plotting the image using plot and i want to specify that the text using the function: text() but I always want the text to be 1cm above the arrow at the specified x-coordinate.
enter image description here

You can do this simply with ?text. So not entirely with cm's but if you know the range of your data you can position your text as data points in the plot.
Example:
Let's make some test data.
dat <- matrix(rnorm(3*4), ncol=2)
colnames(dat) <- c("v1", "v2")
Create a scatter plot. You can of course apply text to any graphical plot, but I'm keeping it simple.
plot(v2 ~ v1, data = dat)
And now just create a line, in whatever direction. I'll just go with an abline at height 1.0 on the y-axis
abline(h=1.0)
text(1,1,"this is an abline", pos = 1)
With text I add text on position 1,1 (x, y) in the plot. I adjust it with pos so It doesn't get crossed by the abline.
Good luck!

Related

How to add text labels to a scatterplot?

Is there a way to add text labels to the points on a scatterplot? Each point has a string associated with it as its label. I like to label only as many points as it can be done withour overlapping?
df = DataFrame(x=rand(100), y=rand(100), z=randstring.(fill(5,100)))
scatter(df.x, df.y)
annotate!(df.x, df.y, text.(df.z))
using StatisticalGraphics package:
using InMemoryDatasets
using StatisticalGraphics
using Random
ds=Dataset(x=rand(100), y=rand(100), z=randstring.(fill(5,100)))
sgplot(ds, Scatter(x=:x,y=:y,labelresponse=:z))
Here is something I wrote for Makie.jl that suited my needs:
Non-overlapping labels for scatter plots
It works best for single line, short text labels, and where all labels have similar lengths with one another. It is still WIP, as I am working to improve it for placement of longer text labels.
Here are some samples of what it can do:
Essentially, you call function viz to plot a scatter chart on your (x, y) data set:
resolution = (600, 600) # figure size (pixels) -- need not be a equal dimension
fontpt = 12 # label font size (points)
flabel = 1.5 # inflate the label size to create some margins
fdist = 0.3 # inflate the max. distance between a label and its
# anchor point before a line is drawn to connect. them.
# Smaller values would create more connecting lines.
viz(x, y, labels; resolution=resolution, flabel=flabel, fdist=fdist, fontpt=fontpt)
where labels is a list containing the text labels for every pair of (x, y) point.
You can use the extra named argument series_annotations in the scatter function. Here us an example where I use "1", "2", etc. as labels:
using Plots
x = collect(0:0.1:2)
y = sinpi.(x)
scatter(x, y, series_annotations = text.(1:length(x), :top))
Avoiding overlaps is more difficult. You could customize your label with empty "" for duplicates where the points are the same, or see for Makie: Makie: Non-overlapping label placement algorithm for scatter plots

Printing text on plot in a specific position Stata

I am trying to print a text in the botton right of some plots. I know that I can use the coordinates to do that in the option text of a plot, but given that I have to plot around 50 charts that change a lot in terms of the axis magnitude, I´m looking if there is a way to do this by default.
clear all
set obs 100
gen x = runiformint(1, 100)
gen y = runiformint(100, 200)
egen z = seq(), f(1) t(100)
scatter x z, text(1 100 "XXXX")
scatter y z, text(1 100 "XXXX")
In this code I can print the text in the first scatter but not in the second one.
Suppose you want to put text in the bottom right-hand corner of plot. Consider using a title option:
sysuse auto, clear
scatter mpg weight, subtitle("frog", pos(5) ring(0))
scatter length weight, subtitle("toad", pos(5) ring(0))
See for discussion https://www.stata-journal.com/article.html?article=gr0051
Warning: Nothing rules out such a title obscuring point, line or area elements that represent data.

How to display a calculation on data values in R legend

Here's a fiddle for a simplified version of a plot I am trying to generate.
On line 44 the plot points are sized according to 1/Error:
main_aes = aes(x = Date, y = Popular_Support, size=1/Error)
But instead of displaying 1/Error values in the legend, I want it to display Sample Size which is 1/Error^2, which the legend title being Sample Size.
I only want this displayed in the legend, but I still want the original values to weight the point sizes.
How can I do this? How can I perform a calculation on the legend text that is displayed and change the legend title?
You can do this as follows:
plot + scale_size_continuous(breaks=seq(40,70,10), labels=seq(40,70,10)^2,
name="Sample Size")
Also, plot is an R function, so it's probably better to use a different name for your plot objects.

panel.text xyplot R

I am adding text to different panels of a xyplot in lattice and was wondering if anyone knows a way to not specify a x and y coordinates or is there something similar to legend where you can say upper left or upper right,etc?
I ask because I want to use scales=free in the plotting code, but when I do the text in the mytext code ends up covering up parts of the graph and doesn't make for a good plot. I would like to have a way to plot the graphs without making individual plots because in my real dataset I have up to 10 grouping factor levels (sams in the code). The example provided is not as extreme as the real data.
Example data
d_exp<-data.frame(sams=c(rep("A",6),rep("B",6),rep("C",6)),
gear=c(rep(1:2,9)),
fraction=c(.12,.61,.23,.05,.13,.45,0.3,.5,.45,.20,.35,.10,.8,.60,.10,.01,.23,.03),
interval=c(rep(c(0,10,20),6)))
d_exp<-d_exp[order(d_exp$sams,d_exp$gear,d_exp$interval),]
Plot with scales=same. mytext x and y coordinates are specified.
mytext<-c("N=3","N=35","N=6")
panel.my <- function(...) {
panel.superpose(col=c("red","blue"),lwd=1.5,...)
panel.text(x=2.5,y=0.5,labels=mytext[panel.number()],cex=.8)
}
xyplot(fraction~interval | sams, data=d_exp,groups=gear,type="l",
scales=list(relation="same",y=list(alternating=1,cex=0.8),x=list(alternating=1,cex=.8,abbreviate=F)),
strip = strip.custom(bg="white", strip.levels = T),drop.unused.levels=T,as.table=T,
par.strip.text=list(cex=0.8),panel=panel.my)
Same thing with scales=free. Text is in odd places because all text has the same coordinates.
xyplot(fraction~interval | sams, data=d_exp,groups=gear,type="l",
scales=list(relation="free",y=list(alternating=1,cex=0.8),x=list(alternating=1,cex=.8,abbreviate=F)),
strip = strip.custom(bg="white", strip.levels = T),drop.unused.levels=T,as.table=T,
par.strip.text=list(cex=0.8),panel=panel.my)
Thanks for any help.
You can use grid.text() to specify units in a range-independent way. For example
library(grid)
panel.my <- function(...) {
panel.superpose(col=c("red","blue"),lwd=1.5,...)
grid.text(x=.5,y=.8,label=mytext[panel.number()])
}
With grid.text the x and y values use npc units by default which range from 0 to 1. So x=.5 means centered and y=.8 means 80% of the way to the top.

How to plot matrix with background color varying according to entry?

I wanted to ask for any general idea about plotting this kind of plot in R which can compare for example the overlaps of different methods listed on the horizontal and vertical side of the plot? Any sample code or something
Many thanks
A ggplot2-example:
# data generation
df <- matrix(runif(25), nrow = 5)
# bring data to long format
require(reshape2)
dfm <- melt(df)
# plot
require(ggplot2)
ggplot(dfm, aes(x = Var1, y = Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = round(value, 2)))
The corrplot package and corrplot function in that package will create plots similar to what you show above, that may do what you want or give you a starting point.
If you want more control then you could plot the colors using the image function, then use the text function to add the numbers. You can either create the margins large enough to place the text in the margins, see the axis function for the common way to add text labels in the margin. Or you could leave enough space internally (maybe use rasterImage instead of image) and use text to do the labelling. Look at the xpd argument to par if you want to add the lines and the grconvertX and grconvertY functions to help with the coordinates of the line segents.

Resources