user2907249
user2907249

Reputation: 879

Cannot alter legend title in layered ggplot

I have the following chart:

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())

It creates the following chart:

enter image description here

My problem is that I cannot alter any of the aesthetics of the geom_point. For instance I cannot change the color of the points on the graph and I cannot change the legend title. All additions such as the:

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

at the end will affect only the geom_polygon(). If I try to change the color of the circles, it reverts to a pale red but then I cannot change it further, and I have had no luck changing the title using theme(), scale_fill_discrete(), labs() or any method. I first want to change the legend title, but also the color of the circles on the map. I can change the color of the map, but not the circles.

Upvotes: 0

Views: 1007

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 146224

You seem to have a lot of confusion about themes vs. layers. Editing the theme is good for things like the colors of axes and gridlines, but is not the preferred way to do labeling. The labs function works for labels of all dimensions, x, y, color, size, etc., as well as title.

For the color of the points, just tell geom_point what color you want.

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())

Upvotes: 1

lawyeR
lawyeR

Reputation: 7674

Drawing on the comments, here is a proposed approach.

Create a variable in your data frame for Action_Absolute_Value/1000000), name that variable what you want the legend title to be, and solve your legend-title naming problem.

As to coloring the points, per Gregor's comment, add color = "red" [or whatever color you choose] inside the geom_point call.

Upvotes: 1

Related Questions