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;
I would like to put a line that follow my bars like that
And put the values of min and max as an interval like that
or like that but with different color bars
Thank you in advance
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;
The CSV has the response values already formatted, so I couldn't try this solution out, but try the following:
Hope this helps!
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:
Is there a way to put the colors manually?
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.
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.
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;
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;
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;
Thank you,
Is there a way to put the numbers inside the bars like that
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;
Something like that
Or with tha bars like that
In this last case can I modify the width of the line?
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;
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;
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
Is there the offset option to move the percentage values a little bit?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.