有人知道为什么我的传奇的标题不是';t更新?(ggplot2,堆叠条形图)
出于某种原因,尽管在代码块中的所有可能位置添加了+labs(fill=“Survival status”),但我无法将图例的标题从“surv_status”(变量名)更新为“Survival status” 有什么建议吗?谢谢大家!有人知道为什么我的传奇的标题不是';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
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')