BookmarkSubscribeRSS Feed
Errant
Obsidian | Level 7

Hello,

 

I have an original data set that looks like this:originalearth.png

Thus far I've created a scatter plot using this code:

proc sgplot data=W2.earthquakes(where=(year >= 2000));
  	XAXIS type = discrete;
	Scatter X = year Y = magnitude;
	Title 'Magnitude of Earthquakes occuring in 2000 and beyond';
run;

And I get a graph that looks like this:earth2.png

 

 

I want to overlay the mean magnitude for each year on the same graph in a red line. I also want to incorporate reference lines that indicate different levels of earthquakes. These reference lines  would be light, moderate, strong, major, and great earthquakes and they would be defined at magnitudes of 4.0, 5.0, 6.0, 7.0, and 8.0. The ideal final output would look like the graph above but with the mean line and the additional reference lines running through it. 

 

I'm at a loss of how to do calculate the average within the sgplot proc. Do I need a create a new dataset with the average as a variable? Would it be possible to incorporate proc means within proc sgplot?

 

Thank you!

 

 

 

 

4 REPLIES 4
ballardw
Super User

@Errant wrote:

Hello,

 

I have an original data set that looks like this:originalearth.png

Thus far I've created a scatter plot using this code:

proc sgplot data=W2.earthquakes(where=(year >= 2000));
  	XAXIS type = discrete;
	Scatter X = year Y = magnitude;
	Title 'Magnitude of Earthquakes occuring in 2000 and beyond';
run;

And I get a graph that looks like this:earth2.png

 

 

I want to overlay the mean magnitude for each year on the same graph in a red line. I also want to incorporate reference lines that indicate different levels of earthquakes. These reference lines  would be light, moderate, strong, major, and great earthquakes and they would be defined at magnitudes of 4.0, 5.0, 6.0, 7.0, and 8.0. The ideal final output would look like the graph above but with the mean line and the additional reference lines running through it. 

 

I'm at a loss of how to do calculate the average within the sgplot proc. Do I need a create a new dataset with the average as a variable? Would it be possible to incorporate proc means within proc sgplot?

 

Thank you!

 

 

 

 


Reference lines are done with the REFLINE statement. Pretty simple.

The "mean per year" would require adding data.

Or perhaps you could change from a SCATTER to a VBOX plot. The VBOX plot shows mean and median and other distribution information.

DanH_sas
SAS Super FREQ

The mean values will need to be computed using PROC MEANS. You will need to match-merge that data set with the original data set so that you can use something like another SCATTER plot to display those values using a different symbol and color.

 

I also like ballardw's suggestion about using VBOX instead. In that case, you do not have to externally calculate the means. Instead, use a VBOX statement (with the NOOUTLIER option), and overlay your current SCATTER plot on top of it. That display will show you a lot of information.

 

As for your reference lines, you can use one or more REFLINE statements to draw (and label, if you want) your reference lines. 

Errant
Obsidian | Level 7

Hi Dan,

 

Thank you for your response. So I tried this 

proc sgplot data=HW2.earthquakes (where=(year >= 2000));
vbox magnitude/ category=year nofill nooutliers connect = mean lineattrs=(color=white);
scatter x=year y=magnitude ;
refline 4.0 /axis=y  label='light' lineattrs=(color=gray pattern=2)transparency=0.5;
refline 5.0 /axis=y label='moderate' lineattrs=(color=gray pattern=2)transparency=0.5;
refline 6.0 /axis=y label='strong' lineattrs=(color=gray pattern=2)transparency=0.5;
refline 7.0 /axis=y label='major' lineattrs=(color=gray pattern=2)transparency=0.5;
refline 8.0 /axis=y label='great' lineattrs=(color=gray pattern=2)transparency=0.5;
run;

 

And I get this output:earth3.PNG

How can I simultaneously get a scatter plot and with the vbox? Would that be more a layout overlay procedure?

 

 

DanH_sas
SAS Super FREQ

The VBOX actually IS drawn. However, you have NOFILL (which turned off the box fill color) and you have LINEATTRS=(color=white), which set the box plot outline to the same color as the wall color. Together, this makes it look like the box is not drawn. Just remove the LINEATTRS option, and you should get what you want.


Thanks!
Dan

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 739 views
  • 2 likes
  • 3 in conversation