Graphics Reference
In-Depth Information
str(cabbage_exp)
'data.frame' : 6 obs. of 6 variables:
$ Cultivar: Factor w / 2 levels "c39" , "c52" : 1 1 1 2 2 2
$ Date : Factor w / 3 levels "d16" , "d20" , "d21" : 1 2 3 1 2 3
$ Weight : num 3.18 2.8 2.74 2.26 3.11 1.47
$ sd : num 0.957 0.279 0.983 0.445 0.791 ...
$ n : int 10 10 10 10 10 10
$ se : num 0.3025 0.0882 0.311 0.1408 0.2501 ...
In the mtcars example, cyl is numeric, so it is treated as a continuous variable. Because of this,
even though the actual values of cyl include only 4, 6, and 8, the legend has entries for the inter-
mediate values 5 and 7. To make ggplot() treat cyl as a categorical variable, you can convert it
to a factor in the call to ggplot() , or you can modify the data so that the column is a character
vector or factor ( Figure 12-3 ):
# Convert to factor in call to ggplot()
ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) + geom_point()
# Another method: Convert to factor in the data
m <- mtcars # Make a copy of mtcars
m$cyl <- factor(m$cyl) # Convert cyl to a factor
ggplot(m, aes(x = wt, y = mpg, colour = cyl)) + geom_point()
Search WWH ::




Custom Search