Plotting Bar Graphs with Individual Values

classic Classic list List threaded Threaded
5 messages Options
JSB
Reply | Threaded
Open this post in threaded view
|

Plotting Bar Graphs with Individual Values

JSB
I am trying to plot bar graphs + SE that also incorporates all the individual values for each bar/category. Here is an example: https://www.researchgate.net/figure/271219084_fig6_Figure-9-Differential-expression-of-HCN-subunits-between-the-SOC-nuclei-underlies

Is this possible to do in SPSS? I have a sample syntax that plots the bar graphs below. I have not been able to add the individual values or find a source that discusses how to do that. The syntax is based on the Wide data format, but I can also transform it into a Long format, if absolutely necessary. 

GGRAPH 
  /GRAPHDATASET NAME="graphdataset" VARIABLES= MEANSE(VAR1, 1)  MEANSE(VAR2, 1) MEANSE(VAR3, 1)
VarCateg MISSING=LISTWISE REPORTMISSING=NO 
    TRANSFORM=VARSTOCASES(SUMMARY="#SUMMARY" INDEX="#INDEX" LOW="#LOW" HIGH="#HIGH") 
  /GRAPHSPEC SOURCE=INLINE. 
BEGIN GPL 
  SOURCE: s=userSource(id("graphdataset")) 
  DATA: SUMMARY=col(source(s), name("#SUMMARY")) 
  DATA: INDEX=col(source(s), name("#INDEX"), unit.category()) 
  DATA: VarCateg=col(source(s), name("VarCateg"), unit.category()) 
  DATA: LOW=col(source(s), name("#LOW")) 
  DATA: HIGH=col(source(s), name("#HIGH")) 
  COORD: rect(dim(1,2), cluster(3,0)) 
  GUIDE: axis(dim(2), label("Mean")) 
  GUIDE: text.subfootnote(label("Error Bars: +/- 1 SE")) 
  GUIDE: legend(aesthetic(aesthetic.color.interior), label("Treatment")) 
  SCALE: cat(dim(3), include("0", "1", "2", "3")) 
  SCALE: linear(dim(2), include(0)) 
  SCALE: cat(aesthetic(aesthetic.color.interior), include("1", "2", "3")) 
  SCALE: cat(dim(1), include("1", "2", "3")) 
  ELEMENT: interval(position(VarCateg*SUMMARY*INDEX), color.interior(VarCateg), 
    shape.interior(shape.square)) 
  ELEMENT: interval(position(region.spread.range(VarCateg*(LOW+HIGH)*INDEX)), 
    shape.interior(shape.ibeam)) 
END GPL.

Thank you.
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Plotting Bar Graphs with Individual Values

Andy W
See
https://andrewpwheeler.wordpress.com/2012/02/20/avoid-dynamite-plots-visualizing-dot-plots-with-super-imposed-confidence-intervals-in-spss-and-r/.
If you post an example of your data I can show an example on your variables.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
JSB
Reply | Threaded
Open this post in threaded view
|

Re: Plotting Bar Graphs with Individual Values

JSB
Thanks Andy. My data is below - it has an additional level of grouping than the example in your blog. Also, I had trouble with SEMs with your code: in SPSS 25, the error bars are invisible and I couldn't find a way to change their color. In SPSS 23 they appear as very light gold color, and again, I couldn't change that.

Here is the structure of my data, along with the bar graphs where I want to add the scatter dots:

DATA LIST LIST
/ Treatment(A7)    IFL    GRP    NMD    GLU.
BEGIN DATA
Start    32.96    404.5    574.09    125.31
Start    27    386.36    429.34    70.88
Start    43.7    763.11    490.21    74.5
Start    22.25    787    101.83    51.1
Start    14.87    563.34    146.14    59.2
Middle    40.18    423.11    432.26    72.85
Middle    47.99    766    457.65    78.3
Middle    26.04    232.59    38.43    49.69
Middle    26.36    390.02    143.08    60.65
Middle    24.6    486.07    90.29    55.21
Middle    28.63    513.72    105.81    55.13
End    37.4    395.91    362.45    68.1
End    37.74    868.85    392.57    88.31
End    29.95    355.62    172.46    60.65
End    14.73    245.57    90.29    40.23
End    16.86    656.03    45.65    48.29
END DATA.

DATASET NAME BarsValues.

* Chart Builder.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=MEANSE(IFL, 1) MEANSE(GRP, 1) MEANSE(NMD, 1)
    MEANSE(GLU, 1) Treatment MISSING=LISTWISE REPORTMISSING=NO
    TRANSFORM=VARSTOCASES(SUMMARY="#SUMMARY" INDEX="#INDEX" LOW="#LOW" HIGH="#HIGH")
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: SUMMARY=col(source(s), name("#SUMMARY"))
  DATA: INDEX=col(source(s), name("#INDEX"), unit.category())
  DATA: Treatment=col(source(s), name("Treatment"), unit.category())
  DATA: LOW=col(source(s), name("#LOW"))
  DATA: HIGH=col(source(s), name("#HIGH"))
  COORD: rect(dim(1,2), cluster(3,0))
  GUIDE: axis(dim(2), label("Mean"))
  GUIDE: legend(aesthetic(aesthetic.color.interior), label("Treatment"))
  GUIDE: text.subfootnote(label("Error Bars: +/- 1 SE"))
  SCALE: cat(dim(3), include("0", "1", "2", "3"))
  SCALE: linear(dim(2), include(0))
  ELEMENT: interval(position(Treatment*SUMMARY*INDEX), color.interior(Treatment),
    shape.interior(shape.square))
  ELEMENT: interval(position(region.spread.range(Treatment*(LOW+HIGH)*INDEX)),
    shape.interior(shape.ibeam))
END GPL.

VARSTOCASES
  /ID=id
  /MAKE scores FROM IFL GRP NMD GLU
  /INDEX=VarType(scores)
  /KEEP=Treatment
  /NULL=KEEP.




On Fri, Oct 6, 2017 at 11:51 AM, Andy W <[hidden email]> wrote:
See
https://andrewpwheeler.wordpress.com/2012/02/20/avoid-dynamite-plots-visualizing-dot-plots-with-super-imposed-confidence-intervals-in-spss-and-r/.
If you post an example of your data I can show an example on your variables.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Plotting Bar Graphs with Individual Values

Andy W
Using clustering seems to mess up the dodged dot plot, so I recreated the
same effect using small multiple panels. It is a bit busy, and in the figure
caption you would want to put something like the circles are the original
datapoints, the square is the mean, and the bars and plus/minus two standard
errors. But it gets the point across.

***************************************************************.
*Adding the mean and standard error into the dataset.
AGGREGATE OUTFILE=* MODE=ADDVARIABLES
  /BREAK Treatment VarType
   /MeanScore = MEAN(Scores)
   /SDScore = SD(Scores)
   /NGroup = N.

*Now calculating the standard error of the mean.
COMPUTE StandErr = SDScore/SQRT(NGroup).
COMPUTE LowS = MeanScore - 2*StandErr.
COMPUTE HighS = MeanScore + 2*StandErr.
FORMATS scores LowS HighS MeanScore (F4.0).
EXECUTE.

*Now make a chart of the points and +- 2 standard errors.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=VarType scores Treatment
MeanScore LowS HighS
    MISSING=VARIABLEWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  PAGE: begin(scale(900px,500px))
  SOURCE: s=userSource(id("graphdataset"))
  DATA: VarType=col(source(s), name("VarType"), unit.category())
  DATA: scores=col(source(s), name("scores"))
  DATA: Treatment=col(source(s), name("Treatment"), unit.category())
  DATA: LowS=col(source(s), name("LowS"))
  DATA: HighS=col(source(s), name("HighS"))
  DATA: MeanScore=col(source(s), name("MeanScore"))
  COORD: rect(dim(1,2))
  GUIDE: axis(dim(2), label("Scores"), delta(100))
  GUIDE: axis(dim(1))
  GUIDE: axis(dim(3), opposite())
  SCALE: cat(dim(1), reverse())
  SCALE: linear(dim(2), min(0), max(900))
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  GUIDE: legend(aesthetic(aesthetic.color.exterior), null())
  ELEMENT: point.dodge.symmetric(position(bin.dot(Treatment*scores*VarType,
dim(2))), color.interior(Treatment), color.exterior(Treatment),
           size(size."9"), transparency.interior(transparency."0.8"))
  ELEMENT:
edge(position(region.spread.range(Treatment*(LowS+HighS)*VarType)),
color.interior(Treatment), size(size."0.9"))
  ELEMENT: point(position(Treatment*MeanScore*VarType),
color.interior(Treatment), color.exterior(Treatment), shape(shape.square),
size(size."16"),
           transparency.interior(transparency."1"))
  PAGE: end()
END GPL.

*No color version.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=VarType scores Treatment
MeanScore LowS HighS
    MISSING=VARIABLEWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  PAGE: begin(scale(900px,500px))
  SOURCE: s=userSource(id("graphdataset"))
  DATA: VarType=col(source(s), name("VarType"), unit.category())
  DATA: scores=col(source(s), name("scores"))
  DATA: Treatment=col(source(s), name("Treatment"), unit.category())
  DATA: LowS=col(source(s), name("LowS"))
  DATA: HighS=col(source(s), name("HighS"))
  DATA: MeanScore=col(source(s), name("MeanScore"))
  COORD: rect(dim(1,2))
  GUIDE: axis(dim(2), label("Scores"), delta(100))
  GUIDE: axis(dim(1))
  GUIDE: axis(dim(3), opposite())
  SCALE: cat(dim(1), reverse())
  SCALE: linear(dim(2), min(0), max(900))
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  GUIDE: legend(aesthetic(aesthetic.color.exterior), null())
  ELEMENT: point.dodge.symmetric(position(bin.dot(Treatment*scores*VarType,
dim(2))), color.interior(color.black), color.exterior(color.black),
           size(size."9"), transparency.interior(transparency."0.8"))
  ELEMENT:
edge(position(region.spread.range(Treatment*(LowS+HighS)*VarType)),
color.interior(color.black), size(size."0.9"))
  ELEMENT: point(position(Treatment*MeanScore*VarType),
color.interior(color.darkgrey), color.exterior(color.black),
shape(shape.square), size(size."16"),
           transparency.interior(transparency."1"))
  PAGE: end()
END GPL.
***************************************************************.

<http://spssx-discussion.1045642.n5.nabble.com/file/t329824/DotPlot.png>

The way this is set up there is no intrinsic need for color, although
aesthetically I do not mind the color. Your problem you describe with the
color previously has to do with SPSS's defaults, you could map whatever
category to receive whatever color you want.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
JSB
Reply | Threaded
Open this post in threaded view
|

Re: Plotting Bar Graphs with Individual Values

JSB
Excellent. Thanks a lot. Your blog is very helpful for visualizations with SPSS, so I've been trying to learn some of the GPL syntax by trial and error. I especially liked your heatmaps and corrgrams. Are you aware of any good books that teach the syntax for graphing? I consulted the Command Syntax Reference in SPSS, but didn't find much/any information about graphing details. For example, I was trying to figure out how to replace the squares with a line for the mean, as is usually done, and I couldn't find it. Doing "shape.line" didn't work. Closest I can come is "shape.ibeam," but that removes the symbols for the mean altogether. The same issue concerns changing the colors of each category so it matches the rest of the color coded graphs I have, but when I alter the "black" in the colorless version's syntax, the colors change for all groups. In the color graph syntax, I don't see a place where I can modify the color:

ELEMENT: point.dodge.symmetric(position(bin.dot(Treatment*scores*VarType,
dim(2))), color.interior(Treatment), color.exterior(Treatment),
           size(size."9"), transparency.interior(transparency."0.8"))


On Wed, Oct 11, 2017 at 7:45 AM, Andy W <[hidden email]> wrote:
Using clustering seems to mess up the dodged dot plot, so I recreated the
same effect using small multiple panels. It is a bit busy, and in the figure
caption you would want to put something like the circles are the original
datapoints, the square is the mean, and the bars and plus/minus two standard
errors. But it gets the point across.

***************************************************************.
*Adding the mean and standard error into the dataset.
AGGREGATE OUTFILE=* MODE=ADDVARIABLES
  /BREAK Treatment VarType
   /MeanScore = MEAN(Scores)
   /SDScore = SD(Scores)
   /NGroup = N.

*Now calculating the standard error of the mean.
COMPUTE StandErr = SDScore/SQRT(NGroup).
COMPUTE LowS = MeanScore - 2*StandErr.
COMPUTE HighS = MeanScore + 2*StandErr.
FORMATS scores LowS HighS MeanScore (F4.0).
EXECUTE.

*Now make a chart of the points and +- 2 standard errors.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=VarType scores Treatment
MeanScore LowS HighS
    MISSING=VARIABLEWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  PAGE: begin(scale(900px,500px))
  SOURCE: s=userSource(id("graphdataset"))
  DATA: VarType=col(source(s), name("VarType"), unit.category())
  DATA: scores=col(source(s), name("scores"))
  DATA: Treatment=col(source(s), name("Treatment"), unit.category())
  DATA: LowS=col(source(s), name("LowS"))
  DATA: HighS=col(source(s), name("HighS"))
  DATA: MeanScore=col(source(s), name("MeanScore"))
  COORD: rect(dim(1,2))
  GUIDE: axis(dim(2), label("Scores"), delta(100))
  GUIDE: axis(dim(1))
  GUIDE: axis(dim(3), opposite())
  SCALE: cat(dim(1), reverse())
  SCALE: linear(dim(2), min(0), max(900))
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  GUIDE: legend(aesthetic(aesthetic.color.exterior), null())
  ELEMENT: point.dodge.symmetric(position(bin.dot(Treatment*scores*VarType,
dim(2))), color.interior(Treatment), color.exterior(Treatment),
           size(size."9"), transparency.interior(transparency."0.8"))
  ELEMENT:
edge(position(region.spread.range(Treatment*(LowS+HighS)*VarType)),
color.interior(Treatment), size(size."0.9"))
  ELEMENT: point(position(Treatment*MeanScore*VarType),
color.interior(Treatment), color.exterior(Treatment), shape(shape.square),
size(size."16"),
           transparency.interior(transparency."1"))
  PAGE: end()
END GPL.

*No color version.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=VarType scores Treatment
MeanScore LowS HighS
    MISSING=VARIABLEWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  PAGE: begin(scale(900px,500px))
  SOURCE: s=userSource(id("graphdataset"))
  DATA: VarType=col(source(s), name("VarType"), unit.category())
  DATA: scores=col(source(s), name("scores"))
  DATA: Treatment=col(source(s), name("Treatment"), unit.category())
  DATA: LowS=col(source(s), name("LowS"))
  DATA: HighS=col(source(s), name("HighS"))
  DATA: MeanScore=col(source(s), name("MeanScore"))
  COORD: rect(dim(1,2))
  GUIDE: axis(dim(2), label("Scores"), delta(100))
  GUIDE: axis(dim(1))
  GUIDE: axis(dim(3), opposite())
  SCALE: cat(dim(1), reverse())
  SCALE: linear(dim(2), min(0), max(900))
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  GUIDE: legend(aesthetic(aesthetic.color.exterior), null())
  ELEMENT: point.dodge.symmetric(position(bin.dot(Treatment*scores*VarType,
dim(2))), color.interior(color.black), color.exterior(color.black),
           size(size."9"), transparency.interior(transparency."0.8"))
  ELEMENT:
edge(position(region.spread.range(Treatment*(LowS+HighS)*VarType)),
color.interior(color.black), size(size."0.9"))
  ELEMENT: point(position(Treatment*MeanScore*VarType),
color.interior(color.darkgrey), color.exterior(color.black),
shape(shape.square), size(size."16"),
           transparency.interior(transparency."1"))
  PAGE: end()
END GPL.
***************************************************************.

<http://spssx-discussion.1045642.n5.nabble.com/file/t329824/DotPlot.png>

The way this is set up there is no intrinsic need for color, although
aesthetically I do not mind the color. Your problem you describe with the
color previously has to do with SPSS's defaults, you could map whatever
category to receive whatever color you want.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD