Log axis appearance in julia/Plots - julia

Here is a plot generated by julia's Plots library, using the xaxis=:log attribute:
The plot has evenly spaced tick marks, with labels like 10^0.25. This might be useful sometimes, but I find it a bit confusing for this plot, because most people don't know the value of 10^0.25 without looking it up.
I would prefer the x axis to have logarithmically spaced ticks, representing uniform intervals of the quantity on the x axis. Here's a quick example to show what I mean, generated using semilogx in Python's matplotlib lirary:
In this plot the x axis has ticks at x=1, x=2, x=3 etc., which I find more useful for the figures I'm generating. Can this be achieved in julia using Plots?

As it has been told in the discourse topic, the default tick behavior (for most backends) is different than what you expected.
There is a way you can achieve the tick behaviour you want. You can manually set tick positions and tick labels through xticks (yticks or zticks) argument.
For example the following snippet will put ticks at equally spaced points as you wanted in your post.
x = 1:0.1:10;
y = rand(length(x));
plot(x, y, xscale=:log10, xticks=(1:10, 1:10))
The first element of the tuple is for the location of the ticks and the second is for the labels. Although I used a range object for labels, you can use array of Strings or LaTeXStrings etc.
You might also want to take a look at minorticks (xminorticks for your case) and minorgrid attributes in the Julia Plots documentation.

Related

Making barplot axis more readable: how to place ticks at some positions on the X axis so they correspond to specific bars for a large vector

Say i have a fairly long vector which i want to present as a barplot:
myvec<-runif(2000,0,1)
barplot(myvec, col="grey", border=NA, names.arg = seq(1:2000))
i would like x axis to go pretty:
to have, say 4-5 labels to be shown on the axis, but with the ticks that determine to which specific bar it corresponds.
worst to worst i can live with the random labels that are being picked automatically, but i want to see which bar they correspond to.
thanks
You can try this:
library(ggplot2)
myvec<-data.frame(v1=runif(2000,0,1))
ggplot(myvec,aes(x=dplyr::row_number(v1),y=v1))+geom_bar(stat='identity')

Plot piecewise data, x-axis limits

I use Julia with Plots , to generate my plots.
I want to plot data (A,B) and i know that all interesting data lies in two region of A. The two regions should be plotted between each other in one plot.
My A-data is evenly spaced. So what i did was cutting out my interesting pieces and glued them into one object.
My problem is that i don't know how to manipulate the scale on the x-axis.
When I just plot the B data against their array index, I basically get the form I want. I just need the numbers from A on the x-axis.
I give here a toy example
using Plots
N=5000
B=rand(N)
A=(1:1:N)
xl_1=100
xu_1=160
xl_2=600
xu_2=650
A_new=vcat(A[xl_1:xu_1],A[xl_2:xu_2])
B_new=vcat(B[xl_1:xu_1],B[xl_2:xu_2])
plot(A_new,B_new) # This leaves the spacing between the data explicit
plot(B_new) # This creats basically the right spacing, but
# without the right x axis grid
I did not find anything how one can use two successive xlims, therefore i try it this way.
You can't pass two successive xlims, because you can't have a break in the axis. That is by design in Plots.
So your possibilities are: 1) to have two subplots with different parts of the plot, or 2) to plot with the index, and just change the axis labels.
The second approach would use a command like xticks = ([1, 50, 100, 150], ["1", "50", "600", "650"], but I'd recommend the first as it's strictly speaking a more correct way of displaying the data:
plot(
plot(A[xl_1:xu_1], B[xl_1:xu_1], legend = false),
plot(A[xl_2:xu_2], B[xl_2:xu_2], yshowaxis = false),
link = :y
)

In R rgl, how to choose the position of tick marks in plot3d?

In R,
library(rgl)
m <- matrix(rnorm(300),100,3)
par3d(ignoreExtent=F)
plot3d(m,box=T,axes=F,xlab='',ylab='',zlab='')
axes3d(labels=F,tick=F,box=F)
gr <- grid3d('z')
par3d(ignoreExtent=T)
plot3d(cbind(m[,1:2],rgl.attrib(gr[1],'vertices')[1,3]),col='gray',add=T)
still prints the ticks with numbers:
Shouldn't tick=F parameter in axes3d() get rid of the tick marks and the numbers?
I want to add the x and y axes at the bottom of the graph, not at the top. Also, when I add them using axis3d(), the ticks aren't orthogonal anymore, but inclined in 45 degrees relative to their plane, which I think is ugly.
par3d(ignoreExtent=F)
plot3d(m,box=T,axes=F,xlab='',ylab='',zlab='')
box3d()
axis3d('x--',labels=T,tick=T)
axis3d('y+-',labels=T,tick=T)
axis3d('z++',labels=T,tick=T)
gr <- grid3d('z')
par3d(ignoreExtent=T)
plot3d(cbind(m[,1:2],rgl.attrib(gr[1],'vertices')[1,3]),col='gray',add=T)
If I have to go this second way, how to get rid of the front lines of the box? Or is there another way to print the default tick marks (orthogonal) in the desired position?
Axes in rgl are somewhat confusing and not very flexible. First, there are two different kinds: those drawn by axis3d, and those drawn with rgl.bbox. Only the first type pays attention to the tick argument, and your first example used the second type.
You can remove the ticks in the rgl.bbox axes by setting marklen = 0, marklen.rel = FALSE, but this has the unfortunate effect of putting the numbers right on the box. There isn't a separate parameter to control placement of the numbers independent of tickmark length. If you don't want numbers at all, use xlen = 0, ylen = 0, zlen = 0.
The axis3d axes are also not very flexible. If you want to change their orientation, you'll need to modify that function. The mpos array holds the coordinates of each tick; change it to make the ticks point the way you want.
Regarding the box: it's fixed if you use box3d() to draw it. If you want the rgl.bbox style, you'll have to use that function. You could also use segments3d() and mtext3d() to construct your own axes, but they won't move around like the rgl.bbox axes.

Why are all relevant tick-marks not plotted on the X-axis?

I often determine that when plotting in R not all relevant tick-marks are drawn. Relevant here means that there is data present.
See this example
> set.seed(NULL)
> d <- data.frame(a=sample(1:10, replace=TRUE), b=sample(11:30))
> plot(d)
The resulting plot where you can see values on the X-axis at 3, 5, 7 and 9. But the tick-marks for them are missing.
The focus of my question is to understand why R acts like that. What is the algorithm and logic behind it?
btw: I know how to solve it. I can draw the X-axis myself. But that is not part of the question.
You could find a brief description of the algorithm for plotting the tick marks using?axis.
plot() is a generic function to plot a wide sort of data. In your example, you are using discrete data. For continuous data, it does not make much sense to have a single tick mark for every single value, which would make unreadable the axes.
However, you can easily adjust the ticks in your plot using axis()

R, Plotting points with Labels on a single (horizontal) numberline

For educational purpose I'm trying to plot a singel horizontal "numberline" with some datapoints with labels in R. I came this far;
library(plotrix)
source("spread.labels.R")
plot(0:100,axes=FALSE,type="n",xlab="",ylab="")
axis(1,pos=0)
spread.labels(c(5,5,50,60,70,90),rep(0,6),ony=FALSE,
labels=c("5","5","50","60","70","90"),
offsets=rep(20,6))
This gave me a numberline with smaller lines pointing up to (and a little bit "in") the labels from where the datapoints should lie on the numberline - but without the points itself. Can anyone give me additional or alternative R-codes for solving thess problems:
- datapoints itself still missing are not plotted,
- and labels maybe not evenly divided over the whole numberline,
- and lines come into the labels and not merely point to the labels
Thank a lot,
Benjamin Telkamp
I usually like to create plots using primitive base R graphics functions, such as points(), segments(), lines(), abline(), rect(), polygon(), text(), and mtext(). You can easily create curves (e.g. for circles) and more complex shapes using segments() and lines() across granular coordinate vectors that you define yourself. For example, see Plot angle between vectors. This provides much more control over the plot elements you create, however, it often takes more work and careful coding than more "pre-packaged" solutions, so it's a tradeoff.
For your case, it sounds to me like you're happy with what spread.labels() is trying to do, you just want the following changes:
add point symbols at the labelled points.
prevent overlap between labels and lines.
Here's how this can be done:
## define plot data
xlim <- c(0,100);
ylim <- c(0,100);
px <- c(5,5,50,60,70,90);
py <- c(0,0,0,0,0,0);
lx.buf <- 5;
lx <- seq(xlim[1]+lx.buf,xlim[2]-lx.buf,len=length(px));
ly <- 20;
## create basic plot outline
par(xaxs='i',yaxs='i',mar=c(5,1,1,1));
plot(NA,xlim=xlim,ylim=ylim,axes=F,ann=F);
axis(1);
## plot elements
segments(px,py,lx,ly);
points(px,py,pch=16,xpd=NA);
text(lx,ly,px,pos=3);

Resources