【问题标题】:ggplot2 labels for color=factor(foo)颜色=因子(foo)的ggplot2标签
【发布时间】:2013-08-15 03:16:12
【问题描述】:

在 ggplot2 图中很难使用标签。以下是示例页面中的类似图:

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

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

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我会在 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")
    

    【讨论】:

      【解决方案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"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-12
        • 1970-01-01
        • 2016-12-16
        • 1970-01-01
        • 1970-01-01
        • 2020-07-11
        相关资源
        最近更新 更多