R Club

ggplot2 Chapter 5 errata

posted by: Mike Covington

Setting the plot title

On pages 67 - 68, there are several lines of code that use + opts(title = "geom_xxxx") to set the plot titles. Since opts() is deprecated and should no longer be used, proper alternatives are:

+ labs(title = "geom_xxxx")
## OR
+ ggtitle("geom_xxxx")

Finding a missing function

When you try to plot the right-side panel of Figure 5.4 on pg. 71, you will likely get an error:

library(ggplot2)
qplot(carat, depth, data = diamonds, geom = "boxplot",
  group = round_any(carat, 0.1, floor), xlim = c(0, 3))
## Error in eval(expr, envir, enclos) : could not find function "round_any"

Let’s try to use the round_any() function without qplot().

round_any(1234, 100)
## Error: could not find function "round_any"

We get the same error, so let’s see if we can figure out what’s up with round_any().

?round_any
## No documentation for 'round_any' in specified packages and libraries:
## you could try '??round_any'

OK, let’s follow that advice…

??round_any

In the window that pops up, we see that round_any() is found in both plyr and reshape. These packages are both dependencies of ggplot2 and the round_any() function is the same in each. For access to the function, we need to load one of them.

library(plyr)
?round_any

Now we get the help file for round_any() and the function actually works:

round_any(1234, 100)
## [1] 1200

Another function that you need later will also be loaded with this approach: ddply()

Specifying the transparency of a color

The code on pg. 73-74 for building the plots in Figures 5.8 and 5.9 uses a deprecated method for setting the transparency of a color:

df <- data.frame(x = rnorm(100000), y = rnorm(100000))
norm <- ggplot(df, aes(x, y))
norm + geom_point(colour = alpha("black", 1/3))
## Error in do.call("layer", list(mapping = mapping, data = data, stat = stat,  :
##   could not find function "alpha"

Instead, color and alpha must be set separately:

norm + geom_point(colour = "black", alpha = 1/3)

Removing a legend

Since opts() has been deprecated, legends should not be removed with + opts(legend.position = "none"). The proper method is now uses theme().

+ theme(legend.position = "none")

Geom vs. Stat

On page 75, stat_bin2d() and stat_binhex() are used when geom_bin2d() and geom_hex() would have given the same results. This is becuase they are the default geoms for these stats (and vice versa). In such a case, it is probably more simple to just specify the geom since that is what we are used to.

Later on, however, stat_density2d(geom = "point") and stat_density2d(geom = "tile") are used to demonstrate how to use the non-default geom for a stat.

Others

There are more issues with the code in this chapter. But this will get you started. I’ll update this when I have a chance.

comments powered by Disqus