无法在分层ggplot中更改图例标题

时间:2015-02-18 16:54:26

标签: r ggplot2

我有以下图表:

ggplot(data = centmerge, aes(x=long, y=lat, group = centmerge$group)) +
geom_polygon(stat = "identity", fill = "green", color = "black") +
geom_point(data = centavg, aes(x = long, y = lat, group = Performance.Country.Name, size = Action_Absolute_Value/1000000))+  
ggtitle("Contract Costs") +
coord_map("polyconic")+
theme(axis.text.y = element_blank(), axis.text.x = element_blank(), axis.title.x=element_blank(), 
axis.title.y=element_blank())

它创建了以下图表:

enter image description here

我的问题是我无法改变geom_point的任何美学。例如,我无法更改图表上的点的颜色,我无法更改图例标题。所有添加内容如:

theme(axis.text.y = element_blank(), axis.text.x = element_blank(), axis.title.x=element_blank(), 
    axis.title.y=element_blank())

最后只会影响geom_polygon()。如果我试图改变圆圈的颜色,它会恢复为淡红色,但我无法进一步改变它,我没有运气使用theme(),scale_fill_discrete(),labs()或任何方法更改标题。我首先要更改图例标题,还要更改地图上圆圈的颜色。我可以更改地图的颜色,但不能更改圆圈。

2 个答案:

答案 0 :(得分:1)

借鉴评论,这是一种建议的方法。

在Action_Absolute_Value / 1000000的数据框中创建一个变量,将该变量命名为你想要的图例标题,并解决你的图例标题命名问题。

关于点的着色,根据Gregor的评论,在geom_point调用中添加color = "red" [或您选择的任何颜色]。

答案 1 :(得分:1)

您似乎对主题与图层存在很多混淆。编辑主题适用于轴和网格线的颜色,但不是标记的首选方法。 labs函数适用于所有维度的标签,x,y,颜色,大小等,以及标题。

对于点的颜色,只需告诉geom_point你想要的颜色。

ggplot(data = centmerge, aes(x=long, y=lat, group = centmerge$group)) +
  geom_polygon(stat = "identity", fill = "green", color = "black") +
  geom_point(data = centavg,
             aes(x = long, y = lat, group = Performance.Country.Name,
                 size = Action_Absolute_Value/1000000),
             # color goes outside of aes() because it's constant for all points
             color = "peachpuff3") +  
  coord_map("polyconic") +
  labs(x = "", y = "",
       # size will give the name for the size legend
       size = "Action Absolute Value (millions)",
       title = Contract Costs")
  theme(axis.text.y = element_blank(), axis.text.x = element_blank())