微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 facet_grid 和文本位置更改直方图的颜色?

如何解决如何使用 facet_grid 和文本位置更改直方图的颜色?

如何更改使用 facet_grid 绘制的两个直方图的颜色? 我想要绿色的“活动”和红色的“失败”的直方图。

此外,如何更改行文本的位置以使其更向下一点?

这是我的代码

df %>%
  ggplot(aes(x = `Banks/turnover%Last avail. yr`)) +
  geom_histogram(
      bins = nclass.Sturges(`Banks/turnover%Last avail. yr`),colour = "black")+
  geom_vline(data = status_means,aes(xintercept = avg),color="red",linetype="dashed")+
  geom_text(data = status_means,aes(x = avg,label = avg),y= Inf )+
  theme(legend.position="None")+
  facet_grid(~general_status)

enter image description here

解决方法

您需要在 export const sendCode = (id,username) => (dispatch) => { dispatch({ some code here }); return registerAccount .sendCode(id,username) .then((response) => { dispatch({ payload: response.data.verifyCode }); return response.data; }) .catch(() => { return null; }); }; export const getCodeAndVerify = (id,userCode) => (dispatch) => { dispatch({ some code here }); const getVerifyCode = // I need to get response.data.verifyCode from sendCode above // I try to use // const getVerifyCode = { verifyCode: sendCode() }; // but this returns [object object] return registerAccount .getCodeAndVerify(id,userCode,getVerifyCode) .then(() => { // some code here }) .catch(() => { // some code here }); }; 参数中添加一个因子。在这里,我只使用 fill,因为我不确定您是如何在数据中区分的。

active-failed

然后,您可以添加一条线来定义颜色:

ggplot(df_all,aes(x = `Banks/turnover%Last avail. yr`,fill = as.factor(active-failed)))

正如@teunbrand 所说,您可以使用 scale_fill_manual(values = c("green","red")) 移动文本,使其不被截断。

记得给 good reproducible example,通过 vjust 提供一些数据会很有帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。