how to write text inside a rectangle in R - r

I want each rectangle to contain a number, so that the first plotted rectangle would contain : rect 1 the second rect 2 and so on, but i don't know how to insert text inside rectangles.
require(grDevices)
## set up the plot region:
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rect(0.0,0.0,40.0,35.0, , text= "transparent")
rect(10.0,210.0,45.0,230.0)
rect(170.0,50.0,240.0,150.0)
rect(75.0,110.0,125.0,125.0)
rect(50.0,130.0,65.0,160.0)
rect(15.0,140.0,30.0,150.0)
rect(100.0,50.0,130.0,90.0)
rect(150.0,40.0,155.0,60.0)
rect(52.0,80.0,75.0,90.0)
rect(62.0,65.0,85.0,75.0)
rect(20.0,75.0,25.0,80.0)
rect(30.0,40.0,50.0,80.0)
rect(102.0,155.0,113.0,217.0)
par(op)

Like the other answers mention, you can use the coordinates that you give to rect to place the text somewhere relative.
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rect(0.0,0.0,40.0,35.0)
center <- c(mean(c(0, 40)), mean(c(0, 35)))
text(center[1], center[2], labels = 'hi')
You can easily put this into a function to save yourself some typing/errors
recttext <- function(xl, yb, xr, yt, text, rectArgs = NULL, textArgs = NULL) {
center <- c(mean(c(xl, xr)), mean(c(yb, yt)))
do.call('rect', c(list(xleft = xl, ybottom = yb, xright = xr, ytop = yt), rectArgs))
do.call('text', c(list(x = center[1], y = center[2], labels = text), textArgs))
}
Use it like this
recttext(50, 0, 100, 35, 'hello',
rectArgs = list(col = 'red', lty = 'dashed'),
textArgs = list(col = 'blue', cex = 1.5))

You need to use text() as a separate graphics call.
coords <- matrix(
c(0.0,0.0,40.0,35.0,
10.0,210.0,45.0,230.0,
170.0,50.0,240.0,150.0,
75.0,110.0,125.0,125.0,
50.0,130.0,65.0,160.0,
15.0,140.0,30.0,150.0,
100.0,50.0,130.0,90.0,
150.0,40.0,155.0,60.0,
52.0,80.0,75.0,90.0,
62.0,65.0,85.0,75.0,
20.0,75.0,25.0,80.0,
30.0,40.0,50.0,80.0,
102.0,155.0,113.0,217.0),
ncol=4,byrow=TRUE)
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rfun <- function(x,i) {
do.call(rect,as.list(x))
}
apply(coords,1,rfun)
text((coords[,1]+coords[,3])/2,
(coords[,2]+coords[,4])/2,
seq(nrow(coords)))

text( (0.0+40.0)/2, (0.0+35.0)/2 , 'transparent')
where we chose x,y to be the centroid of your rectangle. You could define a function to draw the rect then place the text at its centroid.
Note: these coords are large; this will display outside your normal view. So you'll either need to zoom to see it, or scale coords to the range 0.0..1.0
By the way, read 12.2 Low-level plotting commands

Related

How to display multiple custom colour palettes in a single figure with R

To expand upon visualize a list of colors/palette in R I am trying to display a series of custom colour palettes in R in a single figure. Is there a way that I can expand on one of the methods listed in the link to display the list of palettes below:
convert_coolers <- function(coolers_string){
strsplit(coolers_string, split = ", ")[[1]]
}
# diverging
storm_panels <- convert_coolers("#001219, #005f73, #0a9396, #94d2bd, #e9d8a6, #ee9b00, #ca6702, #bb3e03, #ae2012, #9b2226")
harry_tipper <- convert_coolers("#f72585, #b5179e, #7209b7, #560bad, #480ca8, #3a0ca3, #3f37c9, #4361ee, #4895ef, #4cc9f0")
firepit <- convert_coolers("#03071e, #370617, #6a040f, #9d0208, #d00000, #dc2f02, #e85d04, #f48c06, #faa307, #ffba08")
# sequences
the_deep <- convert_coolers("#03045e, #023e8a, #0077b6, #0096c7, #00b4d8, #48cae4, #90e0ef, #ade8f4, #caf0f8")
earth <- convert_coolers("#ede0d4, #e6ccb2, #ddb892, #b08968, #7f5539, #9c6644")
# categorical
pastal_rainbow <- convert_coolers("#ff595e, #ffca3a, #8ac926, #1982c4, #6a4c93")
fisherman <- convert_coolers("#353535, #3c6e71, #ffffff, #d9d9d9, #284b63")
in a figure resembling that displayed by RColorBrewer::display.brewer.all()? i.e. with palettes stacked as horizontal bars labelled to the left with the palette title.
I have been trying to dissect the method out from the RColorBrewer function but am finding that it depends too much on internal variables for me to understand what is going on.
I achieved what I set out to do by modifying RColorBrewer::display.brewer.all
Following directly on from the code in the question:
display_custom_palettes <- function(palette_list, palette_names){
nr <- length(palette_list)
nc <- max(lengths(palette_list))
ylim <- c(0, nr)
oldpar <- par(mgp = c(2, 0.25, 0))
on.exit(par(oldpar))
plot(1, 1, xlim = c(0, nc), ylim = ylim, type = "n", axes = FALSE,
bty = "n", xlab = "", ylab = "")
for (i in 1:nr) {
nj <- length(palette_list[[i]])
shadi <- palette_list[[i]]
rect(xleft = 0:(nj - 1), ybottom = i - 1, xright = 1:nj,
ytop = i - 0.2, col = shadi, border = "light grey")
}
text(rep(-0.1, nr), (1:nr) - 0.6, labels = palette_names, xpd = TRUE,
adj = 1)
}
plot.new()
palette_list <- list(storm_panels, harry_tipper, firepit, the_deep, earth, pastal_rainbow, fisherman)
palette_names <- c("storm panels", "harry tipper", "firepit", "the deep", "earth", "rainbow", "fisherman")
display_custom_palettes(palette_list, palette_names)

<SOLVED> How to plot a sphere as wireframe with back view hidden, in R?

Using R, I would like to plot a sphere with latitude and longitude lines, but without any visibility of hidden part of the sphere. And, ideally, I'd like to have the initial view start out with a specific tilt (but that's down the road).
This matlab question gets to the idea
Plotting a wireframe sphere in Python hidding backward meridians and parallels
... but it's matlab. The closest solution that stackoverflow suggested
Plot Sphere with custom gridlines in R
doesn't help with the hidden line aspect.
The closest I got was editting a sphereplot routine:
library(sphereplot)
matt.rgl.sphgrid <- function (radius = 1, col.long = "red", col.lat = "blue", deggap = 15,
longtype = "H", add = FALSE, radaxis = TRUE, radlab = "Radius")
{
if (add == F) {
open3d(userMatrix = rotationMatrix((90)*pi/180, 1, 0, 0)) #changed
}
for (lat in seq(-90, 90, by = deggap)) {
if (lat == 0) {
col.grid = "grey50"
}
else {
col.grid = "grey"
}
#create an array here using the sph2car call below, then rotate those and
#set the appropriate ones to NA before passing that array to this call
#ditto for the next plot3d call as well
plot3d(sph2car(long = seq(0, 360, len = 100), lat = lat,
radius = radius, deg = T),
col = col.grid, add = T,
type = "l")
}
for (long in seq(0, 360 - deggap, by = deggap)) {
if (long == 0) {
col.grid = "grey50"
}
else {
col.grid = "grey"
}
plot3d(sph2car(long = long, lat = seq(-90, 90, len = 100),
radius = radius, deg = T),
col = col.grid, add = T,
type = "l")
}
if (longtype == "H") {
scale = 15
}
if (longtype == "D") {
scale = 1
}
# rgl.sphtext(long = 0, lat = seq(-90, 90, by = deggap), radius = radius,
# text = seq(-90, 90, by = deggap), deg = TRUE, col = col.lat)
# rgl.sphtext(long = seq(0, 360 - deggap, by = deggap), lat = 0,
# radius = radius, text = seq(0, 360 - deggap, by = deggap)/scale,
# deg = TRUE, col = col.long)
}
matt.rgl.sphgrid(radaxis=FALSE)
But I can't figure out how to hide the lines.
Any pointers or examples I've overlooked?
SOLUTION: Just prior to the plot3d calls, set any negative values in "y" (in this case, given a first rotation of 90 degrees) to NA

More plots in an unique page: the titles are hidden

I'm trying to plot 6x4 graphs on a page, each with its title. I have used a lot of options.
pdf("myDocument.pdf") #, width = 50, height =60)
par(mfrow=c(6,4), oma = c(2,0,2,0) + 0.0)
par(mar=c(2, 0, 1, 0) + 0.1)
par(cex.axis=0.1)
par( mgp = c(1, 1, 0),
omd = c(1.0, 0, 1.0, 0),
pin=c(10,10),
plt = c(1,1,1,1),
usr =c(1,0,1,0)
)
for(z in 1:ncol(df_finale)){
...
corrplot(matriceTermine,
title = colnames(df_finale)[z],
xaxs = "i",
#insig = "pch",
#pch.col="blue",
#addrect=24, rect.lwd=10,
#addCoefasPercent=T,
method = "shade", # shade ellipse
order="original",
col=col1(11),
addgrid.col="grey",
addCoef.col="grey",
mar = c(0,0,0,0),
cl.cex= 0.2, cl.pos = "n", # 0.2
tl.pos = "n", tl.cex= 0.3, tl.offset = 0.9, tl.srt = 90,
#tl.pos="d")
}
dev.off()
It results:
Which parameters do I have to modify to make the titles visible?
And suppose I want to reduce the dimensions of the plots (to give extra-spaces to the titles), how can I do?
I have solved in this way, adding:
...
corrplot(matriceTermine,
#title = colnames(df_finale)[z],
...,
method = "s")
title(colnames(df_finale[z]), line = -0.1, cex = 2.5, font=3, cex.main = 2)
...
With
line = - q
it's possible to "move" the title of q.
For successive modifications I suggest to save the plot in a "svg" and modifying it with Inkscape.

R: How to add highlighted angle lines in polar plots?

Please consider the following sample polar plot:
library(plotrix)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0, 350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
I would like to add lines at angles 30 and 330 as well as 150 and 210 (from the center to the outside). I experimented with the line function but could not get it to work.
The calculations for exact placement are a bit goofy but using your test data
set.seed(15)
testlen<-c(rnorm(36)*2+5)
testpos<-seq(0,350,by=10)
polar.plot(testlen,testpos,main="Test Polar Plot",
lwd=3,line.col=4,rp.type="s")
You can add lines at 20,150,210,300 with
add.line <- c(30,330, 150,210)/360*2*pi
maxlength <- max(pretty(range(testlen)))-min(testlen)
segments(0, 0, cos(add.line) * maxlength, sin(add.line) * maxlength,
col = "red")
And that makes the following plot
You can just use the rp.type = "r" argument and add = TRUE. So, something like
library(plotrix)
set.seed(1)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0,350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
followed by
pos <- c(30, 330, 150, 210)
len <- c(10, 10, 10, 10)
polar.plot(lengths = len, polar.pos = pos,
radial.lim = c(0, 15),
lwd = 2, line.col = 2, rp.type = "r", add = TRUE)
yields your desired output.

labeling points on an archetype archmap

How might one add labels to an archmap from the archetypes package? Or alternatively, would it be possible to recreate the archmap output in ggplot?
Using code from the SportsAnalytics demo (I hope this isn't bad form)
library("SportsAnalytics")
library("archetypes")
data("NBAPlayerStatistics0910")
dat <- subset(NBAPlayerStatistics0910,
select = c(Team, Name, Position,
TotalMinutesPlayed, FieldGoalsMade))
mat <- as.matrix(subset(dat, select = c(TotalMinutesPlayed, FieldGoalsMade)))
a3 <- archetypes(mat, 3)
archmap(a3)
I'd like the player names ( NBAPlayerStatistics0910$Name ) over the points on the chart. Something like below but more readable.
If you don't mind tweaking things a bit, you can start with the archmap() function base, toss in an extra parameter and add a text() call:
amap2 <- function (object, a.names, projection = simplex_projection, projection_args = list(),
rotate = 0, cex = 1.5, col = 1, pch = 1, xlab = "", ylab = "",
axes = FALSE, asp = TRUE, ...)
{
stopifnot("archetypes" %in% class(object))
stopifnot(is.function(projection))
k <- object$k
if (k < 3) {
stop("Need at least 3 archetypes.\n")
}
cmds <- do.call(projection, c(list(parameters(object)), projection_args))
if (rotate != 0) {
a <- pi * rotate/180
A <- matrix(c(cos(a), -sin(a), sin(a), cos(a)), ncol = 2)
cmds <- cmds %*% A
}
hmds <- chull(cmds)
active <- 1:k %in% hmds
plot(cmds, type = "n", xlab = xlab, ylab = ylab, axes = axes,
asp = asp, ...)
points(coef(object) %*% cmds, col = col, pch = pch)
######################
# PLAY WITH THIS BIT #
######################
text(coef(object) %*% cmds, a.names, pos=4)
######################
rad <- ceiling(log10(k)) + 1.5
polygon(cmds[hmds, ])
points(cmds[active, ], pch = 21, cex = rad * cex, bg = "grey")
text(cmds[active, ], labels = (1:k)[active], cex = cex)
if (any(!active)) {
points(cmds[!active, , drop = FALSE], pch = 21, cex = rad *
cex, bg = "white", fg = "grey")
text(cmds[!active, , drop = FALSE], labels = (1:k)[!active],
cex = cex, col = "grey20")
}
invisible(cmds)
}
amap2(a3, dat$Name)
Obviously, my completely quick stab is not the end result you're looking for, but it should help you get on your way (if I read what you want to do correctly).

Resources