Visualization: Data Visualization Quick Start

Brett Champion
Wolfram Research
wolfram-u@wolfram.com

Basic Features for Visualization Functions

Feature Classes

Styles

Colors, thickness, point size, opacity, …

BarChart[Range[5],ChartStyle->Orange,ImageSize->Small]

Element appearance and shape

BarChart[Range[5],ChartElements->,ImageSize->Small]

BarChart[Range[5],ChartElementFunction->"GlassRectangle",ImageSize->Small]

Labels

Textual labels and tooltips

BarChart[Range[5],ChartLabels->{"one","two","three","four","five"},ImageSize->Small]

Legends

BarChart[Range[5],ChartStyle->"Rainbow",ChartLegends->{"one","two","three","four","five"},ImageSize->Small]

Interactions

Built-in highlighting effects

BarChart[Range[5],ImageSize->Small]

Use elements for buttons, popup windows and other events

BarChart[{1,2,3,4,PopupWindow[5, "five"]},ImageSize->Small]

Metadata

Wrapper used to include additional information

BarChart[{1->0.8,2->2.1,3->1.7,4->3.8,5->5.3},ImageSize->Small]

Available to ChartElementFunction for custom appearances

BarChart[{1->0.4,2->1.3,3->1.7,4->3.3,5->5.3},ChartElementFunction->({ChartElementData["Rectangle"][##],Black,Thick,Line[{{#1[[1,1]],#3[[1]]},{#1[[1,2]],#3[[1]]}}]}&),ImageSize->Small]

Basic Features for Visualization Functions

Feature Scope

Options

Generally the easiest to use

Specified globally and uniformly

Examples: ChartStyle, ChartLabels, LabelingFunction, Background

BarChart[Range[5],ChartStyle->Orange,ChartElementFunction->"GlassRectangle",ChartLabels->Placed[Range[5],Above],ImageSize->Small]

Wrappers

Wrapped directly around data

Can be used at any level, allows for targeted use

Can be nested

Examples: Tooltip, Style, Button, Labeled

BarChart[
EventHandler[
{Labeled[1,"first"],Style[2,Red],PopupWindow[3,"three"],Tooltip[Style[4,Blue],"blue"]},"MouseClicked":>(Speak[Round[MousePosition["Graphics"],0.1]])],
ImageSize->Small]

Creating a Basic Plot

data=Table[{
country,
CountryData[country,"LiteracyFraction"],CountryData[country,"LifeExpectancy"],
CountryData[country,"Population"],
CountryData[country,"Continent"]
},{country,CountryData[]}];
data={#[[1]],100*#[[2]],#[[3]],#[[4]],#[[5]]}&/@DeleteMissing[data,1,1];

SmallGrid[data]

Create a basic plot:

BubbleChart[data[[All,{2,3,4}]]]

Creating a Basic Plot

BubbleChart[data[[All,{2,3,4}]],ChartLabels->Placed[data[[All,1]],Tooltip],FrameLabel->{"Literacy fraction","Life expectancy"},PlotLabel->"Life Expectancy vs. Literacy Rate vs. Population"]

Creating a Basic Plot

continentData=SplitBy[SortBy[data,Last],Last];

Length[continentData]

SmallGrid[First[continentData]]

continents=Last[First[#]]&/@continentData

label[{country_,x_,y_,z_,continent_}]:=Tooltip[{x,y,z},country]

splitdata=Map[label,continentData,{2}];

Short[InputForm[splitdata],5]

BubbleChart[splitdata,FrameLabel->{"Literacy fraction","Life expectancy"},PlotLabel->"Life Expectancy vs. Literacy Rate vs. Population",ChartLegends->continents]

Drilling Down

WolframAlpha["us population in 1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010,2020",{{"Result",1},"ComputableData"},TimeConstraint->60]

populationTimeSeries=WolframAlpha["us population in 1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010,2020",{{"Result",1},"ComputableData"},TimeConstraint->200]

{years,values}=Transpose[populationTimeSeries]

values=QuantityMagnitude/@values;

BarChart[values,ChartLabels->Placed[{years},{Below}]]

Drilling Down

base=Floor[Log10[First[values]],3]

BarChart[values,ChartLabels->Placed[{years,Round[values/10^base]},{Below,Above}],Axes->None,AspectRatio->1/3,PlotLabel->Row[{"Decennial Population in ",Switch[base,0,"People",3,"Thousands",6,"Millions",9,"Billions"]}]]

Drilling Down

orangedata=MapAt[Labeled[Style[#,LightOrange],"(est.)",Top]&,values,-1];
InputForm[orangedata]

BarChart[orangedata,ChartLabels->Placed[{years,Round[values/10^base]},{Below,Above}],Axes->None,AspectRatio->1/3,PlotLabel->Row[{"Decennial Population in ",Switch[base,0,"People",3,"Thousands",6,"Millions",9,"Billions"]}]]

Drilling Down

PopulationBarChart[country_]:=PopulationBarChart[country]=Block[{years,values,base},
{years, values}=Transpose[{#[[1]],QuantityMagnitude[#[[2]]]}&/@WolframAlpha[country<>" population in 1910,1920,1930",{{"Result",1},"ComputableData"},TimeConstraint->100]];
base=Floor[Log10[First[values]],3];
BarChart[MapAt[Labeled[Style[#,LightOrange],"(est.)",Top]&,values,-1],ChartLabels->Placed[{years,Round[values/10^base]},{Below,Above}],Axes->None,AspectRatio->1/3,PlotLabel->Row[{CountryData[country,"StandardName"]," Decennial Population in ",Switch[base,0,"People",3,"Thousands",6,"Millions",9,"Billions"]}]]
]

PopulationBarChart["Ireland"]

PopulationBarChart["Japan"]

Adding Metadata

richData=Table[{
country,
CountryData[country,"LiteracyFraction"],CountryData[country,"MaleLiteracyFraction"],CountryData[country,"FemaleLiteracyFraction"],CountryData[country,"LifeExpectancy"],
CountryData[country,"MaleLifeExpectancy"],CountryData[country,"FemaleLifeExpectancy"],CountryData[country,"Population"],
CountryData[country,"MalePopulation"],
CountryData[country,"FemalePopulation"],
CountryData[country,"Continent"]
},{country,CountryData[]}];
richData={#[[1]],100*#[[2]],100*#[[3]],100*#[[4]],#[[5]],#[[6]],#[[7]],#[[8]],#[[9]],#[[10]],#[[11]]}&/@DeleteMissing[richData,1,1];
richData=Replace[#,#[[1]]->CountryData[#[[1]],"StandardName"],2]&/@richData;

splitRichData=SplitBy[SortBy[richData,Last],Last];
SmallGrid[First[splitRichData]]

metalabel[{country_,lit_,mlit_,flit_,exp_,mexp_,fexp_,pop_,mpop_,fpop_,continent_}]:=Tooltip[{lit,exp,pop},country]->{country,{mlit,flit},{mexp,fexp},{mpop,fpop}}

metadata=Map[metalabel,splitRichData,{2}];

Short[InputForm[metadata],5]

renderBubble[{{xmin_,xmax_},{ymin_,ymax_}},{lit_,exp_,pop_},meta_]:=
Block[{country,mlit,flit,mexp,fexp,mpop,fpop},
{country,{mlit,flit},{mexp,fexp},{mpop,fpop}}=First[meta];
Mouseover[
Disk[{xmin+xmax,ymin+ymax}/2,{xmax-xmin,ymax-ymin}/2],
{
Disk[{xmin+xmax,ymin+ymax}/2,{xmax-xmin,ymax-ymin}/2],
{Black,
Inset[Grid[{{"","Lit.","L. Exp.","Pop"},{"M",mlit,mexp,mpop},{"F",flit,fexp,fpop}},Dividers->All,Background->White],Scaled[{0.02,0.98}],{Left,Top}]
}
}
]
]

BubbleChart[metadata,ChartElementFunction->renderBubble,FrameLabel->{"Literacy fraction","Life expectancy"},PlotLabel->"Life Expectancy vs. Literacy Rate vs. Population",ChartLegends->continents]

Putting Everything Together

metalabelButton[var_,{country_,lit_,mlit_,flit_,exp_,mexp_,fexp_,pop_,mpop_,fpop_,continent_}]:=Button[Tooltip[{lit,exp,pop},country]->{country,{mlit,flit},{mexp,fexp},{mpop,fpop}},var=country]

metadataButton=Map[metalabelButton[currentCountry,#]&,splitRichData,{2}];

InputForm[metadataButton[[1,1]]]

Panel[Column[Panel/@{BubbleChart[metadataButton,FrameLabel->{"Literacy fraction","Life expectancy"},ChartElementFunction->renderBubble,AspectRatio->0.6,PlotLabel->"Life Expectancy vs. Literacy Rate vs. Population",ChartLegends->Placed[continents,Bottom,Style[#,FontFamily->"Helvetica",FontSize->16]&]],Dynamic[If[StringQ[currentCountry],PopulationBarChart[currentCountry],""]]},Alignment->Center]]

Initialization

SmallGrid[data_]:=Grid[Take[data,5],Frame->All,FrameStyle->Lighter@Gray]

Clear[m8red];
m8red[a_]:=RGBColor[0.811765,0.117647,0.145098,a];

(*SetOptions[BubbleChart,
	ImageSize->Large,
	LabelStyle->{FontFamily->"Gill Sans", FontSize->16}
	];*)

PicturePrint[expr_,opts:OptionsPattern[Cell]]:=CellPrint[TextCell[expr,"Picture",opts,ShowStringCharacters->False]]

OverviewMouseover[header_,subs_]:=CellPrint@Cell[BoxData[ToBoxes@Mouseover[Framed[Style[header,"OverviewSection"],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5],

Evaluate@If[subs=="",Framed[Style[header,"OverviewSection",White],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5,Background->m8red[1]],Row[{Framed[Style[header,"OverviewSection",White],FrameMargins->{{15,15},{5,5}},FrameStyle->None,RoundingRadius->5,Background->m8red[1]]," » ",Framed[Style[subs,"OverviewSection"],FrameMargins->{{15,15},{5,5}},FrameStyle->m8red[1],BoxFrame->2,RoundingRadius->5,Background->White]}]]]],"OverviewSection"]

(*MapThread[OverviewMouseover,Transpose[{{"Chapter One","Chapter One topics, …"},{"Chapter Two","Chapter Two topics, …"},{"Chapter Three","Chapter Three topics, …"},{"Chapter Four","Chapter Four topics, …"}}]]*)

LargeOverviewMouseover[header_,subs_]:=CellPrint@Cell[BoxData[ToBoxes@Mouseover[Framed[Style[header,"LargeOverviewSection"],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5],

Evaluate@If[subs=="",Framed[Style[header,"LargeOverviewSection",White],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5,Background->m8red[1]],Row[{Framed[Style[header,"LargeOverviewSection",White],FrameMargins->{{15,15},{5,5}},FrameStyle->None,RoundingRadius->5,Background->m8red[1]]," » ",Framed[Style[subs,"LargeOverviewSection"],FrameMargins->{{15,15},{5,5}},FrameStyle->m8red[1],BoxFrame->2,RoundingRadius->5,Background->White]}]]]],"LargeOverviewSection"]

MapThread[LargeOverviewMouseover,Transpose[{
{"Basic Features for Visualization Functions","General overview of design principles"},
{"Creating a Basic Plot","Create and tweak a bubble chart"},
{"Drilling Down","Use wrappers to build a customized bar chart"},
{"Adding Metadata","Incorporate more information into the bubble chart"},
{"Putting Everything Together","Use the bubble chart as an interface to explore the data"}}]];