【问题标题】:Removing default ggplot legend and create a customized one删除默认 ggplot 图例并创建自定义图例
【发布时间】:2021-02-02 14:21:16
【问题描述】:

我想使用ggplot 绘制条形图。当我定义所有参数时,ggplot 将根据数据框列名自动添加图例。这是我的数据框:

         shift  Var      Ave
        <dbl> <chr>     <dbl>
1           0 Ave_los    268
2           0 Ave_los_n  195
3           1 Ave_los    284
4           1 Ave_los_n  217
5           2 Ave_los    214
6           2 Ave_los_n  194

这是我用来绘制的代码:

ggplot(data=data3, aes(x=shift, y=Ave, fill=Var)) +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_x_continuous(breaks=c(0:2)) +
  geom_text(aes(label=round(Ave,digit=2)), vjust=1.6, color="black", position = position_dodge(0.9), size=2.3)

这个绘图的结果如下所示:

但是,我不想使用 Ave_losAve_los_n 作为图例,而是想用 平均时间(积极组)替换它们平均时间(否定组)。我该怎么做?

【问题讨论】:

标签: r ggplot2 bar-chart legend


【解决方案1】:

使用 tidyverse 中的重新编码更改“Var”列的值。给定here

library(tidyverse)
    data3%>%recode(Ave, Ave_los = "Average Time (positive group)", Ave_los_n = "Average Time (negative group")%>%ggplot(data=data3, aes(x=shift, y=Ave, fill=Var))+geom_bar(stat="identity", position=position_dodge())+ scale_x_continuous(breaks=c(0:2)) +  geom_text(aes(label=round(Ave,digit=2)), vjust=1.6, color="black", position = position_dodge(0.9), size=2.3)

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 2014-08-26
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多