how to add '%' and '+' symbol on an IDL plot? - idl-programming-language

I am trying to plot the following graph on IDL
PRO HMMM
a = '+'
b = '-'
c = '%'
x = [b+'20'+c, b+'10'+c, '0', a+'10'+c, a+'20'+c]
y = [1.2, 3.2, 4.5, 5.1, 6]
plot, x, y
END
The graph is produced but the '%' and '+' disappear. What is the right way to present those symbols on idl?

If I understand your question, you are trying to place the '+', '-', and '%' symbols on the plot in the positions given by the integers in the x array. So the first point would be the text '-20%', the second point '-10%' and so on.
You can do this with the XYOUTS procedure and separate arrays for the string values and the x values.
PRO HMMM
x = [-20, -10, 0, 10, 20]
xs = ['-20%','-10%','0','+10%','+20%']
y = [1.2, 3.2, 4.5, 5.1, 6]
plot, x, y
XYOUTS, x,y,xs
END

Related

Double y axis in a subplot

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!

Scale in plot graph from RStudio only shows labels inbetween on y axis and leaves out the ends 5, and -25

y = c(2.9, 3.1, −1.2, −1.1, −3.3 ,3.7 ,1.9 ,−0.3, −5.9, −7.9,
−5.5, −7.2, −4.1 ,−8.6, −5.5, −0.7, −5.1, −7.1, −4.2,
0.9, −6.1, −4.1, −4.8, −11.3 −9.3, −10.7, −1.8, −7.4, −22.9)
x = c(1971:1999)
plot(x, y)
I'm new to R and can't seem to figure out how to make the labels inclusive for the whole y range. The first and last numbers appear to be excluded by default?
You can simply add ylim = c(-25, 5) to the plot call:
y = c(2.9, 3.1, −1.2, −1.1, −3.3 ,3.7 ,1.9 ,−0.3, −5.9, −7.9,
−5.5, −7.2, −4.1 ,−8.6, −5.5, −0.7, −5.1, −7.1, −4.2,
0.9, −6.1, −4.1, −4.8, −11.3 −9.3, −10.7, −1.8, −7.4, −22.9)
x = c(1972:1999)
plot(x, y, ylim = c(-25, 5))

Place points at different places on filled.contour3 plots

I am using the filled.contour3 function in the mannner described here. My code is like this
plot.new()
par(mfrow = c(3,3))
pop_x <- 3.0
pop_y <- 6.0
for (i in 1:9){
b_x <- calc_b_x(i)
b_y <- calc_b_y(i)
x <- calc_x(i)
y <- calc_y(i)
z <- calc_z(i)
filled.contour3(x, y, z)
text(x = pop_x, y = pop_y , 'x', cex = 1.5, font = 2)
text(x = b_x, y = b_y , 'a', cex = 1.5, font = 2)
}
This successfully plots 9 graphs in 3 rows. It also puts one 'x' on each graph in the correct position. However the second text call ends up putting 9 'a's on each plot, each in right position. But I only on want one 'a' on each graph, in the correct position for that graph. How do I fix this?
It turns out it was not a problem with filled.contour3. The b_x and b_y were mistakenly vectors rather than scalars, so a single call to
text(x = b_x, y = b_y , 'a', cex = 1.5, font = 2)
produced many points.

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

Axis breaks with integer and non-integer numbers: how to suppress the zero decimals of the integer numbers without excluding the non-integer numbers?

I’m working in a graph where the axis breaks include integer and non-integer numbers.
For illustration purposes consider the following example:
library(ggplot2)
ggplot() + geom_point(aes(x = 0:10, y = 0:10)) +
scale_x_continuous(breaks = seq(0, 10, 2.5)) +
scale_y_continuous(breaks = seq(0, 10, 2.5))
Ggplot is plotting the breaks as: 0.0, 2.5, 5.0, 7.5, 10.0
However I wished that the integer numbers (0, 5 and 10) would appear without the zero decimal, and at the same time I still want keep the non-integer numbers (2.5 and 7.5).
Considering the example above, I wished that the axis breaks would appear as: 0, 2.5, 5, 7.5, 10
Is it possible to do this?
Thanks in advance for any suggestion.
Try this:
ggplot()+geom_point(aes(x=0:10,y=0:10))+
scale_x_continuous(breaks=seq(0,10,2.5), labels=c(0,2.5,5,7.5,10))+
scale_y_continuous(breaks=seq(0,10,2.5), labels=c(0,2.5,5,7.5,10))
with output

Resources