首页 > 解决方案 > r中的gg图和geom_point函数

问题描述

给定下面的代码,我试图可视化以下分层气泡图:所有起始站的点。大小随皮卡的总数而变化。所有终点站的点。大小随返回的总数而变化。我最终需要一个名为 p1 的 ggplot 对象,两层的 alpha = 0.5。

library(lubridate)
library(tidyverse)
nycbikes18 <- read_csv("data/2018-citibike-tripdata.csv",
locale = locale(tz = "America/New_York"))
nycbikes18

#> # A tibble: 333,687 x 15
#>    tripduration starttime           stoptime           
#>           <dbl> <dttm>              <dttm>             
#>  1          932 2018-01-01 02:06:17 2018-01-01 02:21:50
#>  2          550 2018-01-01 12:06:18 2018-01-01 12:15:28
#>  3          510 2018-01-01 12:06:56 2018-01-01 12:15:27
#>  4          354 2018-01-01 14:53:10 2018-01-01 14:59:05
#>  5          250 2018-01-01 17:34:30 2018-01-01 17:38:40
#>  6          613 2018-01-01 22:05:05 2018-01-01 22:15:19
#>  7          290 2018-01-02 12:13:51 2018-01-02 12:18:42
#>  8          381 2018-01-02 12:50:03 2018-01-02 12:56:24
#>  9          318 2018-01-02 13:55:58 2018-01-02 14:01:16
#> 10         1852 2018-01-02 16:55:29 2018-01-02 17:26:22
#> # … with 333,677 more rows, and 12 more variables:
#> #   start_station_id <dbl>, start_station_name <chr>,
#> #   start_station_latitude <dbl>, start_station_longitude <dbl>,
#> #   end_station_id <dbl>, end_station_name <chr>,
#> #   end_station_latitude <dbl>, end_station_longitude <dbl>,
#> #   bikeid <dbl>, usertype <chr>, birth_year <dbl>, gender <dbl>

预期产出 在此处输入图像描述

我尝试了下面的代码,但不确定如何修复 n 侧。

  p1 <- nycbikes18
  p1 <- ggplot(p1) + 
        geom_point(aes(start_station_longitude,start_station_latitude, 
        size=n), alpha = 0.5) +
        geom_point(aes(end_station_longitude,end_station_latitude, size=n), 
        alpha = 0.5)
  p1

标签: r

解决方案


在第一次通话中,您正在ggplot()用“结束站”aes覆盖您的“起始站”美学。geom_point()

根据您的描述,您想要的是:

ggplot(p1) + 
    geom_point(aes(start_station_longitude,start_station_latitude, size = n_start), alpha = 0.5) +
    geom_point(aes(end_station_longitude,end_station_latitude, size = n_end), alpha = 0.5)

尽管如果您分享一个可重现的示例并解释您遇到的错误,您会提高获得帮助的机会。


推荐阅读