I am doing the same scatter plots in 2D and 3D with ggplot2 and plot3d. I always like to do coord_fixed() in ggplot2 scatter plots when possible, for better readability. Is there a way to do the same in the scatter3D plot?
MWE:
data(iris)
head(iris)
library(ggplot2)
ggplot(iris, aes(x=Petal.Length, y=Petal.Width)) +
geom_point(pch=16) + theme_light() + coord_fixed()
library(plot3D)
scatter3D(iris$Petal.Length, iris$Sepal.Length, iris$Petal.Width, bty = "u", pch = 16, alpha = 0.5,
xlab = "Petal.Length", ylab = "Sepal.Length", zlab = "Petal.Width", phi = 0, theta = 40,
col.panel = "white", col.grid = "gray", col="black", ticktype = "detailed")
scale = FALSE does this:
scatter3D(iris$Petal.Length, iris$Sepal.Length, iris$Petal.Width, bty = "u", pch = 16, alpha = 0.5,
xlab = "Petal.Length", ylab = "Sepal.Length", zlab = "Petal.Width", phi = 0, theta = 40,
col.panel = "white", col.grid = "gray", col="black", ticktype = "detailed",
scale = FALSE)
From ?persp:
If scale is TRUE the x, y and z coordinates are transformed separately. If scale is FALSE the coordinates are scaled so that aspect ratios are retained
Related
I want to enlarge the size of the axis labeling.
par(mar=c(1,1,1,1))
scatter3D(x = Eplot_sand.mean$OM, y = Eplot_sand.mean$rho_B, z = Eplot_sand.mean$Pv.mean,
phi = 20, theta = 35, bty = "b2", type = "h",alpha = 0.8, clim =c(0,150),
ticktype = "detailed", pch = 19, cex = 1.2, clab = "Pv (kPa)",
xlab = "OM (%)",
revolutions=100,
zlab = "Pv (kPa)",
ylab = "bulk density (g cm-³)",
bg.col="black",
sphere.size=100,
xlim = c(0,5),
ylim = c(1,2),
zlim = c(0,150),
col = ramp.col(c("blue", "green","orange", "red")))
Thanks in advance!
Similarly to the limit command in your function (xlim etc), you can do a cex command for your label indicating the size of text be scaled relative to the default:
scatter3D(...,
cex.lab=par("cex.lab"))
This is also slightly documented here:
https://www.rdocumentation.org/packages/scatterplot3d/versions/0.3-41/topics/scatterplot3d
I am trying to create this stratified histogram on R, however I am not getting the right plot. I would like to use the rect function as well if possible.
How would I write an R function to create this stratified histogram using the iris dataset in R?
This is the code so far:
strathist = function(x, y, ylab = "Frequency", xlab = "", main = ""){
cols = hcl(h=seq(0, 360, by = 120))
h = hist(x, breaks = 24, plot = FALSE)
tb = table(y, cut(x, h$breaks))
plot.new()
barplot(tb, ylim = c(0, max(h$count)), col = cols,
ylab = ylab, xlab = xlab, main = main, axisnames = FALSE)
box()
axis(1, 0:(length(h$breaks)-1), h$breaks)
axis(2)
legend("topright", c(rownames(tb)), fill = cols)
}
with(iris, strathist(Sepal.Width, Species, xlab = "Sepal.Width", main = "Stratified Histogram"))
There are a few different ways to do this, if you insist on using R base, you could use two barplots, one to add the colors and one to add the boxes:
strathist = function(x,
y,
ylab = "Frequency",
xlab = "",
main = "") {
cols = hcl(h = seq(0, 360, by = 120))
h = hist(x,
breaks = 24,
plot = F)
tb = table(y, cut(x, h$breaks))
ylim <- c(-.05 * max(colSums(tb)),
1.25 * max(colSums(tb)))
barplot(
tb,
col = cols,
ylim = ylim,
ylab = ylab,
xlab = xlab,
main = main,
axisnames = FALSE,
border = NA,
space = 0
)
barplot(
colSums(tb),
ylim = ylim,
col = NA,
ylab = ylab,
xlab = xlab,
main = main,
axisnames = FALSE,
add = T,
space = 0
)
box()
axis(1, seq(0,
length(h$breaks) - 1,
by = 5),
seq(min(x),max(x), by = .5))
axis(2)
legend("topright", c(rownames(tb)), fill = cols,border = NA,)
}
with(
iris,
strathist(Sepal.Width, Species, xlab = "Sepal.Width", main = "Stratified Histogram")
)
Or you could go the somewhat easier route to use ggplot2:
library(ggplot2)
ggplot(iris,aes(x = Sepal.Width)) +
geom_histogram(bins = 25,aes(fill = Species)) +
geom_histogram(bins = 25, fill = NA, color = 'black') +
theme_minimal()
Created on 2020-09-13 by the reprex package (v0.3.0)
I tried change point color to black in scatter3d plot using R, but failed.
It is difficult to view data because the color of the point and the surface color overlap.
Could i get some ideas?
# scatter plot with regression plane
scatter3D(x, y, z, pch = 16, cex = 1, alpha.col = 0.8, color="black", col = ramp.col(c("dark green", "khaki", "dark red")), bty="b2",
theta = 595, phi = 35, ticktype = "detailed", d=200,
xlab = "", ylab = "", zlab = "",
surf = list(x = x.pred, y = y.pred, z = z.pred,
facets=T, border="black"), main = "Room A")
Thank you!
You can follow your command with points3D and include add=TRUE.
# scatter plot with regression plane
scatter3D(x, y, z,
col = ramp.col(c("dark green", "khaki", "dark red")), bty="b2",
theta = 595, phi = 35, ticktype = "detailed", d=200,
xlab = "", ylab = "", zlab = "",
surf = list(x = x.pred, y = y.pred, z = z.pred,
facets=T, border="black"), main = "Room A")
points3D(x, y, z, pch = 16, color="black", alpha = 0.8, add=TRUE)
I have the following code:
require(lattice)
data <- data.frame(x = c(1,1,2,3,4,5), y = c(5,1,4,6,1,7),
color = c("red", "red", "black", "blue", "yellow", "red"))
data$size <- data$x*data$y*0.2
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6))
As you can see, this gives scatter plot with conditional sizing of circles. What I want to do is to fill the circles by condition as well (coloring provided by data$color). I tried to use fill option, but it fails to give me what I want. I will appreciate any suggestions on how to accomplish that.
We can set the pch argument to be solid circles.
library(lattice)
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6),
col = data$color, pch = 19)
i am writing up a function to generate a stratified histogram.
Here is my code.
i would like to remove the inside border (so those stack counts are separated by colour only, not colour and border). any ideas?
data("iris")
strathist = function(x, y, ylab = "Frequency", xlab = "", main = ""){
cols = hcl(h=seq(0, 300, by = 50), fixup = FALSE)
h = hist(x, breaks = 20, plot = F)
tb = table(y, cut(x, h$breaks))
par(mar = rep(4, 4))
plot.new()
barplot(tb, space = 0, ylim = c(-0.4, 2 + max(h$count)), col = cols,
ylab = ylab, xlab = xlab, main = main, axisnames = F)
axis(1, 0:(length(h$breaks)-1), h$breaks)
box()
legend("topright", c(rownames(tb)), cex = 0.8, fill = cols)
}
with(iris, strathist(Sepal.Length, Species, xlab = "Sepal.Length", main = "Stratified Histogram of Iris Species"))