Graphics Reference
In-Depth Information
p
<-
ggplot(cdat, aes(x
=
healthexp, y
=
infmortality, size
=
GDP))
+
geom_point(shape
=
21
, colour
=
"black"
, fill
=
"cornsilk"
)
# GDP mapped to radius (default with scale_size_continuous)
p
# GDP mapped to area instead, and larger circles
p
+
scale_size_area(max_size
=
15
)
Figure 5-34. Left: balloon plot with value mapped to radius; right: with value mapped to area
Discussion
The example here is a scatter plot, but that is not the only way to use balloon plots. It may also
be useful to use them to represent values on a grid, where the x- and y-axes are categorical, as
in
Figure 5-35
:
# Add up counts for male and female
hec
<-
HairEyeColor[,,
"Male"
]
+
HairEyeColor[,,
"Female"
]
# Convert to long format
library(reshape2)
hec
<-
melt(hec, value.name
=
"count"
)
ggplot(hec, aes(x
=
Eye, y
=
Hair))
+
geom_point(aes(size
=
count), shape
=
21
, colour
=
"black"
, fill
=
"cornsilk"
)
+
scale_size_area(max_size
=
20
, guide
=
FALSE
FALSE
)
+
geom_text(aes(y
=
as.numeric(Hair)
-
sqrt(count)
/
22
, label
=
count), vjust
=
1
,
colour
=
"grey60"
, size
=
4
)