简体   繁体   中英

gg plot and geom_point function in r

Given below code,I'm trying to visualise the following layered bubble charts: points of all start stations. Sizes vary with the total number of pickups. points of all end stations. Sizes vary with the total number of returns.I need end up with a ggplot object named p1, with alpha = 0.5 to both layers.

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>

expected output 在此处输入图像描述

I tried below code but not sure how to fix the n side.

  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

You are overwriting your "start station" aesthetics in ggplot() with the "end station" aes in the first geom_point() call.

From your description what you want is something like:

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)

Although you improve your chances of getting help if you share a reproducible example and explain what error you are getting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM