How to plot multiple curves on same plot in julia? - julia

I am using julia to build linear regression model from scratch. After having done all my mathematical calculations, I need to plot a linear regression graph
I have a scatter plot and linear fit (Linear line) plots separately ready, How do I combine them or use my linear fit plot on scatter plot?
Basically, how do I draw multiple plots on a single plot in Julia?
Note: Neither do I know python or R
x = [1,2,3,4,5]
y = [2,3,4,5,6]
plot1 = scatter(x,y)
plot2 = plot(x,y) #line plot
#plot3 = plot1+plot2 (how?)

Julia doesn't come with one built-in plotting package, so you need to choose one. Popular plotting packages are Plots, Gadfly, PyPlot, GR, PlotlyJS and others. You need to install them first, and with Plots you'll also need to install a "backend" package (e.g. one of the last three mentioned above).
With Plots, e.g., you'd do
using Plots; gr() # if GR is the plotting "backend" you've chosen
scatter(point_xs, point_ys) # the points
plot!(line_xs, line_ys) # the line
The key here is the plot! command (as opposed to plot), which modifies an existing plot rather than creating a new one.
More simply you can do
scatter(x,y, smooth = true) # fits the trendline automatically
See also http://docs.juliaplots.org/latest/
(disclaimer: I'm associated with Plots - others may give you different advice)

Related

How to plot 3 different time-series with "actual" values rather than a density plot as ridgeline plots (formerly known as Joyplots) in R?

I would like to plot ridgeline plots of 3 different timeseries with same axes with actual values, but NOT a density plot as ridgeplots generally show.
Tried using Henrik Lindberg's code here : https://github.com/halhen/viz-pub/tree/master/sports-time-of-day
It does what it is supposed to do, but can not produce smoothing.
Also tried the ggridges manual codes (below)
ggplot(df,aes(x = time, y = activity, height = p)) + geom_density_ridges()
ggridges produces density plots, not as a timeseries as I want it to be. Henrik's code produces desired timeseries, but without the smoothing as I wanted from a ridgeplot.

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

Different lowess curves in plot and qplot in R

I am comparing two graphs with a non-parametric lo(w)ess curve superimposed in each case. The problem is that the curves look very different, despite the fact that their arguments, such as span, are identical.
y<-rnorm(100)
x<-rgamma(100,2,2)
qplot(x,y)+stat_smooth(span=2/3,se=F)+theme_bw()
plot(x,y)
lines(lowess(y~x))
There seems to be a lot more curvatute in the graph generated by qplot(). As you know detecting curvature is very important in the diagnostics of regression analysis and I fear that If I am to use ggplot2, I would reach erroneous conclusions.
Could you please tell me how I could produce the same curve in ggplot2?
Thank you
Or, you can use loess(..., degree=1). This produces a very similar, but not quite identical result to lowess(...)
set.seed(1) # for reproducibility
y<-rnorm(100)
x<-rgamma(100,2,2)
plot(x,y)
points(x,loess(y~x,data.frame(x,y),degree=1)$fitted,pch=20,col="red")
lines(lowess(y~x))
With ggplot
qplot(x,y)+stat_smooth(se=F,degree=1)+
theme_bw()+
geom_point(data=as.data.frame(lowess(y~x)),aes(x,y),col="red")
Here is a new stat function for use with ggplot2 that uses lowess(): https://github.com/harrelfe/Hmisc/blob/master/R/stat-plsmo.r. You need to load the proto package for this to work. I like using lowess because it is fast for any sample size and allows outlier detection to be turned off for binary Y. But it doesn't provide confidence bands.

Extracting one plot from the plot function in geoR

I am using the geoR package, and I would like to display only one of the graphs obtained when using the plot function. Using a dataset provided with the package:
library(geoR); data(elevation)
plot(elevation)
This gives 4 plots on a 2 x 2 grid as in below. I would like to use the bottom right plot alone, but I am not sure how to get this plot alone.
So I tried plotting it from scratch:
axExFact <-1.1 # to set fitting y-axis limits
ymax <- max(c(0, signif(max(elevation[[2]])*axExFact, digits=1)))
ymin <- min(c(0, signif(min(elevation[[2]])*axExFact, digits=1)))
with(elevation, hist(data, main='Plot', xlab = 'Value ',cex.lab=1.25, font.lab=2,ylim=c(ymin, ymax)))
Though I can get the y-axis limits adjusted to fully cover the extent of the data, I am unable to add a density estimate along the histogram. Thought it could be done with lines(density(as.numeric[[elevation]])), but it doesn't work.
So it would be a lot easier to just get the graph obtained with the plot function. Then the only problem would be to adjust the y-axis. Any suggestions would be welcomed.

In R and ggplot2 package, How to Add Lines?

I am doing a survival analysis and have produced the survival graph using
plot function to plot the Kaplan-Meier(KA variable) estimate as y value against time.
lines function to plot the step lines between estimate i and i+1, for each i=1,2,....
The code is as follows:
plot(KA)
for( i in 1:(length(KA)-1)){
lines(c(i,i+1),c(KA[i],KA[i])) # The horizontal step lines
lines(c(i+1,i+1),c(KA[i],KA[i+1])) # The vertical step lines
}
Now I want to make a more beautiful survival graph using ggplot2 package.
The question is: how to add the step lines into the graph?
I'm sorry, I can not put graphs as my reputation is less than 10.
Have a look at either geom_step, geom_path or geom_segment.
They might be helpful in what you are trying to achieve.
http://had.co.nz/ggplot2/geom_step.html
http://had.co.nz/ggplot2/geom_path.html
http://had.co.nz/ggplot2/geom_segment.html

Resources