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
Related
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)
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)
I am trying to construct a 3d-plot with grid lines using the function prespbox() and adding an additional plot of a figure based on some data using the surf3d() function.
This is my code:
library(plot3D) # added by comment
perspbox (x = seq(0.05, 0.5, length.out = nrow(M$x)),
y = seq(0.05, 0.5, length.out = ncol(M$y)), z,
bty = "u",
col.axis = "black", col.panel = "white", lwd.panel = 1,
col.grid = "grey", lwd.grid = 1, las=1,
phi = 40, theta = 40, col = NULL,ps=30,
nticks=4, ticktype="detailed", xlab="\n\n h1", ylab="\n\n h2", zlab="\n\n Fund",
colkey = TRUE,cex.axis=0.5, plot = TRUE)
surf3D(M$x, M$y, z, colvar = z, colkey = TRUE,add=TRUE)
My problem is, that the tick lines at the axis are too long.
Do you have an idea, how I could get rid of the tick lines? I want to keep the axis labels, but completely remove the tick lines. I already tried the tck option, but it seems not to have any effect here.
I am trying to plot rose diagrams/ circular histograms on specific coordinates on a map analogous to drawing pie charts on a map as in the package mapplots.
Below is an example generated with mapplots (see below for code), I'd like to replace the pie charts with rose diagrams
The package circular lets me plot the rose diagrams, but I am unable to integrate it with the mapplots package. Any suggestions for alternative packages or code to achieve this?
In response to the question for the code to make the map. It's all based on the mapplots package. I downloaded a shapefile for the map (I think from http://www.freegisdata.org/)
library(mapplots)
library(shapefiles)
xlim = c(-180, 180)
ylim = c(-90, 90)
#load shapefile
wmap = read.shapefile ("xxx")
# define x,y,z for pies
x <- c(-100, 100)
y <- c(50, -50)
z1 <- c(0.25, 0.25, 0.5)
z2 <- c(0.5, 0.2, 0.3)
z <- rbind(z1,z2)
# define radii of the pies
r <- c(5, 10)
# it's easier to have all data in a single df
plot(NA, xlim = xlim, ylim = ylim, cex = 0.75, xlab = NA, ylab = NA)
draw.shape(wmap, col = "grey", border = "NA")
draw.pie(x,y,z,radius = r, col=c("blue", "yellow", "red"))
legend.pie (x = -160, y = -70, labels = c("0", "1", "2"), radius = 5,
bty = "n", cex = 0.5, label.dist=1.5, col = c("blue", "yellow", "red"))
the legend for the pie size can then be added using legend.bubble
Have a look at this example, you can use the map as background an plot your rose diagrams withPlotrix or ggplot2. In either case you would want to overlay multiple of these diagrams on top of your map which is easy to do in ggplot, just have a look at the example.
I discovered subplot() in the package Hmisc, which seems to do exactly what I wanted. Below is my solution (without the map in the background, which can be plotted using mapplots). I am open to suggestions on how to improve this though...
library(Hmisc)
library (circular)
dat <- data.frame(replicate(2,sample(0:360,10,rep=TRUE)))
lat <- c(50, -40)
lon <- c(-100, 20)
# convert to class circular
cir.dat <- as.circular (dat, type ='angles', units = 'degrees', template = 'geographic', modulo = 'asis', zero = 'pi/2', rotation = 'clock')
# function for subplot, plots relative frequencies, see rose.diag for how to adjust the plot
sub.rose <- function(x){
nu <- sum(!is.na(x))
de <- max(hist(x, breaks = (seq(0, 360, 30)), plot = FALSE)$counts)
prop <- nu/de
rose.diag(x, bins = 12, ticks = FALSE, axes = FALSE,
radii.scale = 'linear',
border = NA,
prop = prop,
col = 'black'
)
}
plot(NA, xlim = xlim, ylim = ylim)
for(i in 1:length(lat)){
subplot(sub.rose(cir.dat[,i]), x = lon[i], y = lat[i], size = c(1, 1))
}
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)