【问题标题】:ggplot color legend shape mixes alphanumeric and shapeggplot 颜色图例形状混合字母数字和形状
【发布时间】:2016-09-02 07:45:59
【问题描述】:

在 ggplot 中包含 ggrepel 导致图例中的形状很有趣:

问:如何将其替换为正常形状?

示例代码:

data(mtcars)
library(ggplot2)
library(ggrepel)

ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs), size = factor(cyl))) +
  geom_point() +
  geom_text_repel(aes(label = rownames(mtcars)), size = 5)

会话信息:

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=nl_NL.UTF-8      
[8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
  [1] magrittr_1.5       ggrepel_0.5        ggplot2_2.1.0.9000

loaded via a namespace (and not attached):
  [1] labeling_0.3     colorspace_1.2-6 scales_0.4.0     assertthat_0.1   plyr_1.8.4       rsconnect_0.4.3  tools_3.2.3      gtable_0.2.0     tibble_1.1       Rcpp_0.12.6      grid_3.2.3       digest_0.6.10   
[13] munsell_0.4.3  

【问题讨论】:

    标签: r ggplot2 ggrepel


    【解决方案1】:

    ggrepel 有show.legend 参数,所以我们需要show.legend = FALSE 如下:

    show.legend
    合乎逻辑。这层应该包含在图例中吗? NA,默认值,包括是否映射了任何美学。 FALSE 从不 包括,TRUE 总是包括。

    ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs), size = factor(cyl))) +
      geom_point() +
      geom_text_repel(aes(label = rownames(mtcars)), size = 5, show.legend = FALSE)
    

    为了清楚起见,请在 ggplot 之外准备数据:

    #fix the data
    plotDat <- mtcars
    plotDat$vs <- as.factor(plotDat$vs)
    plotDat$cyl <- as.numeric(as.factor(plotDat$cyl))
    plotDat$myLabel <- rownames(plotDat)
    
    #then plot
    ggplot(plotDat, aes(x = mpg, y = wt,
                        color = vs, size = cyl,
                        label = myLabel)) +
      geom_point() +
      geom_text_repel(size = 5, show.legend = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2016-12-29
      • 2022-11-27
      相关资源
      最近更新 更多