3

My plot consists of three circles and two points. I am hoping to complete two, seemingly simple but proving difficult, tasks. I am hoping to 1) Create two legends & 2) change the household point's shape, size, and color. The circles generated using the following function...

circleFun <- function(center,diameter, npoints){
  # recovered from 
  # https://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}

I then call the function with 3 different inputs to generate 100 x-y points for each cirlce

A <- circleFun(c(0,0), 1, npoints=100) %>%
        cbind("A") %>%
          set_names(c("x", "y", "Neighborhood"))
B <- circleFun(c(.5, .5), 1, npoints=100) %>%
         cbind("B") %>%
          set_names(c("x", "y", "Neighborhood"))

C <- circleFun(c(1, 1), 1, npoints=100) %>%
      cbind("C") %>%
        set_names(c("x", "y", "Neighborhood"))
neigh <- rbind(A, B, C)

I then create my point data

hh <- as.data.frame(matrix(c(.25,.5,.25,.5,1,2), 2, 3)) %>%
        set_names(c("x", "y", "Household"))

Thus far I have two different data sets, both points, both following aes(x,y). However, their grouping is different: the first data set is grouped by "Neighborhood", the second is grouped by "Household".

I then plot what I have thus far..

  # Plot Neighborhoods and set up plot specifics
  c <- ggplot(data=neigh, aes(x,y, group = Neighborhood, color = Neighborhood)) +
  geom_path(size = 1.5) +
  xlab("Quality of Public Amenities") +
  ylab("Price of Housing") +
  ggtitle("Figure 2.5") +
  theme(panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        plot.title = element_text(hjust=0.5, face = 'bold', size = 14)) 

 # Add corresponding household points
 c+geom_point(data=hh, aes(x=x,y=y,group = as.factor(Household), color = as.factor(Household)))

This is my output.. enter image description here So why am I asking for help here? I am hoping to 1) Create two legends, one for Neighborhood and another for Households & 2) change the household point's shape, size, and color. Due to the fact that their both point plots, R is not letting me separate the aesthetics of the plots (aes()) which is causing me to not fulfill tasks 1 & 2. The example is fully replicable.

DPek
  • 180
  • 2
  • 15

1 Answers1

2

Try this. First I use only x and y as global aesthetics. Second instead of mapping Household in geom_point on color I map it on fill which adds a second legend. One drawback of this solution. You have to chose from the filled shapes, e.g. shape 21 for filled points. The size of the points can be set via the size argument, while the colors can be set e.g. via scale_fill_manual.

library(ggplot2)
library(dplyr)
library(purrr)

 # Plot Neighborhoods and set up plot specifics
c <- ggplot(data=neigh, aes(x,y)) +
  geom_path(aes(group = Neighborhood, color = Neighborhood), size = 1.5) +
  xlab("Quality of Public Amenities") +
  ylab("Price of Housing") +
  ggtitle("Figure 2.5") +
  theme(panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        plot.title = element_text(hjust=0.5, face = 'bold', size = 14)) 

# Add corresponding household points
c + 
  geom_point(data=hh, aes(group = as.factor(Household), fill = as.factor(Household)), shape = 21, color = "transparent", size = 2) +
  scale_fill_manual(name = "Household", values = c("black", "orange"))

Created on 2020-03-31 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51