Graphics Reference
In-Depth Information
stead of using grid lines that run all the way across, this time we'll make the lines go only up to
the points, by using
geom_segment()
. Note that
geom_segment()
needs values for
x
,
y
,
xend
,
and
yend
:
ggplot(tophit, aes(x
=
avg, y
=
name))
+
geom_segment(aes(yend
=
name), xend
=
0
, colour
=
"grey50"
)
+
geom_point(size
=
3
, aes(colour
=
lg))
+
scale_colour_brewer(palette
=
"Set1"
, limits
=
c(
"NL"
,
"AL"
))
+
theme_bw()
+
theme(panel.grid.major.y
=
element_blank(),
# No horizontal grid lines
legend.position
=
c(
1
,
0.55
),
# Put legend inside plot area
legend.justification
=
c(
1
,
0.5
))
Another way to separate the two groups is to use facets, as shown in
Figure 3-31
.
The order in
which the facets are displayed is different from the sorting order in
Figure 3-30
; to change the
display order, you must change the order of factor levels in the
lg
variable:
ggplot(tophit, aes(x
=
avg, y
=
name))
+
geom_segment(aes(yend
=
name), xend
=
0
, colour
=
"grey50"
)
+
geom_point(size
=
3
, aes(colour
=
lg))
+
scale_colour_brewer(palette
=
"Set1"
, limits
=
c(
"NL"
,
"AL"
), guide
=
FALSE
FALSE
)
+
theme_bw()
+
theme(panel.grid.major.y
=
element_blank())
+
facet_grid(lg ~
.
, scales
=
"free_y"
, space
=
"free_y"
)