Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人知道为什么我的传奇的标题不是';t更新?(ggplot2,堆叠条形图)_Ggplot2_Bar Chart - Fatal编程技术网

有人知道为什么我的传奇的标题不是';t更新?(ggplot2,堆叠条形图)

有人知道为什么我的传奇的标题不是';t更新?(ggplot2,堆叠条形图),ggplot2,bar-chart,Ggplot2,Bar Chart,出于某种原因,尽管在代码块中的所有可能位置添加了+labs(fill=“Survival status”),但我无法将图例的标题从“surv_status”(变量名)更新为“Survival status” 有什么建议吗?谢谢大家! ggplot(data_train, aes(x = axil_nodes, fill = as.factor(surv_status))) + geom_bar(position = "fill") + labs(fill = &qu

出于某种原因,尽管在代码块中的所有可能位置添加了+labs(fill=“Survival status”),但我无法将图例的标题从“surv_status”(变量名)更新为“Survival status”

有什么建议吗?谢谢大家!

ggplot(data_train, aes(x = axil_nodes, fill = as.factor(surv_status))) +
  geom_bar(position = "fill") +
   labs(fill = "Survival status") +
  theme_classic() +
  xlab("Number of positive axillary nodes") +
  ggtitle("Visualizing positive axillary node count by survival status") +   
  theme(legend.background = element_rect(color = "black", linetype = "dashed")) +
  scale_fill_discrete(name = "surv_status", labels = c("Survived", "Deceased"))

使用
ggplot2
可以使用几种不同的方法更改图例的标题。我从脑海中知道三个:

  • 使用
    labs(fill=“Legend Title”)
    ,其中
    fill
    可以被与图例相关的任何美学修饰语所取代

  • 使用
    scale.*.**()
    函数和
    name=
    参数。例如,
    scale\u color\u discrete(name=“Legend Title”)

  • 在一个
    guide
    函数中使用
    guides()
    并声明
    title=
    。例如,
    guides(shape=guides\u legend(title=“legend title”)

如果使用多种方法处理标题,则优先级为:

  • guides()
    方法
  • scale\u*\ u**()
    方法
  • labs()
    方法
  • 使用与映射关联的名称-即在
    aes()
    函数中指定的列的名称
  • 下面的例子清楚地说明了这一点。您可以在
    gg
    对象的每个引用之间移动,但优先级似乎保持不变。绘图中带有以下代码的任何顺序的最终图例将根据
    guides()
    命名图例

    如果我删除
    guides()
    部分,图例名称现在将反映
    scale\u color\u discrete()


    在您的示例中,您有
    labs(fill=“Survival status”)
    以及
    scale\u fill\u discrete(name=“surv\u status”…
    。使用上述规则,您应该了解更改
    labs()
    函数永远不会优先于
    scale\u fill\u discrete()
    。您可以从
    scale\u fill\u discrete()
    中删除
    name
    的参数,或者在
    scale\u fill\u discrete()中更改
    name=
    的值
    更改图例标题。

    使用
    ggplot2
    可以使用几种不同的方法更改图例标题。我知道有三种方法:

    • 使用
      labs(fill=“Legend Title”)
      ,其中
      fill
      可以被与图例相关的任何美学修饰语所取代

    • 使用
      scale.*.**()
      函数和
      name=
      参数。例如,
      scale\u color\u discrete(name=“Legend Title”)

    • 使用
      guides()
      并在其中一个
      guide
      函数中声明
      title=
      。例如,
      guides(shape=guide\u legend(title=“legend title”)

    如果使用多种方法处理标题,则优先级为:

  • guides()
    方法
  • scale\u*\ u**()
    方法
  • labs()
    方法
  • 使用与映射关联的名称-即在
    aes()
    函数中指定的列的名称
  • 因此,下面的示例清楚地说明了这一点。您可以移动对
    gg
    对象的每个引用,但优先级似乎保持不变。绘图中具有以下代码任何顺序的最终图例将导致根据
    guides()
    命名图例

    如果我删除
    guides()
    部分,图例名称现在将反映
    scale\u color\u discrete()

    在您的示例中,您有
    labs(fill=“Survival status”)
    以及
    scale\u fill\u discrete(name=“surv\u status”…
    。使用上述规则,您应该了解更改
    labs()
    函数永远不会优先于
    scale\u fill\u discrete()
    。您可以从
    scale\u fill\u discrete()
    中删除
    name
    的参数,或者在
    scale\u fill\u discrete()
    中更改
    name=
    的值,以更改图例标题。

    回答非常好(+1)。为了完整性,您可以在第一个相关的
    aes()
    函数中添加默认值取自映射的第四点,非常好的答案(+1)。为了完整性,您可以在第一个相关的
    aes()
    函数中添加默认值取自映射的第四点,
    ggplot(df, aes(x,y, color=z)) + geom_point() +
      guides(color=guide_legend(title='My other other legend')) +
      scale_color_discrete(name='My Legend') +
      labs(color='My other Legend')
    
    ggplot(df, aes(x,y, color=z)) + geom_point() +
      scale_color_discrete(name='My Legend') +
      labs(color='My other Legend')