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

Hello community,

I would like to plot a barplot with sgplot. I am using this code to plot this:


proc sgplot data=elapsed_long_perc;
    /* Definisci i colori personalizzati per ogni fascia */
    styleattrs datacolors=(LIO MOO STO GRRO DARO); 

/* 	vbarparm category=Fascia response=Percentuale / */
/*    limitlower=max limitupper=min; */

    /* Creazione del barplot con colori distinti per ogni fascia */
    vbar Fascia / response=Percentuale datalabel 
                  group=Fascia 
                  datalabelattrs=(size=12); 


    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);
    
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run;

harmonic_0-1740583116213.png


I would like to put a line that follow my bars like that

harmonic_1-1740583236280.png


And put the values of min and max as an interval like that

harmonic_2-1740583440096.png

or like that but with different color bars

harmonic_3-1740583492630.png


Thank you in advance




 



 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Sure. Check this:


proc sgplot data=elapsed_long_perc noautolegend;
    /* Definizione dei colori personalizzati per ogni fascia */
    styleattrs datacolors=(CREAM PAPPK BIOY VIGY VPAG); 

    /* Creazione del barplot senza bordi */
    vbarparm category=Fascia response=Percentuale / 
        nooutline group=Fascia limitlower=max limitupper=min limitattrs=(color=LTGRAY )
        groupdisplay=cluster;

    *scatter x=Fascia y=Percentuale /datalabel=Percentuale datalabelpos=center markerattrs=(size=0);


    /* Aggiunta delle barre di errore con maggiore spessore */
    highlow x=Fascia low=min high=max / 
        lineattrs=(thickness=3 color=LTGRAY); 

    /* Linea curva che segue i valori */
    series x=Fascia y=Percentuale / smoothconnect
        smoothconnect lineattrs=(color=LIYBR thickness=2 pattern=solid);

    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);

    /* Aggiunta delle etichette ai valori */
	text x=Fascia y=Percentuale text=Percentuale / position=left textattrs=(weight=bold  size=12) discreteoffset=-0.1;
    scatter x=Fascia y=min /datalabel=min datalabelpos=center markerattrs=(size=0) datalabelattrs=(size=11  color=black) discreteoffset=0.2;
    scatter x=Fascia y=max /datalabel=max datalabelpos=center markerattrs=(size=0) datalabelattrs=(size=11 weight=bold color=white) discreteoffset=0.2;
/* text x=Fascia y=min text=min / position=bottomright textattrs=(size=10); */
/* text x=Fascia y=max text=max / position=topright textattrs=(size=10); */
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run;

Ksharp_0-1740704602366.png

 

View solution in original post

15 REPLIES 15
DanH_sas
SAS Super FREQ

The CSV has the response values already formatted, so I couldn't try this solution out, but try the following:

  1. Add a SERIES plot after the VBARPARM statement to draw the line:
    series x=Fascia y=Percentuale / smoothconnect;
  2. On the VBARPARM, add GROUPDISPLAY=CLUSTER. This will allow the group colors to be applied to the bars and also display the limit bars.

Hope this helps!

harmonic
Quartz | Level 8

I am using this code

proc sgplot data=elapsed_long_perc;
    /* Definisci i colori personalizzati per ogni fascia */
    styleattrs datacolors=(LIO MOO STO GRRO DARO); 

	vbarparm category=Fascia response=Percentuale /
   limitlower=max limitupper=min groupdisplay=cluster;

	series x=Fascia y=Percentuale / smoothconnect;

    /* Creazione del barplot con colori distinti per ogni fascia */
/*     vbar Fascia / response=Percentuale datalabel  */
/*                   group=Fascia  */
/*                   datalabelattrs=(size=12);  */

    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);
    
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run;

And I see this plot:

harmonic_0-1740586545905.png



Is there a way to put the colors manually?

DanH_sas
SAS Super FREQ
Add GROUP=Fascia to the bar chart.
harmonic
Quartz | Level 8
Thank you, it works! Is there a way to put the percentage values on top of the bar as well? Something like datalabel.
DanH_sas
SAS Super FREQ

If the DATALABEL option does not work for you due to the limit bars, you can use a TEXT plot to put the labels on the bars. Put this after your SERIES statement:

text x=Fascia y=Percentuale  text=Percentuale / position=bottom;

You can use TEXTATTRS on the statement to customize the text appearance.

harmonic
Quartz | Level 8
Is there a way to put the min and max values as well maybe smaller than the mean?

DanH_sas
SAS Super FREQ

There are a different ways to do this. If it were me, I would put the MIN/MAX values in an XAXISTABLE. Try add this statement and see if you like it:

xaxistable min max / position=top location=inside;

If you want all three on the end of the bar, it can be done with TEXT plots, but it will take more work to either determine the correct locations or do some string manipulations with your data. Try the axis table first and see what you think.

Ksharp
Super User
proc import datafile='C:\Users\xiakeshan\Downloads\ELAPSED_LONG_PERC.csv' out=elapsed_long_perc dbms=csv replace;
run;

proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); vbarparm category=Fascia response=Percentuale /datalabel group=Fascia groupdisplay=cluster datalabelattrs=(size=12) ; series x=Fascia y=Percentuale /lineattrs=(thickness=4 color=greybb); /* Personalizzazione degli assi */ yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12); xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12); /* Titolo del grafico */ title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno"; run;

Ksharp_2-1740625918027.png

 

 

 



proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); vbarparm category=Fascia response=Percentuale /nooutline group=Fascia ; highlow x=Fascia high=max low=min /type=bar nooutline transparency=0.2 lowlabel=Percentuale labelattrs=(size=12 color=black ); /* Personalizzazione degli assi */ yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12); xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12); /* Titolo del grafico */ title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno"; run;

Ksharp_1-1740625908529.png

 

 

 

 



proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); vbarparm category=Fascia response=Percentuale / group=Fascia groupdisplay=cluster limitlower=max limitupper=min limitattrs=(color=grey) ; /* Personalizzazione degli assi */ yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12); xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12); /* Titolo del grafico */ title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno"; run;

Ksharp_0-1740625899345.png

 

 

harmonic
Quartz | Level 8

Thank you,

harmonic_0-1740645406504.png

 

Is there a way to put the numbers inside the bars like that

harmonic_1-1740645464543.png

 

Ksharp
Super User

Sure. You want this one ?

 



proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); 
    vbarparm category=Fascia  response=Percentuale /nooutline group=Fascia seglabel seglabelattrs=(size=12 color=white)  ; 
    highlow x=Fascia high=max low=min /type=bar nooutline transparency=0.2; 


    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);
    
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run; 

Ksharp_0-1740646322856.png

 

 

harmonic
Quartz | Level 8

Something like that

harmonic_0-1740646704565.png

 

Or with tha bars like that

harmonic_1-1740646862569.png

In this last case can I modify the width of the line?



Ksharp
Super User
Sure. But I have no time. I have to leave now.
Tomorrow, if I have time ,you will post a new code .
Ksharp
Super User

OK. Since I have some time:



proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); 
    vbarparm category=Fascia  response=Percentuale /nooutline group=Fascia    ; 
    highlow x=Fascia high=max low=min /type=bar nooutline transparency=0.2; 
    scatter x=Fascia y=Percentuale /datalabel=Percentuale datalabelpos=top markerattrs=(size=0);
    scatter x=Fascia y=min /datalabel=min datalabelpos=top markerattrs=(size=0)  ;
    scatter x=Fascia y=max /datalabel=max datalabelpos=top markerattrs=(size=0) datalabelattrs=(color=white);

    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);
    
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run; 

Ksharp_0-1740648787590.png

 




proc sgplot data=elapsed_long_perc;
styleattrs datacolors=(LIO MOO STO GRRO DARO); 
    vbarparm category=Fascia  response=Percentuale / group=Fascia groupdisplay=cluster limitlower=max limitupper=min limitattrs=(color=grey) ; 
    scatter x=Fascia y=Percentuale /datalabel=Percentuale datalabelpos=center markerattrs=(size=0);
    scatter x=Fascia y=min /datalabel=min datalabelpos=center markerattrs=(size=0) ;
    scatter x=Fascia y=max /datalabel=max datalabelpos=center markerattrs=(size=0) datalabelattrs=(color=white);

    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);
    
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run; 

Ksharp_1-1740648824389.png

 

harmonic
Quartz | Level 8

I custom my code like that


proc sgplot data=elapsed_long_perc noautolegend;
    /* Definizione dei colori personalizzati per ogni fascia */
    styleattrs datacolors=(CREAM PAPPK BIOY VIGY VPAG); 

    /* Creazione del barplot senza bordi */
    vbarparm category=Fascia response=Percentuale / 
        nooutline group=Fascia limitlower=max limitupper=min 
        groupdisplay=cluster;

    *scatter x=Fascia y=Percentuale /datalabel=Percentuale datalabelpos=center markerattrs=(size=0);


    /* Aggiunta delle barre di errore con maggiore spessore */
    highlow x=Fascia low=min high=max / 
        lineattrs=(thickness=3 color=LTGRAY); 

    /* Linea curva che segue i valori */
    series x=Fascia y=Percentuale / 
        smoothconnect lineattrs=(color=LIYBR thickness=2 pattern=solid);

    /* Personalizzazione degli assi */
    yaxis label="Variazione Percentuale (%)" labelattrs=(size=14) valueattrs=(size=12);
    xaxis label="Frequenza pedonale annuale" discreteorder=data labelattrs=(size=14) valueattrs=(size=12);

    /* Aggiunta delle etichette ai valori */
	text x=Fascia y=Percentuale text=Percentuale / position=left textattrs=(weight=bold  size=12);
    scatter x=Fascia y=min /datalabel=min datalabelpos=center markerattrs=(size=0) datalabelattrs=(size=11  color=black);;
    scatter x=Fascia y=max /datalabel=max datalabelpos=center markerattrs=(size=0) datalabelattrs=(size=11 weight=bold color=white);
/* text x=Fascia y=min text=min / position=bottomright textattrs=(size=10); */
/* text x=Fascia y=max text=max / position=topright textattrs=(size=10); */
    /* Titolo del grafico */
    title height=14pt "Variazione Elapsed medio rispetto a 2 volte all'anno";
run;

And I obtained this plot

harmonic_0-1740650402188.png


Is there the offset option to move the percentage values a little bit?



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 4811 views
  • 11 likes
  • 3 in conversation