向ggplot2添加图例

我正在尝试在ggplot2上添加图例。

当我使用以下代码时,我得到以下图。

ggplot() + 
  geom_bar(data = profitCountries,aes(y = (revenue),x = residence),stat="identity",fill="darkgreen",color = "black") +

  geom_bar(data = profitCountries,aes(y = -(total_spend),fill="red",color = "black") +

  geom_line(data = profitCountries,aes(y = total_profit,x = residence,group = 1),size = 1.5,color = "blue"  ) + 

  scale_y_continuous(breaks = seq(-500000,500000,100000),limits=c(-500000,500000) ) +
  xlab('Countries') + ggtitle('Campaign spending and revenue by countries') + 
  ylab('Campaign spending                                     Revenue')  + theme_grey()

向ggplot2添加图例

根据其他帖子的建议,我在color内添加了aes()。当我尝试使用以下代码进行操作时,得到以下图表。

ggplot() + 
  geom_bar(data = profitCountries,fill="darkgreen"),fill="red"),group = 1,color = "blue"),size = 2 ) + 

  scale_y_continuous(breaks = seq(-500000,500000) ) +
  xlab('Countries') + ggtitle('Campaign spending and revenue by countries') + 
  ylab('Campaign spending                                     Revenue')  + theme_grey()

向ggplot2添加图例

在第二个图中,颜色改变,并创建了两个图例。反正有解决办法吗?

XHLIZIMING 回答:向ggplot2添加图例

您当前正在将绿色的字符矢量映射到自动确定其颜色的手动比例。

您可能想要

    Person person = new Person
{
    Name = "Nigal Newborn",Age = 1
};

string jsonIncludeNullValues = JsonConvert.SerializeObject(person,Formatting.Indented);

Console.WriteLine(jsonIncludeNullValues);
// {
//   "Name": "Nigal Newborn",//   "Age": 1,//   "Partner": null,//   "Salary": null
// }

string jsonIgnoreNullValues = JsonConvert.SerializeObject(person,Formatting.Indented,new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(jsonIgnoreNullValues);
// {
//   "Name": "Nigal Newborn",//   "Age": 1
// }
本文链接:https://www.f2er.com/3092335.html

大家都在问