【问题标题】:how to change the color mapping for legend如何更改图例的颜色映射
【发布时间】:2021-08-16 07:25:33
【问题描述】:

我们在一页中用相同的颜色映射绘制了点、平滑线和文本,我们只想在图例中选择一个(例如geom_point),如何更改图例。

library(ggplot2)
library(ggpubr)
data(mtcars)
df <- mtcars
df$cyl <- as.factor(df$cyl)

cor.test(df$mpg,df$wt)
b <- ggplot(df, aes(x = wt, y = mpg))
b + geom_point(aes(color = cyl))+
  geom_smooth(aes(color = cyl), method = lm,se = FALSE, fullrange = TRUE)+
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
  ggpubr::stat_cor(aes(color = cyl), method='pearson',label.x = 2.4,
                   r.digits = 3,p.digits = 4)+
  theme_bw()+
  theme(axis.title = element_text(family="Times",size=10),
        axis.text = element_text(family="Times",color="black",size=8),
        legend.title = element_text(family="HST",face="bold",size=8),
        legend.text=element_blank(),
        legend.key.size = unit(0.3,'cm'),
        legend.position = "top",
        legend.background = element_blank(),
        panel.grid = element_blank())

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在不想在图例中显示的图层上设置show.legend = FALSE

    library(ggplot2)
    library(ggpubr)
    data(mtcars)
    df <- mtcars
    df$cyl <- as.factor(df$cyl)
    
    ggplot(df, aes(x = wt, y = mpg)) + 
      geom_point(aes(color = cyl))+
      geom_smooth(aes(color = cyl), method = lm,se = FALSE, fullrange = TRUE, 
                  show.legend = FALSE)+
      scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
      ggpubr::stat_cor(aes(color = cyl), method='pearson',label.x = 2.4,
                       r.digits = 3,p.digits = 4, show.legend = FALSE)
    #> `geom_smooth()` using formula 'y ~ x'
    

    reprex package (v1.0.0) 于 2021-08-16 创建

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2014-08-10
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多