Drawing points in phylogenetic tree R - r

I would like to draw only one point on the fist node, and then draw another one in the first tip. So far I could draw points but only all of them at once and I cannot find the way to draw it separately. What I have so far:
library(ape)
t3 = '((a:1,b:1):1,(c:1.5,d:0.5):0.5):1;'
plot(read.tree(text = t3),root.edge=T)
nodelabels(pch=21, col="black", adj=1, bg='blue', cex=2)
any help is appreciated

This isn't the exact answer, but it should help. I got this by looking at the code for nodelabels function.
library(ape)
t3 = '((a:1,b:1):1,(c:1.5,d:0.5):0.5):1;'
plot(read.tree(text = t3),root.edge=T)
lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
node <- (lastPP$Ntip + 1):length(lastPP$xx)
XX <- lastPP$xx[node]
YY <- lastPP$yy[node]
BOTHlabels(text="", node, XX[1], YY[1], adj = c(0.5, 0.5),
frame = "rect", pch = 21, thermo = NULL, pie = NULL,
piecol = NULL, col = "blue", bg = "blue",
horiz = FALSE, width = NULL, height = NULL, cex=2)
The XX's and YY's gives the nodes. Here, I'm using only the first one. What you have to do for tips is similar, too. Have a look at the code for tiplabels.

You can define to which node you want to plot with the "node" argument within the nodelabels function, and the same is true for tiplabels function but with "tip" argument.
So:
library(ape)
t3 = '((a:1,b:1):1,(c:1.5,d:0.5):0.5):1;'
tree <- read.tree(text = t3)
first_node <- length(tree$tip.label)+1
plot(tree, root.edge=T)
nodelabels(node = first_node, pch=21, col="black", bg='blue', cex=2)
tiplabels(tip = 1, pch=21, col="black", bg='blue', cex=2)

Related

spplot legend with point rather than with scale

I am creating a plot based on INLA predictions. Everything works perfectly for the modelling, but for the plot,the legend on the graph are points (like https://gis.stackexchange.com/questions/350918/how-do-i-reverse-spplot-colour-key-so-the-values-are-decreasing) rather than a scale (like here http://www.nickeubank.com/wp-content/uploads/2015/10/RGIS3_MakingMaps_part1_mappingVectorData.html):
And here is the code that I would like to change. I guess there is a problem of factor (R spplot: colorbar rather than legend for SpatialPoint data, spplot issue with legend range and colors distribution) but I cannot understand how/what to change:
m_grid <- inla(formWITHOUT, data = inla.stack.data(region.stack.predGrid, spde = inlaSetting$region.spde),
family = "gamma",
control.predictor = list(A = inla.stack.A(region.stack.predGrid), compute = TRUE, link=1),
control.compute = list(cpo = TRUE, dic = TRUE),
control.family=list(link="default"))
summary(m_grid)
index.pred <- inla.stack.index(region.stack.predGrid, "region.pred")$data
region.grid_sf$Sbiomass <- m_grid$summary.fitted.values[index.pred, "mean"]
region.grid_sf$Sbiomass_sd <- m_grid$summary.fitted.values[index.pred, "sd"]
my.palette <- brewer.pal(n = 7, name = "OrRd")
par(mar = c(0,0,0,0))
spplot(region.grid_sf, c("Sbiomass"), col.regions = my.palette, col = "transparent")
Thanks in advance for any tips !
I finally found the answer:
cuts <- c(0,5000,10000,15000,20000,25000,30000)
spplot(region.grid_sf,
c("Sbiomass"),
col.regions = my.palette,
col = "transparent",
key.space = list(x=0.1,y=0.3),
colorkey =T,
cuts = cuts,
cex = 2,
pch = 22)

R: Combining several lines and points in one polar plot

I have data from several sources describing an y value in a 360 degrees space but I cannot plot them together with a fitted spline on a single polar plot.
Here's some simulated data:
# Data for test
set.seed(35)
sim1 <- cbind(rnorm(6,0),seq(0,359,359/5))
sim2 <- cbind(rnorm(9,0),seq(0,359,359/8))
sim3 <- cbind(rnorm(7,0),seq(0,359,359/6))
If not doing a polar plot my procedure would be as follows:
# Create spline for points
total <- rbind(sim1,sim2,sim3)
fit= smooth.spline(total[,2],total[,1], cv=T)
# Classic solution if not polar plot
plot(sim1[,2],sim1[,1],ylim = c(-3,4), col = "darkgrey")
lines(sim1[,2],sim1[,1], pch=2, col = "darkgrey")
points(sim2[,2],sim2[,1], pch=2, col = "darkgrey")
lines(sim2[,2],sim2[,1], pch=2, col = "darkgrey")
points(sim3[,2],sim3[,1], pch=2, col = "darkgrey")
lines(sim3[,2],sim3[,1], pch=2, col = "darkgrey")
lines(fit, , col = "red")
Which would give me this kind of figure:
Plot
But trying to plot it in a polar plot. I cannot get further than plotting each individually:
# Plot
library(plotrix)
polar.plot(sim1[,1],sim1[,2],lwd=3,line.col="red", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
polar.plot(sim2[,1],sim2[,2],lwd=3,line.col="blue", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
polar.plot(sim3[,1],sim3[,2],lwd=3,line.col="darkgrey", radial.lim=c(-3,3),clockwise=TRUE,rp.type = "s")
Poor plot but 360
I have also tried using ggplot2 as well as plotly but nothing yielded what I was hoping for.
Use the add parameter to add lines. Perhaps something like this?
polar.plot(sim1[,1], sim1[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p")
polar.plot(sim2[,1], sim2[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(sim3[,1], sim3[,2], lwd=1, line.col = "grey20", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
polar.plot(fit$y, fit$x, lwd=2, line.col = "firebrick", radial.lim = c(-3,3),
clockwise = TRUE, rp.type = "p", add = TRUE)
GGplot alternative:
library(ggplot2,ggthemes)
# Data for test
set.seed(35)
sim1 <- cbind(rnorm(6,0),seq(0,359,359/5))
sim2 <- cbind(rnorm(9,0),seq(0,359,359/8))
sim3 <- cbind(rnorm(7,0),seq(0,359,359/6))
# Create spline for points
total <- rbind(sim1,sim2,sim3)
colnames(total)=c('Col1','Col2')
total=as.data.frame(total)
MyNames=c(rep('sim1',nrow(sim1)),rep('sim2',nrow(sim2)),rep('sim3',nrow(sim3)))
total=cbind(MyNames,total)
Radial=ggplot(total)+
theme_light()+
geom_line(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames),alpha=0.4)+
geom_point(aes(x=Col2,y=Col1,group=MyNames,colour=MyNames))+
geom_line(aes(x=Col2,y=Col1),stat='smooth', method = "loess", span=0.5, alpha=0.4, size=1.2)+
scale_x_continuous(breaks=seq(0,360,by=60),expand=c(0,0),lim=c(0,360))+
coord_polar(theta='x',start=0)+
ggtitle('Sim')+
theme(axis.text=element_text(size=14),axis.title=element_text(size=16,face="bold"),legend.text=element_text(size=14),legend.title=element_text(size=14),title=element_text(size=16,face="bold"),plot.title = element_text(hjust = 0.5))
Radial

How to remove empty parts of the plot in R?

I have a question regarding R plot.
The problem is that I have my graph in the centre of the plot and would like to remove unnecessary empty space around it.
Setting limits to the axes did not help.
my R code is below:
visualize_circle_arrows <- function(x) {
colors<- unlist(lapply(x$MeanOffsetLength,get_grey_color, max(x$MeanOffsetLength), min(x$MeanOffsetLength)), recursive = TRUE, use.names = TRUE)
library(graphics)
plot(x$IconCenterX, -x$IconCenterY, xlim=c(0, 1080), ylim=c(-1920, 0), asp=1, cex = .05, col = "blue", main="Offset vector for Thumb in warm",
xlab="X coordinates of the screen, px", ylab="Y coordinates of the screen, px")
radius <- rep(135,times=nrow(x))
symbols(x$IconCenterX, -x$IconCenterY, circles=radius, add=TRUE, inches=FALSE, bg=colors)
arrows(x$IconCenterX, -x$IconCenterY, x1 = x$MeanTouchX, y1 = -x$MeanTouchY, length = 0.04, angle = 25,
code = 2, col = "black", lty = par("lty"),
lwd = 2)
}
get_grey_color <- function(x,max=135, min=0) {
value <- ((x+10-min)/(max+10-min))
rgb(value, value, value)
}
visualize_circle_arrows(warm_thumb)
and the result of it is this graph below
my graph
Any help will be very much appreciated!
Thanks a lot!
p.s. snapshot of warmthumb object is here
warmthumb object

Change plot area background color

I would like to do a graph in R using our company colors. This means the background of all charts should be a light blue, the plotting region however should be white. I was searching for answers and found that drawing a rect does the job (almost). However the plotting region is now white and the graph not visible anymore. Is this even possible?
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)
GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_ORANGE<-rgb(243/255, 112/255, 33/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)
par(bg=GRAPH_BACKGROUND)
colorPlottingBackground<-function(PlottingBackgroundColor = "white"){
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="white")
}
plot.xts(SPY, col=GRAPH_BLUE)
colorPlottingBackground()
I know you already accepted #plannapus's answer, but this is a much simpler solution
par(bg="lightblue")
plot(0, 0, type="n", ann=FALSE, axes=FALSE)
u <- par("usr") # The coordinates of the plot area
rect(u[1], u[3], u[2], u[4], col="white", border=NA)
par(new=TRUE)
plot(1:10, cumsum(rnorm(10)))
What you basically do is to overlay two plots using par(new=TRUE): one with only a white rectangle; and another one with the contents you actually want to plot.
The issue is that you plot your white rectangle after plotting your data, therefore overwriting them. Since plot.xts doesn't have an argument add that would allow you to call it after drawing the rectangle, the only solution I see would be to modify function plot.xts.
plot.xtsMODIFIED<-function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto",
minor.ticks = TRUE, major.format = TRUE, bar.col = "grey",
candle.col = "white", ann = TRUE, axes = TRUE, ...)
{
series.title <- deparse(substitute(x))
ep <- axTicksByTime(x, major.ticks, format.labels = major.format)
otype <- type
if (is.OHLC(x) && type %in% c("candles", "bars")) {
x <- x[, has.OHLC(x, TRUE)]
xycoords <- list(x = .index(x), y = seq(min(x), max(x),
length.out = NROW(x)))
type <- "n"
}
else {
if (NCOL(x) > 1)
warning("only the univariate series will be plotted")
if (is.null(y))
xycoords <- xy.coords(.index(x), x[, 1])
}
###The next three lines are the only modifications i made to the function####
plot(xycoords$x, xycoords$y, type = "n", axes = FALSE, ann = FALSE)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="white")
if(type=="l"){lines(xycoords$x, xycoords$y, ...)}
if (auto.grid) {
abline(v = xycoords$x[ep], col = "grey", lty = 4)
grid(NA, NULL)
}
if (is.OHLC(x) && otype == "candles")
plot.ohlc.candles(x, bar.col = bar.col, candle.col = candle.col,
...)
dots <- list(...)
if (axes) {
if (minor.ticks)
axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB",
...)
axis(1, at = xycoords$x[ep], labels = names(ep), las = 1,
lwd = 1, mgp = c(3, 2, 0), ...)
axis(2, ...)
}
box()
if (!"main" %in% names(dots))
title(main = series.title)
do.call("title", list(...))
assign(".plot.xts", recordPlot(), .GlobalEnv)
}
Then your script become:
library(quantmod)
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)
GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)
par(bg=GRAPH_BACKGROUND)
plot.xtsMODIFIED(SPY, col=GRAPH_BLUE)
The error you're getting (Error in axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...) : formal argument "col" matched by multiple actual arguments.) was also thrown with your previous script. It has to do with the fact that plot.xts uses several time argument ... and that argument col is both valid for axis and plot(or here in my modified version, lines). If you want to avoid it, i see two solutions:
Either you want your axis to be of the same color as your line and therefore you have to change the line that says:
...
axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB",
...)
...
Into
...
axis(1, at = xycoords$x, labels = FALSE, ...)
...
Or you want the axis to have the color intended by the writer of the original plot.xts in which case you need to differenciate the color of the lines and that of the axis.
plot.xtsMODIFIED<-function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto",
minor.ticks = TRUE, major.format = TRUE, bar.col = "grey",
candle.col = "white", ann = TRUE, axes = TRUE,
lcol, ...)
{
...
if(type=="l"){lines(xycoords$x, xycoords$y, lcol, ...)}
...
}
And then in your actual call:
plot.xtsMODIFIED(SPY, lcol=GRAPH_BLUE)
plot.xts will accept the panel.first argument, which is another way to draw the rectangle before plotting the line.
library(quantmod)
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)
GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)
par(bg=GRAPH_BACKGROUND)
white.rect=function() do.call(rect,as.list(c(par()$usr[c(1,3,2,4)],col="white")))
plot.xts(SPY,panel.first=white.rect())
This does not address the issue with col=GRAPH_BLUE pointed out by #plannapus.

Modifying width of outline in a pie chart in R--what is the equivalent of lwd parameter for pie()?

I'm using base R plotting functions to produce a pie chart and I want to change the line thickness of the outlines of each pie segment. ?pie seems to indicate that I can add optional graphic parameters, but adding lwd= does not appear to work. Anyone have any clues as to how I might be able to do this. I'm not yet proficient in producing pie charts in ggplot, and would like to stick with base R plotting (if possible).
library(RColorBrewer)
x1 <- data.frame(V1 = c(200, 100)) ## generate data
row.names(x1) <- c("A", "B")
x1$pct <- round((x1$V1/sum(x1$V1))*100, 1)
lbls1 <- paste(row.names(x1), "-(",x1$pct, '%)', sep='') ## add some informative stuff
pie(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1, lwd= 3)
Notice lwd= does not increase line thickness like it would in other base plotting.
Anyone have any clues?
The call to polygon and lines within pie does not pass ... or lwd
...
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i])
P <- t2xy(mean(x[i + 0:1]))
lab <- as.character(labels[i])
if (!is.na(lab) && nzchar(lab)) {
lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
....
You can get around this by setting par(lwd = 2) (or whatever) outside and prior to your call to pie
i.e.
# save original settings
opar <- par(no.readonly = TRUE)
par(lwd = 2)
pie(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1)
par(lwd = 3)
# reset to original
par(opar)
At the moment, the function inside pie that does the actual drawing is polygon and here is how it is called:
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i])
Notice there is no lwd argument and more critically no ... argument to accept arguments that might not have been hard coded.
Create a new pie2 function. First type pie, copy the code and make a few changes:
pie2 <-
function (x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE,
init.angle = if (clockwise) 90 else 0, density = NULL, angle = 45,
col = NULL, border = NULL, lty = NULL, main = NULL, lwd=1,...)
{
................
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i], lwd=lwd )
.................
}
pie2(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1, lwd=5)

Resources