Graphics Reference
In-Depth Information
Discussion
The lowly pie chart is the subject of frequent abuse from data visualization experts. If you're
thinking of using a pie chart, consider whether a bar graph (or stacked bar graph) would convey
the information more effectively. Despite their faults, pie charts do have one important virtue:
everyone knows how to read them.
Creating a Map
Problem
You want to create a geographical map.
Solution
Retrieve map data from the maps package and draw it with
geom_polygon()
(which can have
a color fill) or
geom_path()
(which can't have a fill). By default, the latitude and longitude will
be drawn on a Cartesian coordinate plane, but you can use
coord_map()
and specify a projec-
tion. The default projection is
"mercator"
, which, unlike the Cartesian plane, has a progress-
ively changing spacing for latitude lines (
Figure 13-32
):
library(maps)
# For map data
# Get map data for USA
states_map
<-
map_data(
"state"
)
ggplot(states_map, aes(x
=
long, y
=
lat, group
=
group))
+
geom_polygon(fill
=
"white"
, colour
=
"black"
)
# geom_path (no fill) and Mercator projection
ggplot(states_map, aes(x
=
long, y
=
lat, group
=
group))
+
geom_path()
+
coord_map(
"mercator"
)