I made a plot with legend. By this code:
scatter3d(x = red, y = green, z = blue, groups = C1class$V1, surface. col = 1:21,
grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(C1class$V1),
col = rainbow(21), pch = 16, inset = -0.25, xpd = TRUE)
But my graph looks like this:
How can I edit it to look better?
Can you help me with some function to fix it?
Thanks for you help.
You do not provide your data, so I cannot test your problem exactly,
but I think that the example below will help you solve your problem.
I think that your problem stems from including the parameter inset = -0.25.
That is shifting the legend out of the display area. Looking at your picture,
I think that the reason that you did that is because otherwise the
legend covers over part of the scatterplot. A different way to fix this is to adjust the drawing area using windowRect to allow more space.
A plot like yours with the legend chopped off.
scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5],
surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
col = rainbow(3), inset = -0.25, pch = 16, xpd = TRUE)
A second plot without the indent. Now the legend overlaps the plot.
scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5],
surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
col = rainbow(3), pch = 16, xpd = TRUE)
Finally, the window size is adjusted to allow more room for the legend.
par3d(windowRect = c(100, 100, 600, 350))
scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5],
surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
col = rainbow(3), pch = 16, xpd = TRUE)
You may need to adjust the windowRec differently for your data, but this setting will be a good place to start.
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 have tried them all but I'm sure I'm doing something wrong. I just want to add a title, xlabel, ylabel, zlabel. I would also like to set 5 values to create the idea of what are the values (something that automatically appeared on bbox (but I don't want to use that, I'm happy with the transparent box).
Here the code I'm using and what I've tried so far (they all appear at the bottom and white:
rgl.viewpoint(theta = 0, phi = 0, fov = 60, zoom = 0.7)
rgl.bg(color = "white")
box3d()
axes3d(edges = "bbox", labels = TRUE, tick = TRUE, nticks = 5, box = FALSE, expand = 1.03, specular = "red")
title3d(main= "3D cluster visualization", xlab="Annual Income", ylab="Spending Score", zlab="Age", line = 10, outer = TRUE)
Add bgplot3d() to your code. It allows you to put base-R plotting features onto an rgl 3D plot. You'll have to play with the settings to make if fit your needs.
require(rgl)
rgl.viewpoint(theta = 0, phi = 0, fov = 60, zoom = 0.7)
rgl.bg(color = "white")
plot3d(x=1:4, y=1:4, z=1:4, type='s')
axes3d(edges = "bbox", labels = TRUE, tick = TRUE, nticks = 5, box = FALSE, expand = 1.03, specular = "red")
bgplot3d({
plot.new()
title(main = 'This is the main title', line = 2)
mtext(side = 1, 'Here is my subtitle', line = 5)
})
I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.
I am using a function which plots two graphs on the same picture. I want to add threshold line to the first graph and a different threshold on the second graph. I am using abline() function to do so. chr6 comes with a library as an example.
install.packages("GenWin")
library(GenWin)
chrom_num = 6
jpeg(filename = paste(chrom_num, ".jpg", sep=""), width = 1200, height = 800)
chr = splineAnalyze(chr6$Fst, chr6$Position, plotRaw = 1, plotWindows = 1, method = 4)
abline(0.3, 0, col = "green")
abline(6, 0, col = "green")
Both threshold lines show up on the second graph. How to prevent this? In other word is there a way to direct to which graph I am adding something?
If you want to add something, I think it would be better to make graphs by yourself from the analyzed data, chr (almost all code is picked out from splineAnalyze). This approach would enable you to customize the graph.
analyzed_data <- chr # All you need to do is changing these lines and data and col names of 1st plot()).
smoothness <- 100 # default value
jpeg(filename = paste("file_name", ".jpg", sep=""), width = 1200, height = 800)
par(mfrow = c(2,1))
# 1st graph
plot(Fst ~ Position, chr6, xlab = "Position (bp)", ylab = "Raw values")
with(analyzed_data,
lines(x = seq(0, max(rawSpline$x), by = smoothness),
y = predict(rawSpline, seq(0, max(rawSpline$x), by = smoothness)), col = "red")
)
abline(0.3, 0, col = "green")
# 2nd graph
with(analyzed_data,
plot(x = (windowData$WindowStop - windowData$WindowStart)/2 + windowData$WindowStart,
y = windowData$Wstat, xlab = "Position (bp)", ylab = "Spline Wstat", pch = 19)
)
abline(6, 0, col = "green")
dev.off()
Of course, you can do it using splineAnalyze(..., plotRaw = 1, plotWindows = 1, ...) and adding the lines.
jpeg(filename = paste("file_name2", ".jpg", sep=""), width = 1200, height = 800)
chr = splineAnalyze(chr6$Fst, chr6$Position, plotRaw = 1, plotWindows = 1, method = 4)
abline(6, 0, col = "green") # draw on 2nd panel
layout(matrix(c(2,1), ncol = 1)) # refocus 1st panel
par(new = T)
plot(Fst ~ Position, chr6, ann = F, type = "n", axes = F) # reproduce the coordinates
abline(0.3, 0, col = "green") # draw on 1st panel
dev.off()
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.