Julia: Passing Plot optional arguments through outer function - julia

I have a function in Julia that produces a plot using the Plots package plot() command. I'd like to set some optional arguments for the plot by passing arguments into my outer function. For example, I'd like to set title and axis labels without needing to program a bunch of if statements to check what parameters I'm trying to pass in. A MWE of what I'd like to do is as follows:
function outer(data; plot_options...)
x = data.x
y = data.y
plot(x,y, plot_options...)
end
So that if I call something like outer(data, title="My Title", lw=2) I produce a plot with title set to "My Title" and a linewidth of 2. Trying the naive thing that I programmed above results in an error.

function outer(data; plot_options...)
x = data.x
y = data.y
plot(x,y; plot_options...)
end
missed a semicolon?

Related

Processing statistics in Gadfly

I want to extend the Gadfly package to match my own idiosyncratic preferences. However I am having trouble understanding how to use Gadfly's statistics in a way that allows for their output to be processed before plotting.
For example, say I want to use the x,y aesthetics produced by Stat.histogram. To add these to a plot, I understand I can include Stat.histogram as an argument in a layer(). But what do I do if I want to use Stat.histogram to calculate the x,y aesthetics, edit them using my own code, and then plot these edited aesthetics?
I'm looking for a function like load_aesthetics(layer(x=x, Stat.histogram)), or a field like layer(x=x, Stat.histogram).aesthetics.
you can create your own statistic. see https://github.com/GiovineItalia/Gadfly.jl/issues/894
Building off #bjarthur's answer, I wrote the below function.
"Return the aesthetics produced by a Gadfly Statistic object."
function process_statistic(statistic::Gadfly.StatisticElement,
input_aesthetics::Dict{Symbol,<:Any}
)
# Check that enough statistics have been provided.
required_aesthetics = Gadfly.input_aesthetics(statistic)
for required_aesthetic in required_aesthetics
if required_aesthetic ∉ keys(input_aesthetics)
error("Aesthetic $(required_aesthetic) is required")
end
end
# Create the aes object, which contains the statistics.
aes = Gadfly.Aesthetics()
[setfield!(aes, key, value) for (key, value) in input_aesthetics]
# These need to be passed to the apply_statistic() function. I do
# not understand them, and the below code might need to be edited
# for this function to work in some cases.
scales = Dict{Symbol, Gadfly.ScaleElement}()
coord = Gadfly.Coord.Cartesian()
# This function edits the aes object, filling it with the desired aesthetics.
Gadfly.Stat.apply_statistic(statistic, scales, coord, aes)
# Return the produced aesthetics in a dictionary.
outputs = Gadfly.output_aesthetics(statistic)
return Dict(output => getfield(aes, output) for output in outputs)
end
Example usage:
process_statistic(Stat.histogram(), Dict(:x => rand(100)))

function as an argument of a function in R

I have this function which I have saved in the database.
runifrect <- function(n,a,b,z,d) {
else(print("Check if the x and y coordinates lie as such: 0<=a<b<=1 and 0<=z<d<=1"))}
Now I am trying to define this function with the use of the old one:
plotrectpoints<- function(runifrect(n,a,b,z,d),a,b,z,d) {
However I am getting an error I dont understand what is wrong with the function, I want it to work for any arbitrary values n,a,b,z,d.
When a function is defined in R it cannot evaluate the values in parenthesis. It rather creates dummy objects which get the values when the function is called. These dummy object names follow the same rules that are applied to all variables names. Since you cannot have a variable name contained parenthesis, you cannot include it into the list of arguments when you define the function.
First function definition
runifrect <- function(n,a,b,z,d) {
if(a<1&a>=0&b>0&b<=1&z<1&z>=0&d<=1&d>0) {
x <- runif(n,a,b)
y <- runif(n,z,d)
k<-c(x,y)
matrix(k,nrow = n,ncol = 2)}
else(print("Check if the x and y coordinates lie as such: 0<=a<b<=1 and 0<=z<d<=1"))}
Second function definition
plotrectpoints<- function(x,a,b,z,d) {
plot(x,
xlim=c(0,1),
ylim=c(0,1),
main = "Plot of rectangle and randomly generated points")
rect(a,z,b,d, border='red',lty='dotted')}
Call to the function
plotrectpoints( runifrect(n,a,b,z,d), a,b,z,d)
This is my first answer on this platform. Please bear with me.
If your end goal is to call the 'runifrect()' function from the 'plotrectpoints()' function, we can remove the 'runifrect(n,a,b,z,d)' parameter and replace that with 'n'.
The code should look as follows:
runifrect <- function(n,a,b,z,d) {
if(a<1&a>=0&b>0&b<=1&z<1&z>=0&d<=1&d>0) {
x <- runif(n,a,b)
y <- runif(n,z,d)
k<-c(x,y)
matrix(k,nrow = n,ncol = 2)}
else(print("Check if the x and y coordinates lie as such: 0<=a<b<=1 and 0<=z<d<=1"))}
plotrectpoints<- function(n,a,b,z,d) {
plot(runifrect(n,a,b,z,d),
xlim=c(0,1),
ylim=c(0,1),
main = "Plot of rectangle and randomly generated points")
rect(a,z,b,d, border='red',lty='dotted')}
and I have used the following parameters to test.
plotrectpoints(10,0.5,0.8,0.3,0.7)
I have also attached the plot the above code generated.
enter image description herePlease let me know if the above code is what you are looking for.

Change x and y labels on a gbm partial plot

I am having trouble changing the x and y labels on a partial plot for a gbm model. I need to rename them for the journal article.
I read this in and create the plot as follows:
library(gbm)
final<- readRDS(final_gbm_model)
summary(final, n.trees=final$n.trees)
Here is the summary output:
var rel.inf
ProbMn50ppb ProbMn50ppb 11.042750
ProbDOpt5ppm ProbDOpt5ppm 7.585275
Ngw_1975 Ngw_1975 6.314080
PrecipMinusETin_1971_2000_GWRP PrecipMinusETin_1971_2000_GWRP 4.988598
N_total N_total 4.776950
DTW60YrJurgens DTW60YrJurgens 4.415016
CVHM_TextZone CVHM_TextZone 4.225048
RiverDist_NEAR RiverDist_NEAR 4.165035
LateralPosition LateralPosition 4.036406
CAML1990_natural_water CAML1990_natural_water 3.720303
PctCoarseMFUpActLayer PctCoarseMFUpActLayer 3.668184
BioClim_BIO12 BioClim_BIO12 3.561071
MFDTWSpr2000Faunt MFDTWSpr2000Faunt 3.383900
PBot_krig PBot_krig 3.362289
WaterUse2 WaterUse2 3.291040
AVG_CLAY AVG_CLAY 3.280454
Age_yrs Age_yrs 3.144734
MFVelSept2000 MFVelSept2000 3.064030
AVG_SILT AVG_SILT 2.882709
ScreenLength ScreenLength 2.683542
HydGrp_C HydGrp_C 2.666106
AVG_POR AVG_POR 2.563147
MFVelFeb2000 MFVelFeb2000 2.505106
HiWatTabDepMin HiWatTabDepMin 2.421521
RechargeAnnualmmWolock RechargeAnnualmmWolock 2.252706
I can create a partial dependence plot as follows:
plot(final,"ProbMn50ppb",n.trees=final$n.trees)
But if I try to set the label arguments I get the following error:
plot(final,"ProbMn50ppb",n.trees=final$n.trees,ylab="LNNO3")
Error in plot.default(X$X1, X$y, type = "l", xlab = x$var.names[i.var], :
formal argument "ylab" matched by multiple actual arguments
How can I change the y and x axis labels?
The plot.gbm function passes its own name to the generic plot function so the two are colliding. So you will not be able to customize the plot the way you want in that mode. But the authors did provide an alternative when you set return.grid=TRUE. Instead of building a plot, it will output the data itself. You can then use that for any plot including ggplot2.
plotdata <- plot(gbm1, return.grid=TRUE)
plot(plotdata, type="l", ylab="ylab", xlab="xlab")
Example data from help(gbm)
You can also change the gbm object itself before plotting (or in a function):
your_gbm_obj$var.names[index] = "axis label"

How to plot StatsBase.Histogram object in Julia?

I am using a package(LightGraphs.jl) in Julia, and it has a predefined histogram method that creates the degree distribution of a network g.
deg_hist = degree_histogram(g)
I want to make a plot of this but i am new to plotting in Julia. The object returned is a StatsBase.Histogram which has the following as its inner fields:
StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}}
edges: 0.0:500.0:6000.0
weights: [79143,57,32,17,13,4,4,3,3,2,1,1]
closed: right
Can you help me how I can make use of this object to plot the histogram?
I thought this was already implemented, but I just added the recipe to StatPlots. If you check out master, you'll be able to do:
julia> using StatPlots, LightGraphs
julia> g = Graph(100,200);
julia> plot(degree_histogram(g))
For reference, the associated recipe that I added to StatPlots:
#recipe function f(h::StatsBase.Histogram)
seriestype := :histogram
h.edges[1], h.weights
end
Use the histogram fields .edges and .weights to plot it e.g.
using PyPlot, StatsBase
a = rand(1000); # generate something to plot
test_hist = fit(Histogram, a)
# line plot
plot(test_hist.edges[1][2:end], test_hist.weights)
# bar plot
bar(0:length(test_hist.weights)-1, test_hist.weights)
xticks(0:length(test_hist.weights), test_hist.edges[1])
or you could create/extend a plotting function adding a method like so:
function myplot(x::StatsBase.Histogram)
... # your code here
end
Then you will be able to call your plotting functions directly on the histogram object.

error labelling axis of plot using Ecdf

I am attempting to plot a graph using the code below:
Require(Hmisc)
Ecdf(ceac_primary,xlab="axis label",xlim=c(5000,50000),q=c(0.9,0.1),
ylab="Probability of Success",main="CEAC")
Where ceac_primary is a data frame with 1 variable of 90k observations.
When I include the 'xlab="axis label"' I keep getting the following error:
Error in Ecdf.default(v, group = group, weights = weights, normwt = normwt, :
formal argument "xlab" matched by multiple actual arguments
However if I exclude the x axis label part of the code, it plots the graph fine.
Is this a known problem, and if so, are there alternative ways to plot an x axis label?
Thanks
Digging around in the source code for Ecdf.data.frame (the method that is called when passing a data.frame to Ecdf), it looks like that function creates an object that is later passed to the xlab argument. Therefore, xlab is not expected as a user-supplied argument when running Ecdf with a data.frame. Here's the code that creates the object lab that gets passed to xlab within Ecdf.data.frame:
lab <- if (vnames == "names")
nam[j]
else label(v, units = TRUE, plot = TRUE, default = nam[j])
Then Ecdf is called with xlab = lab, but also any arguments in the elipses of Ecdf.data.frame are also passed to Ecdf. Since xlab is not a formal argument of Ecdf.data.frame, this is why you get your error.
To get around it, try either of the following:
Convert your data.frame to a vector of the appropriate class (numeric, I presume), and then run
Ecdf(ceac_primary_Vec, xlab = "axis label")
Or, you can create a label for the one column in your data.frame using the label function in the Hmisc package. If that column is called myCol, you can run
label(ceac_primary$myCol) <- "axis label"
Ecdf(ceac_primary)
And that should get your axis label printing correctly.

Resources