using Plots
plot(0:10,sin);
plot!(0:10,sin,seriestype = :scatter)
In this example, the output are actually two plots. How can I save them in one file?
I searched and tried some method, but they only support one single plot and I haven't found any functions for multiple plots.
This is just one figure - plot! (with a bang) mutates the figure object created in the first plot call.
Saving this is as simple as savefig("my_output.svg") - if this does not work as expected please provide more details.
Not directly related, but seeing that you are overlaying a scatter plot onto a line plot you might be interested in the linetype kwarg:
plot(0:10, sin, linetype = :scatterpath)
plot(0:10, sin, lt = :scatterpath) # short form alias
will both produce a line and scatter at the same time (with a single label and single colour).
Related
I want to display a plot in the julia language (using iJulia).
But is doesn't show the plot.
Here is a minimal working example of what I tried:
using Plots
function testplotting()
x=[1,2,3,4]
y1=[1,2,3,4]
y2=[1,2,3,4]
plt=plot(x,y)
plot!(x,y2)
return plt
end
plt=testplotting()
display(plt)
println("finished")`
But is doesn't show the plot..
Without the line where I add the extra line to plot the other array it works, but I want to plot multiple variables at the same time.
Can anyone explain why it doesn't display or how to fix it?
Try plot!(plt, xlabel = "blabla").
plot! with a single argument implicitly modifies the "current" plot, which it gets by querying the current plot in global scope. Given that you are in a function scope, the global scope has no access to the plot you just created. Thus you need to specify which plot you want to modify.
I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)
I'm plotting several regression lines in one graph in R. I use the lty= setting in abline() to distinguish them. However, I find this quite unsatisfying once I have more than three lines: all the line types look too similar. (My graph needs to be black/white. )
I was sure there must be a way of combining symbols and lines in R (to have a dashed line with no symbols, one with crosses, etc.), but could not figure it out, at least for abline(). I'm basically thinking combining the symbols in pch with line types or more interesting lines like here.
Is there a way to create new line types? (like for pch, where one
can just type in characters)?
Are there other ways to make lines more easily distinguishable?
Or do I have to switch to ggplot, or other packages?
This question goes into defining the details of line types, but did not really help me here...
Thanks!
You could do it by utilising predict to break up the abline into segments, that you can then specify a pch= against. By manually specifying points, you can decide how often you want a custom tick mark in the length.out= argument:
x <- 1:10
y <- jitter(x,5)
fit <- lm(y~x)
plot(x,y)
pts <- seq(min(x),max(x),length.out=10)
lines(pts, predict(fit, list(x=pts)), type="o", pch="^")
I want to plot several lists of points, each list has distance (decimal) and error_no (1-8). So far I am using the following:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, b1$e2, col="blue", pch=22)
to add them both to the same plot. (I will add legends, etc later on).
The problem I have is that points overlap, and even when changing the character using for plotting, it covers up previous points. Since I am planning on plotting a lot more than just 2 this will be a big problem.
I found some ways in:
http://www.rensenieuwenhuis.nl/r-sessions-13-overlapping-data-points/
But I would rather do something that would space the points along the y axis, one way would be to add .1, then .2, and so on, but I was wondering if there was any package to do that for me.
Cheers
M
ps: if I missed something, please let me know.
As noted in the very first point in the link you posted, jitter will slightly move all your points. If you just want to move the points on the y-axis:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, jitter(b1$e2), col="blue", pch=22)
Depends a lot on what information you wish to impart to the reader of your chart. A common solution is to use the transparency quality of R's color specification. Instead of calling a color "blue" for example, set the color to #0000FF44 (Apologies if I just set it to red or green) The final two bytes define the transparency, from 00 to FF, so overlapping data points will appear darker than standalone points.
Look at the spread.labs function in the TeachingDemos package, particularly the example. It may be that you can use that function to create your plot (the examples deal with labels, but could just as easily be applied to the points themselves). The key is that you will need to find the new locations based on the combined data, then plot. If the function as is does not do what you want, you could still look at the code and use the ideas to spread out your points.
Another approach would be to restructure your data and use the ggplot2 package with "dodging". Other approaches rather than using points several times would be the matplot function, using the col argument to plot with a vector, or lattice or ggplot2 plots. You will probably need to restructure the data for any of these.
I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps