【发布时间】:2020-03-31 19:11:44
【问题描述】:
我的情节由三个圆圈和两个点组成。我希望完成两个看似简单但实际上很困难的任务。我希望 1) 创建两个图例 & 2) 更改家庭点的形状、大小和颜色。使用以下函数生成的圆圈...
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))
}
然后我用 3 个不同的输入调用该函数,为每个圆生成 100 个 x-y 点
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)
然后我创建我的点数据
hh <- as.data.frame(matrix(c(.25,.5,.25,.5,1,2), 2, 3)) %>%
set_names(c("x", "y", "Household"))
到目前为止,我有两个不同的数据集,两个点都遵循 aes(x,y)。但是,它们的分组方式不同:第一个数据集按“邻居”分组,第二个数据集按“家庭”分组。
然后我绘制我到目前为止所拥有的东西..
# 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)))
这是我的输出..
那我为什么要在这里寻求帮助呢?我希望 1) 创建两个图例,一个用于 Neighborhood,另一个用于 Households & 2) 更改家庭点的形状、大小和颜色。由于它们的两个点图,R 不允许我将图的美学分开 (aes()),这导致我无法完成任务 1 和 2。该示例是完全可复制的。
【问题讨论】: