Move and Resize Subplots in Octave - resize

I'd like to move and resize four subplots in Octave so that they are bigger with less white space between them. The minimal code below only moves and resizes the first subplot (221) whilst leaving the remaining three untouched.
sp_hand1 = subplot(221);plot(sinewave(20,20)) ;
set( sp_hand1 , 'OuterPosition' , [ -0.11 , 0.4 , 0.6 , 0.65 ] ) ;
sp_hand2 = subplot(222);plot(sinewave(20,20)) ;
set( sp_hand2 , 'OuterPosition' , [ -0.11 , 0.4 , 0.6 , 0.65 ] ) ;
sp_hand3 = subplot(223);plot(sinewave(20,20)) ;
set( sp_hand3 , 'OuterPosition' , [ -0.11 , 0 , 0.6 , 0.65 ] ) ;
sp_hand4 = subplot(224);plot(sinewave(20,20)) ;
set( sp_hand4 , 'OuterPosition' , [ -0.11 , 0 , 0.6 , 0.65 ] ) ;
How can I resize them all to be the same size and moved appropriately?

When I faced a similar issue during my thesis, I found that the solution that worked best for me was to use axes directly rather than subplots, and specify position. Some manual adjustment may be unavoidable in the beginning, but it's typically pretty straightforward, and can be automated easily for predictable graph placements, especially if the figure size is pre-specified too.
E.g.
h1 = axes('position', [0.04, 0.54, 0.45, 0.45]); plot( sinewave( 20, 20 ) );
h2 = axes('position', [0.54, 0.54, 0.45, 0.45]); plot( sinewave( 20, 20 ) );
h3 = axes('position', [0.04, 0.04, 0.45, 0.45]); plot( sinewave( 20, 20 ) );
h4 = axes('position', [0.54, 0.04, 0.45, 0.45]); plot( sinewave( 20, 20 ) );
In theory subplots and independent axes should behave more or less the same; the big difference being that in case of overlap, subplot deletes the overlapped plot, whereas axes overlaps happily. This would include 'invisible' overlaps.
I am not 100% sure if there is a way to obtain the same result using 'outerposition', but for me outerposition tends to behave a bit oddly, and I've always managed to get the desired results with 'position' directly, so I've never had a need for it.
I have also found that often plotting more things or changing other aspects of the plot resets some axes properties, so such size adjustments are best done as the last step for each axes object.

Related

ggplot: Reorder Facet rows in facet.grid

I am having issues reorganizing my rows using facet.grid. I have four groups that by default are organized alphabetically. Using the factor command I can organize the groups - but they switch to columns (horizontal) and I need rows (vertical).
Example of subset of my data
Site Date Analyte Result
SJR#Calaveres 5/26/22 Top TP 0.35
SJR#Ship Channel 5/26/22 Top TP 0.56
Turning Basin 5/26/22 Top TP 0.46
Morelli Boat Ramp 5/26/22 Top TP 0.45
Public Dock 5/26/22 Top TP 0.55
wi_daily_wq<-read.csv("nutrients_p.csv")
p <- ggplot(data = wi_daily_wq, aes(x = Date, y = Result))+
geom_point(aes(color = Site)) +
theme_bw()
p
# Add vertical facets, aka divide the plot up vertically since they share an x axis
p + facet_grid(Analyte ~ .)
# Add vertical facets, but scale only the y axes freely
p + facet_grid(Analyte ~ ., scales = "free_y")
p + facet_grid(Analyte ~ ., scales = "free_y",
switch = "y") # flip the facet labels along the y axis from the right side to the left
#Trying to organize rows in order that I want
p + facet_grid(~factor (Analyte,levels=c('Bottom TP','Top TP','Bottom PO4','Top PO4')))
ylab(NULL) + # remove the word "values"
theme(strip.background = element_blank(), # remove the background
strip.placement = "outside") # put labels to the left of the axis text
If I remove the following argument from the above code I get the row plot I like. But, the rows are just in alphabetical order, not the order I need them in.
p + facet_grid(~factor (Analyte,levels=c('Bottom TP','Top TP','Bottom PO4','Top PO4')))
I'm wondering what adjustment I need to make to my code to organize the rows in the order I need them without converting the facets to columns.

How to recycle two colours in a geom_jitter with more than 2 categories

I would like to use only two colours that alternate in a geom_jitter plot.
ggplot(data=dat1, aes(name, value, colour=factor(location)))+
geom_jitter()
The location has more than 2 categories, but I want to colour them by alternating two colours.
So far I've tried to add + scale_color_manual(values=rep(c("red","green"))) but that did not work.
My data is something like this
ID <- c('P1', 'P2', 'P3', 'P4')
location <- c('harm', 'breast', 'colon', 'liver')
value <- c(7.2, 5.4, 0.3, 2.1)
ID location value
p1 harm 7.2
p2 breast 5.4
p3 colon 0.3
p4 liver 2.1
What I want is the plot shows only two colours eg. harm = red, breast=green, colon = red .... I can't do manually because I have a lot of categories and I want to create a function in which the location may be different.
Any advice?
Thanks
you can add the numbers of your categories at the end. E.G if you have 32 categories:
scale_color_manual(values=rep(c("blue","red"), 32 ))

Display user specified contour levels in GrADS

I would like to know how to display specific contour levels on the colorbar. For example, as shown in the schematic above taken from pivotalweather, shows a colorbar for precipitation values that are not really equally spaced. I would like to know how to achieve a similar result with GrADS.
PS: I use the cbarn.gs and the xcbar.gs script sometimes.
You need to use the original color set of GRADS for this.
THREE steps:
1). Set the color using the 'set rgb # R G B'. You need the RGB of the colors in your color bar. Since there are 15 default colors in GrADS, you should start the # at 16.
Check this link for details of the colors:
http://cola.gmu.edu/grads/gadoc/colorcontrol.html
2). You need to set the color level as follows:
set clevs 0.01 0.05 0.1 0.02 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.2 1.4 1.6 1.8 2
2.5 3 3.5 4 5 6 8 15
3). You need to specify the colors based on your defined RGBs.
set ccols 16, 17, 18,....etc.

Array Numpy Side Effect

I found a strange effect when permuting array with numpy:
def permute(yy, kmax) :
kmax=5
kk= np.random.uniform(1,kmax)
nn= int(np.floor(len(yy)/kk))
yy3= np.zeros_like(yy );
np.copyto(yy3,yy)
for ii in range(0, nn):
ax= kk*ii-kk*nn
aux= yy[ax]
aux2= yy[kk*ii]
yy3[ax] = aux
yy3[kk*ii] = aux2
return yy3
and
yy= np.random.normal(0,1,50000)
yy1= permute(yy,2)
( np.var(yy)- np.var(yy1) )
( np.mean(yy)- np.mean(yy1) )
Result is not zero !!!
Do you think this comes from reference assignment in the array ?
I ran your function with np.arange(10) and got
1752:~/mypy$ python stack35004877.py
0.0
0.0
[0 1 2 3 4 5 6 7 8 9] # yy
[0 1 2 3 4 5 6 7 8 9] # yy1
And repeated it with the large random array, with the same 0s for the statistics.
Note that your code did not permute the input
Maybe it will be clearer if I clean it up:
def permute(yy, kmax=5) :
kk= np.random.randint(1,kmax) # int rather than float
nn= int(np.floor(len(yy)/kk))
print(nn,kk)
yy3= yy.copy()
for ii in range(0, nn):
ind1 = kk*ii
ind2 = ind1-kk*nn
yy3[ind2] = yy[ind2]
yy3[ind1] = yy[ind1]
return yy3
You aren't moving anything; and with kmax=2 you just copy every thing from yy to yy3 - something you already did outside the loop. With kmax=5 you don't copy everything in the loop - but the initial copy hides that.
With random.uniform(), kk is a float, and the indexes are also floats. That's not desirable, but apparently not a problem.
But even if I switch the indices:
yy3[ind2] = yy[ind1]
yy3[ind1] = yy[ind2]
I don't permute anything, because ind2 a negative value, that maps on to the same element as ind1. yy[-1] is the last item of yy.
[(0, -10), (1, -9), (2, -8),... (9, -1)]
I could work out the details, but I think you should do that yourself - with a small test case. And skip that initial copyto, that just hides errors in the iteration. Print the details, not just summary statistics from large random arrays.
And in the long run you don't want to use an iteration like this. You want to do the permutation with one indexing call. But first get this version working correctly.

change rgb dynamically in netlogo

So I'm writing a short programme in net logo where I want to color code my turtles based on a variable that they own view which varies between -1 and 1. I tried using color-scale in netlogo to define the colour, but it doesn't do quite what I want.
I wrote this to describe what I want, but netlogo seems to be getting confused when I pass the col variable to the set color command.
to colorise;;------------------------------------------------------------
; this changes the agent's colour based on their current [view] score.
; We could use the color-scale in netlogo, but it only works for one colour
; and it's easy to end up with a colour so dark we can't see it against black patches.
moderate ; resets any agents which have somehow ended up with a view score outside -1 to +1
ifelse view > 0
[ let col ( 1 - view )
set col col * 255
set color [ 255 col col ]
]
[ let col ( 1 + view )
set col col * 255
set color [ col col 255 ]
]
end
does anyone have any ideas?
Thanks!
Will
Assuming you have correctly limited the range of view, you will just run into a list creation problem: you cannot use the bracket notation with variables. Instead try
set color (list col col 255)
etc