Graphics Reference
In-Depth Information
levels(birthwt1$smoke)
"0" "1"
library(plyr)
# For the revalue function
birthwt1$smoke
<-
revalue(birthwt1$smoke, c(
"0"
=
"No Smoke"
,
"1"
=
"Smoke"
))
Now when we plot it again, it shows the new labels (
Figure 6-12
, right):
ggplot(birthwt1, aes(x
=
bwt))
+
geom_density()
+
facet_grid(smoke ~
.
)
If you want to see the histograms along with the density curves, the best option is to use facets,
since other methods of visualizing both histograms in a single graph can be difficult to interpret.
To do this, map
y=..density..
, so that the histogram is scaled down to the height of the dens-
ity curves. In this example, we'll also make the histogram bars a little less prominent by changing
the colors (
Figure 6-13
)
:
ggplot(birthwt1, aes(x
=
bwt, y
=
..
density..))
+
geom_histogram(binwidth
=
200
, fill
=
"cornsilk"
, colour
=
"grey60"
, size
=
.2
)
+
geom_density()
+
facet_grid(smoke ~
.
)