BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Hello, 

 

Using SAS 9.4, I have the attached dataset. I would like to create a mirror bar chart with the x and y axes as labelled in the datalines and the each side of the mirror being the group variable. Does anyone know any efficient code to make this happen? Thank you

data have;
	input yaxis $ xaxis group $;
	datalines;
	High		346	AHS
	Middle-High	220	AHS
	Middle-Low	268	AHS
	Low			312	AHS
	High		281	ASP
	Middle-High	276	ASP
	Middle-Low	278	ASP
	Low			248	ASP
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Like this:

data have;
	input group $ yaxis $ xaxis x2 x3;
  x2DL=abs(x2);
  x3DL=abs(x3);

	datalines;
	AHS	High		346	 346 .	
	AHS	Middle-High	220  220 .	
	AHS	Middle-Low	268	 268 .	
	AHS	Low			312	 312 .	
	ASP	High		-281 .	-281
	ASP	Middle-High	-276 .	-276
	ASP	Middle-Low	-278 .	-278
	ASP	Low			-248 .	-248
;
run;

ODS graphics / width=1200px height=800px;
proc sgplot data=have;
	styleattrs datacolors=(teal black);
	hbarparm category=yaxis response=xaxis / group=group nooutline name='x' dataskin=matte;
	scatter x=x2 y=yaxis /DATALABEL=x2DL DATALABELATTRS=(color=white size=12) DATALABELPOS=LEFT markerattrs=(size=0);
	scatter x=x3 y=yaxis /DATALABEL=x3DL DATALABELATTRS=(color=white size=12) DATALABELPOS=RIGHT markerattrs=(size=0);
	xaxis values=(-360 -340 -320 -300 -380 -260 -240 -220 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 
               0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360) 
  VALUESDISPLAY=("360" "340" "320" "300" "380" "260" "240" "220" "200" "180" "160" "140" "120" "100" "80" "60" "40" "20" 
                "0" "20" "40" "60" "80" "100" "120" "140" "160" "180" "200" "220" "240" "260" "280" "300" "320" "340" "360") 
  FITPOLICY=STAGGER
	label= 'Quartile Ranking';
	yaxis label= 'Frequency';
	label group= 'Source';
	title 'Figure 1. Total Correct Per Quartile of Ranking';
run;

yabwon_0-1709820065915.png

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

6 REPLIES 6
Reeza
Super User

See @Ksharp code in this post

https://communities.sas.com/t5/SAS-Programming/Proc-GCHART-display-variable-in-the-bar/m-p/914690

 


@GS2 wrote:

Hello, 

 

Using SAS 9.4, I have the attached dataset. I would like to create a mirror bar chart with the x and y axes as labelled in the datalines and the each side of the mirror being the group variable. Does anyone know any efficient code to make this happen? Thank you

data have;
	input yaxis $ xaxis group $;
	datalines;
	High		346	AHS
	Middle-High	220	AHS
	Middle-Low	268	AHS
	Low			312	AHS
	High		281	ASP
	Middle-High	276	ASP
	Middle-Low	278	ASP
	Low			248	ASP
;
run;

 

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Thank you for the direction that was quite helpful. I updated my dataset to accommodate the example code, see attached. I am still having a few issues based on my code.

1. On the x axis the numbers are negative, is there a way to make both sides of the axis positive?

2. The labels on the bars show at the extreme ends so that not all of the number is showing, is there a way to center the labels on the bar?

3. Is there a way to adjust the tick values? I would prefer the value to be in increments of 20. I tried using values=() and listing the values but that has not worked. 

 

Thank you 

data have;
	input group $ yaxis $ xaxis x2 x3;
	datalines;
	AHS	High		346	 346 .	
	AHS	Middle-High	220  220 .	
	AHS	Middle-Low	268	 268 .	
	AHS	Low			312	 312 .	
	ASP	High		-281 .	-281
	ASP	Middle-High	-276 .	-276
	ASP	Middle-Low	-278 .	-278
	ASP	Low			-248 .	-248
;
run;

proc sgplot data=have;
	styleattrs datacolors=(teal black);
	hbarparm category=yaxis response=xaxis / group=group nooutline name='x' dataskin=matte;
	scatter x=x2 y=yaxis /markerchar=x2 labelstrip markercharattrs=(color=white size=12);
	scatter x=x3 y=yaxis /markerchar=x3 labelstrip markercharattrs=(color=white size=12);
	xaxis values=(-360 -340 -320 -300 -380 -260 -240 -220 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360) 
	label= 'Quartile Ranking';
	yaxis label= 'Frequency';
	label group= 'Source';
	title 'Figure 1. Total Correct Per Quartile of Ranking';
run;
yabwon
Onyx | Level 15

Like this:

data have;
	input group $ yaxis $ xaxis x2 x3;
  x2DL=abs(x2);
  x3DL=abs(x3);

	datalines;
	AHS	High		346	 346 .	
	AHS	Middle-High	220  220 .	
	AHS	Middle-Low	268	 268 .	
	AHS	Low			312	 312 .	
	ASP	High		-281 .	-281
	ASP	Middle-High	-276 .	-276
	ASP	Middle-Low	-278 .	-278
	ASP	Low			-248 .	-248
;
run;

ODS graphics / width=1200px height=800px;
proc sgplot data=have;
	styleattrs datacolors=(teal black);
	hbarparm category=yaxis response=xaxis / group=group nooutline name='x' dataskin=matte;
	scatter x=x2 y=yaxis /DATALABEL=x2DL DATALABELATTRS=(color=white size=12) DATALABELPOS=LEFT markerattrs=(size=0);
	scatter x=x3 y=yaxis /DATALABEL=x3DL DATALABELATTRS=(color=white size=12) DATALABELPOS=RIGHT markerattrs=(size=0);
	xaxis values=(-360 -340 -320 -300 -380 -260 -240 -220 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 
               0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360) 
  VALUESDISPLAY=("360" "340" "320" "300" "380" "260" "240" "220" "200" "180" "160" "140" "120" "100" "80" "60" "40" "20" 
                "0" "20" "40" "60" "80" "100" "120" "140" "160" "180" "200" "220" "240" "260" "280" "300" "320" "340" "360") 
  FITPOLICY=STAGGER
	label= 'Quartile Ranking';
	yaxis label= 'Frequency';
	label group= 'Source';
	title 'Figure 1. Total Correct Per Quartile of Ranking';
run;

yabwon_0-1709820065915.png

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

A bit more practical version:

ODS graphics / width=1200px height=800px;

%macro plotMe(st,ed,by);
proc sgplot data=have;
	styleattrs datacolors=(teal black);
	hbarparm category=yaxis response=xaxis / group=group nooutline name='x' dataskin=matte;
	scatter x=x2 y=yaxis /DATALABEL=x2DL DATALABELATTRS=(color=white size=12) DATALABELPOS=LEFT markerattrs=(size=0);
	scatter x=x3 y=yaxis /DATALABEL=x3DL DATALABELATTRS=(color=white size=12) DATALABELPOS=RIGHT markerattrs=(size=0);
	xaxis values=(
  %do i=&st. %to &ed. %by &by.;
    &i.
  %end;
  )
  VALUESDISPLAY=(
  %do i=&st. %to &ed. %by &by.;
    "%sysfunc(ABS(&i.))"
  %end;
  ) 
  FITPOLICY=STAGGER
	label= 'Quartile Ranking';
	yaxis label= 'Frequency';
	label group= 'Source';
	title 'Figure 1. Total Correct Per Quartile of Ranking';
run;

%mend plotMe;

%plotMe(-360,360,20);
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
This is perfect. Thank you
Rick_SAS
SAS Super FREQ

For anyone looking at this thread, I believe that the "mirror bar" graph is more commonly called a butterfly plot or sometimes a tornado chart. For information about creating a butterfly plot in SAS, see

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
  • 6 replies
  • 528 views
  • 2 likes
  • 4 in conversation