【问题标题】:Separating geom_point & geom_path plot layers in ggplot (R)在 ggplot (R) 中分离 geom_point 和 geom_path 绘图层
【发布时间】: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。该示例是完全可复制的。

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    试试这个。首先我只使用xy 作为全局美学。第二个而不是将 geom_point 中的家庭映射到 color 上,我将其映射到 fill 上,这增加了第二个图例。该解决方案的一个缺点。您必须从填充的形状中进行选择,例如填充点的形状 21。点的大小可以通过size 参数设置,而颜色可以设置,例如通过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"))
    

    reprex package (v0.3.0) 于 2020 年 3 月 31 日创建

    【讨论】:

      猜你喜欢
      • 2020-07-31
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多