ggplot2标签为color = factor(foo)

时间:2012-06-12 22:01:11

标签: r ggplot2

在ggplot2图上很难处理标签。以下是示例页面中的类似情节:

mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() 
mt + facet_grid(. ~ cyl, scales = "free")

如何定义factor(cyl)的标签列表?

2 个答案:

答案 0 :(得分:4)

我会在data.frame本身中执行此操作:

mtcars$cyl_factor <- factor(mtcars$cyl, labels=c('Four', 'Six', 'Eight'))

ggplot(mtcars, aes(mpg, wt, colour = cyl_factor)) + 
  geom_point() +
  facet_grid(. ~ cyl, scales = "free")

答案 1 :(得分:2)

您可以在色标的labels参数中定义它:

ggplot(mtcars, aes(mpg, wt, colour=factor(cyl))) + 
  geom_point() +
  scale_colour_discrete(breaks = c("4", "6", "8"),
                        labels = c("Four", "Six", "Eight"))