SAS proc sgplot two separate legends - plot

I run into legend issue with SAS proc sgplot. My code is below. In my first plot, I have legend for height_group. In my second plot, I have legend for ellipse line. I want to draw a plot with both legends. That is a legend for height_group, another legend for ellipse line in one plot. How can I do it? I only got the third plot. But the two legends are too far from each other, I want them to be both at bottom, I also want the color legend the same as the first plot which indicates that this legend is for height_group.
data plot;
set sashelp.class;
if height > 65 then height_group = "high";
else height_group = "low";
run;
data attrmap;
retain id "myid";
infile datalines delimiter=',';
length value $45 markercolor $10 markersymbol $10;
input value $ markercolor $ markersymbol $ ;
cards;
high, red, Circle
low, blue, X
;
run;
* This is my first plot;
proc sgplot data=plot dattrmap=attrmap;
scatter x=height y=weight/ group=height_group attrid=myid;
ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
ellipse x=height y=weight / alpha=.05 name="ninetyfive" legendlabel="95% Prediction";
*keylegend "eighty" "ninetyfive";
run;
* This is my second plot;
proc sgplot data=plot dattrmap=attrmap;
scatter x=height y=weight/ group=height_group attrid=myid;
ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
ellipse x=height y=weight / alpha=.05 name="ninetyfive" legendlabel="95% Prediction";
keylegend "eighty" "ninetyfive";
run;
* This is my third plot;
proc sgplot data=plot dattrmap=attrmap;
scatter x=height y=weight/ group=height_group attrid=myid name ="color";
ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
ellipse x=height y=weight / alpha=.05 name="ninetyfive" legendlabel="95% Prediction";
keylegend "eighty" "ninetyfive";
keylegend "color";
run;

You can use lineattrs= option to control the color of ecllipses. And position= option to control the position of legend.
proc sgplot data=plot dattrmap=attrmap;
scatter x=height y=weight/ group=height_group attrid=myid name ="color";
ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction" lineattrs=(color=red);
ellipse x=height y=weight / alpha=.05 name="ninetyfive" legendlabel="95% Prediction" lineattrs=(color=blue);
keylegend "color" "eighty" "ninetyfive"/position=bottom;
run;
By the way, If you split it to two keylegend statment, legends will be located at different position.

Related

How do you reverse the axis and set the zero position for a polar plot in Julia / Plots?

When using Julia / plots heatmap polar plot, the default zero position for the theta axis is or the right ("East") with the angle increasing counter-clockwise, as shown in this example.
How do you reverse the axis and set the zero position for a polar plot in Matplotlib?
I want to have the zero in the "North" position and have a clockwise orientation. How can I do this ?
Thanks

How to add dashed lines parallel to axis lines in scatterplot 3D

I am trying to add dashed lines parallel to x,y,z axises in scatter plot 3D. Can I can modify following code to add dashed lines:
energy <- c(274.7539889,178.8493447,206.8084623,225.2049707,234.91386,359.9859873,251.4635995,406.480808,321.8857666,250.3560571)
time <- c(25.93572586,16.8826942,19.52192798,21.25848803,22.17497005,33.98130057,23.73720217,38.37023385,30.38478544,23.6326544)
cost <- c(6.861252677,13.22453603,13.53984311,4.355987685,21.9010971,16.41429768,15.33270222,27.78572681,26.44744434,24.62564853)
library(scatterplot3d)
with(mtcars, {scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
cex.lab=1.5,font.lab=2,lwd=3,pch=20,
highlight.3d = T, angle = 55
)})
By saving the return value from scatterplot3d you can use some functions from it to plot points and lines. For example:
p3 = scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
highlight.3d = T, angle = 55
)
p3$points3d(x=c(300,300),y=c(25,25),z=c(0,30),type="l",lty=2)
This is documented in the help for scatterplot3d:
Value:
[...]
points3d: function which draws points or lines into the existing plot.
There's also functions for drawing planes and a generic xyz.convert function to convert any x,y,z coordinate to a x,y coordinate on the graphics device.

How to rotate 180 degrees an mtext() in R

Apparently, mtext() in R doesn't support the srt parameter whose job is to rotate a piece of text.
I need mtext() to create an axis title on side 4 of my moving plot (i.e., values to be plotted come from a function so they change and so do the plot axes values). I was wondering then, what options do I have to rotate 180 degrees this side 4 axis title?
An example is BELOW:
curve(dnorm(x),-3,3)
mtext("Strength",side=4,srt=180)
You can use par("usr") to obtain extremes of the plot area and use it to place your text without having to explicitly specify the x and y.
Try
curve(dnorm(x),-3,3)
corners = par("usr") #Gets the four corners of plot area (x1, x2, y1, y2)
par(xpd = TRUE) #Draw outside plot area
text(x = corners[2]+.5, y = mean(corners[3:4]), "Strength", srt = 270)
This way it will always be on the right extreme and vertically in the middle.

Get data from lines() function

I have a plot like this (black points):
I calculate the average of some points (smoothing - the red points in the plot).
Then I can draw with the lines() function a line between these points (blue in the plot). What I want now is the data of the blue line.
Does somebody know how to do this? Is it even possible?
I used the approx function which interpolates between points:
approx(x = r, y = ionizations, n = 1000)

Tikzpicture - Arrow next to/under plot label

I am looking for a help with Latex. I'm new here so unfortunately I'm not allowed to post pictures yet.
I have a tikzpicture plot with labeled axes (x and y). Now I would like to add an arrow below the x-axis to indicate the direction of the subsequential treatments. In addition I would like to have an arrow left of the y-axis label pointing upwards. This arrow should have a second label saying "relative increase in Fe".
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[
%legend style={at={(1.05,0.05)}, %gibt Ort für Legende an
%anchor=south west},
%axis x line=bottom, % erzeugt x-Achse mit Pfeil
%axis y line=left, %
width=15.5cm,
height=10cm,
%scaled ticks=false,
%ymin=0,
xmin=-0.5,
xmax=5,
ymin=0,
ymax=5,
xtick={0,1,2,3,4},
xticklabels={Fe2O3,1,2,3,4},
bar width=50pt,
%ytick={},
%yticklabels={},
%use un%%ts,
%x unit=-,
%x unit prefix=,
%y unit=\frac{m}{s},
%y unit prefix=,
xlabel=Subsequential Treatments over Time ,
ylabel=Auger Peak to Peak Height Ratio Fe:O,
x tick label style= {rotate=90,anchor=east},
ybar stacked]
\addplot [draw=white, very thin]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
\addplot [draw= blue, fill=blue]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
%\node at (100,1) [orange!50!yellow]{\small{ZnO-h}};
\end{axis}
\end{tikzpicture}
\caption[Auger Spectrum of HOPG]{Auger Peak to Peak Height Ratios of Fe:O at an primary electron beam of \unit{2.0}{keV}.}
\label{Auger_ratio_histogram_}
\end{figure}
The code below illustrates two techniques for placing such arrows. The starting point of both is to add name=MyAxis to the axis options, which allows you to refer to the anchor points of the axis as with a normal node. A pgfplots axis also has anchors such as outer north east, which lies outside the axis descriptions, while north east lies on the corner of the axes.
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=MyAxis,
%legend style={at={(1.05,0.05)}, %gibt Ort für Legende an
%anchor=south west},
%axis x line=bottom, % erzeugt x-Achse mit Pfeil
%axis y line=left, %
width=15.5cm,
height=10cm,
%scaled ticks=false,
%ymin=0,
xmin=-0.5,
xmax=5,
ymin=0,
ymax=5,
xtick={0,1,2,3,4},
xticklabels={Fe2O3,1,2,3,4},
bar width=50pt,
%ytick={},
%yticklabels={},
%use un%%ts,
%x unit=-,
%x unit prefix=,
%y unit=\frac{m}{s},
%y unit prefix=,
xlabel=Subsequential Treatments over Time ,
ylabel=Auger Peak to Peak Height Ratio Fe:O,
x tick label style= {rotate=90,anchor=east},
ybar stacked]
\addplot [draw=white, very thin]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
\addplot [draw= blue, fill=blue]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
%\node at (100,1) [orange!50!yellow]{\small{ZnO-h}};
\end{axis}
\draw [-latex] ([yshift=-2ex]MyAxis.outer south west) --node[below]{Direction of subsequential treatments} ([yshift=-2ex]MyAxis.outer south east);
\draw [-latex] (MyAxis.outer south west) ++(-2ex,0) coordinate(start) --node[sloped,above] {relative increase in Fe} (start |- MyAxis.outer north west);
\end{tikzpicture}
\end{document}

Resources