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..
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.