Double y axis in a subplot - julia

I would like to have a double y-axis in one of my subplots; however, it seems like I am missing something about twinx() and link = :x, but I couldn't figure out what..
Plotting double y-axis using twinx() works fine in a standard plotting frame. However, it creates the following problem when I try to link the x-axes. What am I missing here?
using Plots
x = [3, 5, 7, 9];
y = [10, 20, 30 , 40];
z = [-10, -20, -30, -40];
a = plot(1:10, color = :black, legend = false)
b = plot(x, y, color = :green, legend = false)
b = scatter!(twinx(), x, z, color = :orange, legend = false)
plot(a, b, layout = (2, 1), link= :x)
Output:
Thank you very much!

Related

what is the right way to display the points on the graph on top of the axes?

Sometimes there is a need to display points on the graph on top of the axes so that the result looks something like this:
Previously, when I used R, it took a certain amount of alchemy to get the desired result: first the size of the rendering field was set, then the position of the axis, and at the end data points were drawn on top of all this:
f <- rnorm(10, mean=5, sd=1)
plot(x = f, y = c(rep(0, 10)), xlim = c(-1, 11), ylim = c(0, 1),
axes = FALSE, box = FALSE, xaxs = "i", ylab="")
abline(h=0)
axis(side = 1, at = seq(0, 10, 2), pos = 0, tck = -0.02)
points(x = f, y = c(rep(0, 10)), cex=1.0, pch = 21,
col="black", bg="white", lwd = 1)
Now I'm trying to translate my entire workflow to Julia and I want to find some alternative for such visualization. I usually use Plots.jl with GR backend. The trick I used in R does not work in this case: when adding a layer above, it is still cut off by the canvas borders set at the beginning:
c = vcat(1.0, 3.0, 4.0, 6.0, 7.0, 9.0, 12.0, 21.0)
plot(c, zeros(length(c)), seriestype = :scatter,
markersize=5, markershape=:circle, color = :white, label = "", ylims=(0,Inf))
plot!(c, zeros(length(c)), seriestype = :scatter,
markersize=5, markershape=:circle, color = :white, label = "")
Result:
What is the most rational way to get such graphics with Julia?
You should be able to plot on the x-axis by using framestyle=:origin or framestyle=:zerolines
scatter(c, zeros(length(c)),
markersize=5, markershape=:circle,
color = :red, label="", framestyle=:zerolines)
and if you want to show just the x-axis
scatter(c, zeros(length(c)), markersize=5,
markershape=:circle, color = :red, legend=false,
framestyle=:origin, yaxis=false, grid=false, aspect_ratio=1.0)

Changing axis displays for a graph using plot3D in R?

I'm hoping to keep in the image below the ticks on the vertical z axis, but remove ticks and numbers from the x and y axes. I would like to be able to label my x and y axes with a label for each condition in my matrix, but have not figured out how to do this with text3D. For some reason (because I'm on a mac?) I can't download axes3D, which is one potential solution I've seen in other responses.
Here is my code:
x = c(0,1)
y = c(0,1)
zval = c(104.1861, 108.529, 110.3675, 110.4112)
z = matrix (zval, nrow=2, ncol=2, byrow=TRUE)
hist3D(x,y,z, zlim=c(101,111), colvar = NULL, d=2, col = "lightblue", NAcol = "white", breaks = NULL, colkey = NULL, theta=-60, phi=20, nticks=10, axes=TRUE, ticktype="detailed", space=0.5, lighting=TRUE, light="diffuse", shade=.5, ltheta = 50, bty = "g")
My output

Ultimately, I'd like something more along the lines of this:
I'm very new to R.
stackoverflow.com/questions/26794236/ggplot2-3d-bar-plot
^ this seems like it might be what I need, but I couldn't replicate the code without an error. When I tried to run this piece I got an error because my x and z (in this case) axes aren't numerical:
cloud(y~x+z, d, panel.3d.cloud=panel.3dbars, col.facet='grey', xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), par.settings = list(axis.line = list(col = "transparent")))
Maybe this might be helpful (with the caveat that 3D plots can sometimes make interpretation more challenging).
First, I recreated a data frame d based on something similar to what you started with:
x = c(0, 0, 1, 1)
y = c(0, 1, 0, 1)
z = c(104.1861, 108.529, 110.3675, 110.4112)
d <- data.frame(
x = factor(as.logical(x)),
y = factor(as.logical(y)),
z = z
)
Note that for x and y I converted the 0 and 1 to FALSE and TRUE with as.logical, then made them factors.
Then for the plot:
library(latticeExtra)
cloud(z ~ x + y, data = d, panel.3d.cloud=panel.3dbars, col.facet='grey',
xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1),
par.settings = list(axis.line = list(col = "transparent")))
You will want the formula as z ~ x + y where z is a numeric response.
Edit: If you wish to customize the axis labels, you can set the factor labels as follows (for example):
d <- data.frame(
x = factor(as.logical(x), labels = c("Hi", "Lo")),
y = factor(as.logical(y), labels = c("Label1", "Label2")),
z = z
)
Plot

display point labels in scatter3d

I plotted a 3d scatter plot in R using the scatter3d function.
Now, I want to plot the labels on every dot in the 3d scatter, such as every point has its ID next to it i.e., "1", "2" etc..
Here is what I tried:
library("car")
library("rgl")
scatter3d(geometry[,1],geometry[,2],geometry[,3] , surface=FALSE, labels = rownames(geometry), id.n=nrow(geometry))
This tutorial says that adding arguments labels=rownames(geometry), id.n=nrow(geometry) should display the labels on every dot but that did not work.
EDIT:
I uploaded the coordinate file here, you can read it like this
geometry = read.csv("geometry.txt",sep = " ")
colnames(geometry) = c("x","y","z")
EDIT:
Actually, even the example from the tutorial does not label the points and does not produce the plot displayed. There is probably something wrong with the package.
scatter3d(x = sep.l, y = pet.l, z = sep.w,
surface=FALSE, labels = rownames(iris), id.n=nrow(iris))
I can give you a quick fix if you want to use any other function other than scatter3d. This can be achieved using plot3d and text3d function. I have provided the basic code block of how it can be implemented. You can customize it to your needs.
plot3d(geometry[,1],geometry[,2],geometry[,3])
text3d(geometry[,1],geometry[,2],geometry[,3],rownames(geometry))
points3d(geometry[,1],geometry[,2],geometry[,3], size = 5)
After much messing around I got it (I also have the method for plot_ly if you,re interested)
test2 <- cbind(dataSet[,paste(d)],set.final$Groups,test)
X <- test2[,1]
Y <- test2[,2]
Z <- test2[,3]
# 3D plot with the regression plane
scatter3d(x = X, y = Y, z = Z, groups = test2$`set.final$Groups`,
grid = FALSE, fit = "linear",ellipsoid = FALSE, surface=FALSE,
surface.col = c("green", "blue", "red"),
#showLabels(x = x, y = y, z = z, labels=test2$test, method="identify",n = nrow(test2), cex=1, col=carPalette()[1], location=c("lr"))
#labels = test2$test,
id=list(method = "mahal", n = length(test2$test), labels = test2$test)
#id.n=nrow(test2$test)
)
#identify3d(x = X, y = Y, z = Z, labels = test2$test, n = length(test2$test), plot = TRUE, adj = c(-0.1, 0.5), tolerance = 20, buttons = c("right"))
rglwidget()

How to fix the position of a text to follow an object plotted in R

I was wondering how I can fix a piece of text() to always appear above a bracket (or a points() etc.) in a dynamically changing plot? In other words, how should I determine the "x" and "y" for that piece of text to follow the bracket (or a points() etc.) around anywhere in the plot? (see my R code below)
Just as an example, suppose I have a bracket in the plot below whose position (xs and ys) is going to be always known regardless of how the plot will change. Here, how can I fix the position of the text() above this bracket (as currently seen in the plot) no matter where the bracket goes?
P.S. At first, I thought I can take the "y" of the bracket, and then make "y" of the text() to be: ("y" of bracket + .1* "y" of bracket). But given that the plot can dynamically change (i.e., the plot is connected to a function), + .1* "y" of bracket in any plot gives a different position for the text that doesn't guarantee the distance between he text and the bracket be always maintained.
Here is a piece of R code:
if(!require(library(pBrackets))){install.packages('pBrackets') }
library(pBrackets)
plot(1:10, ty = 'n')
brack <- brackets(x1 = 4, y1 = 6, x2 = 8, y2 = 6, h = 1, ticks = .5, curvature = .5,
type=1, col = "blue",
lwd = 2, xpd = T)
text(x = 6, y = 7.2, "Equivalent to ZERO", font = 2) ## How to determine "x" and "y"
# such that the "text" always
# appears above the bracket
# even if the plot changes. This helps making functions.
plot(1:10, ty = 'n')
x1 = 4
y1 = 6
x2 = 8
y2 = 6
h = 1 #Or some variation of h = sqrt( (x2-x1)^2 + (y2-y1)^2 )/4
brack <- brackets(x1 = x1, y1 = y1, x2 = x2, y2 = y2, h = h, ticks = .5, curvature = .5,
type=1, col = "blue",
lwd = 2, xpd = T)
text(x = (x1+x2)/2, y = (y1+h), "Equivalent to ZERO", font = 2, pos = 3)
#pos = 3 means the text will be on top of x and y

Complete missing lines using rgl grid3d

Does anyone know of a way to 1) complete the missing gridlines in the grid3d call for y, and 2) draw horizontal gridlines to close the top of the grids constructed by the grid3d calls for x and y? I've played around with various combinations of pretty calls within grid3d to no avail and am wondering if this is an rgl quirk or a misspecification on my part. Additionally, I'd like to extend the vertical axis numbering to wherever the closed grids end up.
library(rgl)
cpts <- seq(0, 2, length = 40)
spts <- seq(0, 1, length = 20)
grid <- expand.grid(s=spts, c=cpts)
UFn <- function(s,c){c^(0.5) - exp(s) + 1}
U <- UFn(grid$s, grid$c)
open3d()
rgl.surface(x = spts, y = matrix(U,nrow = 40, ncol = 20), z = cpts,
coords = c(1,3,2), specular = "black")
axes3d("x", at = pretty(spts, n = 2), color = "black")
axes3d("y", at = pretty(cpts, n = 5), color = "black")
axes3d("z--", color = "black")
grid3d("x")
grid3d("y", at = pretty(spts, n = 2))
title3d(xlab ='s', ylab = 'c', zlab = 'U', color = "black")
rgl.snapshot("3d.png")
I would say it is a bug. You don't get any z-lines when using grid3d("y",n=2) even though it should be the same. You can work around it by using the list specification of at, setting the x element of the list, eg:
grid3d("y", at = list(x=pretty(spts, n = 2)))

Resources