Sam Dickson
Sam Dickson

Reputation: 5249

How can I combine a scatter point and a series line in the legend of SGPLOT?

I would like to present data using PROC SGPLOT as a scatter plot with error bars connected by a line and have the legend use both the point from the scatter statement and the line from the series statement.

Here are some data:

data dat;
  input x y high low grp $;
  cards;
1 2.50 2.90 2.00 A
2 1.90 2.35 1.45 A
3 1.75 2.25 1.25 A
1 2.10 2.50 1.70 B
2 2.00 2.40 1.60 B
3 1.80 2.20 1.40 B
;
run;

The code I would like to adjust:

proc sgplot data=dat;
  scatter x=x y=y / yerrorupper=high yerrorlower=low group=grp
                    groupdisplay=cluster clusterwidth=0.1
                    markerattrs=(size=7 symbol=circlefilled);
  series x=x y=y / group=grp groupdisplay=cluster clusterwidth=0.1;
  xaxis label='Time' values=(1,2,3);
  yaxis label='Response' min=0 max=3.5;
  keylegend / location=inside position=topright title="Group";
run;

The current output:

enter image description here

I would like the legend to include just one instance each of A and B and for the symbol to be on top of the line in the legend as it is in the plot. How can I do this?

Upvotes: 2

Views: 4566

Answers (1)

DomPazz
DomPazz

Reputation: 12465

Add the markers to the SERIES plot and then only use the series for the legend.

proc sgplot data=dat;
  scatter x=x y=y / yerrorupper=high yerrorlower=low group=grp
                    groupdisplay=cluster clusterwidth=0.1
                    name="scatter";
  series x=x y=y / markers markerattrs=(size=7 symbol=circlefilled)
                   group=grp groupdisplay=cluster clusterwidth=0.1 name="series";
  xaxis label='Time' values=(1,2,3);
  yaxis label='Response' min=0 max=3.5;
  keylegend "series" / location=inside position=topright title="Group";
run;

It produces this, which I think is what you want: enter image description here

Upvotes: 2

Related Questions