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

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.

Related

For-loop to dynamically change plot title

I have 5 variables which want to plot and export in one pdf. However, I have some trouble wiht the for-loop I am running,
parC <-list(unit = 100,labelx = "Time",labely = "Time",cols = "black",
pcex = .01, pch = 1,las = 1,
labax = seq(0,nrow(RP),100),
labay = seq(0,nrow(RP),100))
pdf("filename.pdf", onefile=TRUE)
for (i in RP_values){ # the values that are plotted
for (j in name) { # name is a list of names, so that the title changes dynamically
plotting(i, parC, j)
}
}
dev.off()
RP_values = list of values that is plotted
name = list of names to dynamically change the plotting title
plotting = an adjusted version from the plotRP() function of the crqa package. Here I added a main title to the plot.
The code for the plotting() function:
plotting <- function(RP, par, x){
if (exists("par") == FALSE){ # we use some defaults
## default values
unit = 2; labelx = "Time"; labely = "Time"
cols = "black"; pcex = .3; pch = 1; las = 0;
labax = seq(0, nrow(RP), unit); labay = seq(0, nrow(RP), unit);
} else { # we load the values that we desire
for (v in 1:length(par)) assign(names(par)[v], par[[v]])
}
xdim = nrow(RP)
ydim = ncol(RP)
RP = matrix(as.numeric(RP), nrow = xdim, ncol = ydim) # transform it for plotting
ind = which(RP == 1, arr.ind = T)
tstamp = seq(0, xdim, unit)
par(mar = c(5,5, 1, 3), font.axis = 2, cex.axis = 1,
font.lab = 2, cex.lab = 1.2)
plot(tstamp, tstamp, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = x)
matpoints(ind[,1], ind[,2], cex = pcex, col = cols, pch = pch)
mtext(labelx, at = mean(tstamp), side = 1, line = 2.2, cex = 1.2, font = 2)
mtext(labely, at = mean(tstamp), side = 2, line = 2.2, cex = 1.2, font = 2)
# if (is.numeric(labax)){ ## it means there is some default
# mtext(labax, at = seq(1, nrow(RP), nrow(RP)/10), side = 1, line = .5, cex = 1, font = 2)
# mtext(labay, at = seq(1, nrow(RP), nrow(RP)/10), side = 2, line = .5, cex = 1, font = 2)
# } else{
mtext(labax, at = tstamp, side = 1, line = .5, cex = .8, font = 2, las = las)
mtext(labay, at = tstamp, side = 2, line = .5, cex = .8, font = 2, las = las)
# }
}
My problem is instead of 5 plots I get 25, where each plot appears 5 times, but with a different title. If I do not include the "j" part everything works fine, but of course do not have any main title for each plot.
I appreciate any help.
Best,
Johnson
From your description and comments, it appears you need an elementwise loop and not a nested loop. Consider retrieving all pairwise combinations of names and RP_values with expand.grid and iterate through them with mapply. Also, since parC depends on nrows of corresponding RP, have parC defined inside function for only two parameters (with more informative names like title instead of x):
plotting <- function(RP, title) {
parC <- list(unit=100, labelx="Time", labely="Time",
cols="black", pcex=.01, pch=1, las=1,
labax=seq(0, nrow(RP), 100),
labay=seq(0, nrow(RP), 100))
...
plot(tstamp, tstamp, type="n", xlab="", ylab="",
xaxt="n", yaxt="n", main=title)
...
}
params <- expand.grid(RP_values=RP_values, name=name)
out <- mapply(plotting, RP=params$RP_values, title=params$name)

How to fit logarithmic curve over the points in r?

I want to fit my points with logarithmic curve. Here is my data which contains x and y. I desire to plot x and y and the add a logarithmic fitting curve.
x<-structure(list(X2.y = c(39.99724745, 29.55541525, 23.39578201,
15.46797044, 10.52063652, 7.296161198, 6.232038434, 4.811851132,
4.641281547, 4.198523289, 3.325515839, 2.596563723, 1.894902523,
1.556380314), X5.y = c(62.76037622, 48.54726084, 37.71302646,
24.93942365, 17.71060023, 13.31130267, 10.36341862, 7.706914722,
7.170517624, 6.294292013, 4.917428837, 3.767836298, 2.891519878,
2.280974128), X10.y = c(77.83154815, 61.12151516, 47.19228808,
31.21034981, 22.47098182, 17.29384973, 13.09875178, 9.623698726,
8.845091983, 7.681873268, 5.971413758, 4.543320659, 3.551367285,
2.760718282), X25.y = c(96.87401383, 77.00911883, 59.16936025,
39.13368164, 28.48573658, 22.32580849, 16.55485248, 12.0455604,
10.96092113, 9.435085861, 7.303126501, 5.523147205, 4.385086234,
3.366876291), X50.y = c(111.0008027, 88.79545082, 68.05463659,
45.01166182, 32.94782526, 26.05880295, 19.11878542, 13.84223574,
12.53056405, 10.73571912, 8.291067088, 6.25003851, 5.003586577,
3.81655893), X100.y = c(125.0232816, 100.4947544, 76.87430545,
50.84623991, 37.37696657, 29.76423356, 21.66378667, 15.6256447,
14.08861698, 12.0267487, 9.271712877, 6.971562563, 5.61752001,
4.262921183)), class = "data.frame", row.names = c(NA, -14L))
I tried this:
single_idf<-function(x) {
idf<-x
durations = c(5/60, 10/60, 15/60, 30/60, 1, 2, 3, 4, 5, 6, 8, 12, 18, 24)
nd = length(durations)
Tp = c(2, 5, 10, 25, 50, 100)
nTp = length(Tp)
psym = seq(1, nTp)
# open new window for this graph, set plotting parameters for a single graph panel
windows()
par(mfrow = c(1,1), mar = c(5, 5, 5, 5), cex = 1)
# set up custom axis labels and grid line locations
ytick = c(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,
200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400)
yticklab = as.character(ytick)
xgrid = c(5,6,7,8,9,10,15,20,30,40,50,60,120,180,240,300,360,
420,480,540,600,660,720,840,960,1080,1200,1320,1440)
xtick = c(5,10,15,20,30,60,120,180,240,300,360,480,720,1080,1440)
xticklab = c("5","10","15","20","30","60","2","3","4","5","6","8","12","18","24")
ymax1 = max(idf)
durations = durations*60
plot(durations, col=c("#FF00FF") ,lwd=c(1), idf[, 1],
xaxt="n",yaxt="n",
pch = psym[1], log = "xy",
xlim = c(4, 24*60), ylim = range(c(1,idf+150)),
xlab = "(min) Duration (hr)",
ylab = "Intensity (mm/hr)"
)
for (iT in 2:nTp) {
points(durations, idf[, iT], pch = psym[iT], col="#FF00FF",lwd=1)
}
for (iT in 1:nTp) {
mod.lm = lm(log10(idf[, iT]) ~ log10(durations))
b0 = mod.lm$coef[1]
b1 = mod.lm$coef[2]
yfit = log(10^(b0 + b1*log10(durations)))
lines(durations,col=c("#FF00FF"),yfit, lty = psym[iT],lwd=1)
}
}
But when I run this, the curves stands far away from the points. I want to see curves over the points. How can I arrange this?
single_idf(x)
Consider this as an option for you using ggplot2 and dplyr. Also added method='lm' to match OP expected output (Many thanks and credits to #AllanCameron for his magnificent advice):
library(ggplot2)
library(dplyr)
#Data
df <- data.frame(x,y)
#Plot
df %>%
pivot_longer(-y) %>%
ggplot(aes(x=log(y),y=log(value),color=name,group=name))+
geom_point()+
stat_smooth(geom = 'line',method = 'lm')
Output:
The main problem is that you were plotting the natural log of the fit rather than the fit itself.
If you change the line
yfit = log(10^(b0 + b1*log10(durations)))
To
yfit = 10^(b0 + b1*log10(durations))
And rerun your code, you get

R: Changing height/location of column labels

Using the following code, the column labels for my barplot overlap with the plot itself (see image). Changing the margin appears only to affect the axis label, but not the column labels. Did a search but couldn't find this question. Suggestions? Thank you! (P.S.--I'm a beginner!)
library(colorspace)
df <- matrix(c(20, 14, 26, 18, 14, 4, 19, 21, 13, 1, 5, 4), ncol = 4, byrow = TRUE)
rownames(df) <- c("Character", "Tree", "Distance")
colnames(df) <- c("nrITS", "trnH-\npsbA", "matK", "rbcL")
graph.dat <- as.table(df)
italic_latin2 <- c(expression(atop(italic("nrITS"), (104))),
expression(atop(italic("trnH-\npsbA"), (82))),
expression(atop(italic("matK"), (42))),
expression(atop(italic("rbcL"), (28))))
barplot(graph.dat, beside = TRUE, ylab = "Percent Identified",
xlab = "Locus", ylim = c(0, 30), col = rainbow_hcl(3),
names.arg = italic_latin2)
Look at where the ?barplot function arguments get sent by the ... parameter values. The ?axis page says there is a padj parameter to adjust vertical label positioning, so perhaps:
italic_latin2 <- expression( atop(italic("nrITS"), (104)),
atop(italic("trnH-\npsbA"), (82)),
atop(italic("matK"), (42)),
atop(italic("rbcL"), (28)) )
barplot(graph.dat, beside = TRUE, ylab = "Percent Identified",
xlab = "Locus", ylim = c(0, 30), col = rainbow_hcl(3),
names.arg = italic_latin2, padj=0.8)
Notice that I simplified the expression vector code as well. The arguments to expression are adequately separated by commas.

How do I create venn-diagram with item labels inside

I have these three clusters below. I want to create venn diagram with three clusters and showing their labels inside the diagram with proper size and spread so it looks beautiful. Below is the code I tried, but doesn't give what I wanted.
Clusters:
ClusterI<- c("HanXRQChr10g0293411T", "HanXRQChr09g0239551T", "HanXRQChr15g0489401R", "HanXRQChr02g0052061T", "HanXRQChr14g0430311N", "HanXRQChr15g0482661N", "HanXRQChr02g0046611R", "HanXRQChr02g0048181R", "HanXRQChr09g0260361N", "HanXRQChr08g0224171C", "HanXRQChr15g0489421R", "HanXRQChr03g0065841N", "HanXRQChr05g0129181R")
ClusterII<- c("HanXRQChr03g0082411N", "HanXRQChr13g0421521N", "HanXRQChr09g0240011N", "HanXRQChr11g0348661N", "HanXRQChr16g0505221N", "HanXRQChr15g0468571C", "HanXRQChr16g0522521T", "HanXRQChr10g0317141T", "HanXRQChr16g0520121T", "HanXRQChr13g0421611N", "HanXRQChr03g0077151T", "HanXRQChr15g0477941C", "HanXRQChr04g0103931T", "HanXRQChr04g0098561T", "HanXRQChr06g0183851T", "HanXRQChr09g0267021N", "HanXRQChr10g0279361N", "HanXRQChr06g0184181T", "HanXRQChr09g0240261N", "HanXRQChr03g0077061T", "HanXRQChr10g0279351N", "HanXRQChr02g0050681T", "HanXRQChr01g0016951T", "HanXRQChr13g0423781N", "HanXRQChr15g0478941C", "HanXRQChr09g0239991T", "HanXRQChr11g0320701N", "HanXRQChr04g0098511T", "HanXRQChr02g0037011N", "HanXRQChr13g0426201C", "HanXRQChr04g0117551T", "HanXRQChr09g0243851N", "HanXRQChr03g0079391N", "HanXRQChr09g0239281T", "HanXRQChr09g0241811T", "HanXRQChr04g0101181T", "HanXRQChr01g0029301C", "HanXRQChr08g0209681T", "HanXRQChr14g0453551N", "HanXRQChr05g0149501T", "HanXRQChr13g0397101N", "HanXRQChr13g0417981C", "HanXRQChr10g0316961N")
ClusterIII <- c("HanXRQChr03g0065091T", "HanXRQChr01g0016931T", "HanXRQChr17g0550881C", "HanXRQChr03g0064011T", "HanXRQChr09g0239211T", "HanXRQChr06g0183841T", "HanXRQChr04g0095771T", "HanXRQChr09g0240621T", "HanXRQChr12g0374601C", "HanXRQChr14g0430731R", "HanXRQChr10g0298171T", "HanXRQChr08g0211081T", "HanXRQChr02g0050711T", "HanXRQChr12g0361091T", "HanXRQChr06g0175651N")
code:
v2 <- venn.diagram(list(ClusterI=ClusterI, ClusterII=ClusterII,ClusterIII=ClusterIII),
fill = c("red", "blue","green"),
alpha = c(0.5, 0.5, 0.5), cat.cex = 1.5, cex=0.25,
filename=NULL)
# have a look at the default plot
grid.newpage()
grid.draw(v2)
v2[[7]]$label <- paste(setdiff(ClusterI, ClusterII), collapse="\n")
# in ClusterII only
v2[[8]]$label <- paste(setdiff(ClusterII, ClusterI) , collapse="\n")
# intesection ClusterI and ClusterII
v2[[9]]$label <- paste(intersect(ClusterI, ClusterII), collapse="\n")
# fora: out
v2[[10]]$label <- paste(ClusterIII, collapse="\n")
grid.newpage()
grid.draw(v2)
Try this. It appears there is no overlap among three clusters.
library(VennDiagram)
ClusterI<- c("HanXRQChr10g0293411T", "HanXRQChr09g0239551T", "HanXRQChr15g0489401R", "HanXRQChr02g0052061T", "HanXRQChr14g0430311N", "HanXRQChr15g0482661N", "HanXRQChr02g0046611R", "HanXRQChr02g0048181R", "HanXRQChr09g0260361N", "HanXRQChr08g0224171C", "HanXRQChr15g0489421R", "HanXRQChr03g0065841N", "HanXRQChr05g0129181R")
ClusterII<- c("HanXRQChr03g0082411N", "HanXRQChr13g0421521N", "HanXRQChr09g0240011N", "HanXRQChr11g0348661N", "HanXRQChr16g0505221N", "HanXRQChr15g0468571C", "HanXRQChr16g0522521T", "HanXRQChr10g0317141T", "HanXRQChr16g0520121T", "HanXRQChr13g0421611N", "HanXRQChr03g0077151T", "HanXRQChr15g0477941C", "HanXRQChr04g0103931T", "HanXRQChr04g0098561T", "HanXRQChr06g0183851T", "HanXRQChr09g0267021N", "HanXRQChr10g0279361N", "HanXRQChr06g0184181T", "HanXRQChr09g0240261N", "HanXRQChr03g0077061T", "HanXRQChr10g0279351N", "HanXRQChr02g0050681T", "HanXRQChr01g0016951T", "HanXRQChr13g0423781N", "HanXRQChr15g0478941C", "HanXRQChr09g0239991T", "HanXRQChr11g0320701N", "HanXRQChr04g0098511T", "HanXRQChr02g0037011N", "HanXRQChr13g0426201C", "HanXRQChr04g0117551T", "HanXRQChr09g0243851N", "HanXRQChr03g0079391N", "HanXRQChr09g0239281T", "HanXRQChr09g0241811T", "HanXRQChr04g0101181T", "HanXRQChr01g0029301C", "HanXRQChr08g0209681T", "HanXRQChr14g0453551N", "HanXRQChr05g0149501T", "HanXRQChr13g0397101N", "HanXRQChr13g0417981C", "HanXRQChr10g0316961N")
ClusterIII <- c("HanXRQChr03g0065091T", "HanXRQChr01g0016931T", "HanXRQChr17g0550881C", "HanXRQChr03g0064011T", "HanXRQChr09g0239211T", "HanXRQChr06g0183841T", "HanXRQChr04g0095771T", "HanXRQChr09g0240621T", "HanXRQChr12g0374601C", "HanXRQChr14g0430731R", "HanXRQChr10g0298171T", "HanXRQChr08g0211081T", "HanXRQChr02g0050711T", "HanXRQChr12g0361091T", "HanXRQChr06g0175651N")
v2 <- draw.triple.venn(
area1 = 60,
area2 = 60,
area3 = 60,
n12 = 20,
n23 = 10,
n13 = 15,
n123 = 5,
cex = 0.25,
cat.cex = 1.5,
alpha = c(0.5, 0.5, 0.5),
category = c("ClusterI", "ClusterII", "ClusterIII"),
fill = c("red", "blue","green")
)
overlaps <- calculate.overlap(list("ClusterI"=ClusterI, "ClusterII"=ClusterII, "ClusterIII"=ClusterIII))
overlaps
indx <- as.numeric(substr(names(overlaps),2,2))
for (i in 1:length(overlaps)){
v2[[6+indx[i] ]]$label <- paste(overlaps[[i]], collapse = "\n")
}
grid.newpage()
grid.draw(v2)
Good luck!

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