Dear Visitors:

Please scroll down the page to see present and archive blogs.

Thank you very much: Tramway Null(0)

Webrings - Maps - Trolleys and More

Navigation by WebRing.

Tuesday, November 13, 2018

Plotting Japanese Railways: Source: R Blog



Hi folks:

 I ran into this posting in R-Bloggers for 11/9/18 by "R on Chi's Impe[r]fect blog.   I just looked at it for a few moments and it should be of interest to "R" users, map makers and railway fans form all over.   The code looks complicated but the diagrams are great.   I hope this helps someone.   Will eventually get to look at the code.



Voronoi diagram with ggvoronoi package with Train Station data

November 9, 2018
By
(This article was first published on R on Chi's Impe[r]fect Blog, and kindly contributed to R-bloggers)

I’ve always been curious to make Voronoi diagram, I just think they are beautiful! When I came across data set with train stations in Japan. I instantly thought this would be great data sets to make Voronoi diagram! I’ve gotten data sets from (Ekidata)[http://www.ekidata.jp/] site. I’m amazed how many train stations we have in Japan, as well as coverage of train systems in Japan.
There are couple of packages I could’ve used to make Voronoi diagram, but I’ve utilized package ggvoronoi. I really like using “outline” inside of geom_voronoi function to mask out the shape! (Which I wasn’t sure how to do before using deldir package).

Voronoi Diagram with Train Station as a seed.

ggvoronoi makes it easy to plot voronoi diagram! All I really needed to produce voronoi diagram was longitude & latitude.
Initially I’ve plotted all the train station as a point (using geom_point), you can see that station will reveal shape of Japan, as JR (Japan Railway) really covers coast line of Japan. There are total of 10828 points, as there were 10828 stations listed in most recent data set downloaded today.
I also used treemap package to create treemap.
I’ve colour coded rectangle inside of treemap with company types. 47% of 10K+ stations are JR Japan Railway stations in Japan.
Tokyo (area: 2,188 sq.km) has 943 stations all together, followed by Hokkaido 650 stations, but Hokkaido is the biggest prefecture in terms of area (83,456.87 sq.km) . It would be interesting to get area data for each prefecture, so we can calculate stations per area.
jp <- ggplot2::map_data('world2', region='japan')
names(jp) <- c("lon","lat", "group","order","region","subregion")
## for train, I'm going to tidy up the map bit. (I've excluded Okinawa for now)
jp_outline <- jp %>% filter(subregion %in% c("Honshu","Hokkaido","Kyushu","Shikoku"))

## I also wanted prefecture level data, so I've used map data from mapdata package.
jp_outline_detailed <- map_data("japan")


## station_master lists all stations of all lines
plotPoints <-station_master %>%
  ggplot(aes(x=lon, y=lat)) +
  theme_void(base_family="Roboto Condensed") +
  geom_polygon(data=jp_outline, aes(group=group), fill="#ffffff", color="#33333380") +
  geom_point(aes(color=pref_cd),size=0.1, alpha=0.8) +
  scale_color_viridis_c(end=0.5, guide="none") +
  labs(title="Each Train Station as a point") +
  coord_quickmap()

## station_master2 is reduced version of station_master
plotVoronoi <-station_master2 %>%
  ggplot(aes(x=lon, y=lat)) +
  theme_void(base_family="Roboto Condensed") +
  geom_polygon(data=jp_outline, aes(group=group), fill="#ffffff00", color="#33333380") +
  geom_path(stat="voronoi", size=0.1, aes(color=pref_cd)) +
  coord_quickmap() +
  scale_color_viridis_c(end=0.5, guide="none") +
  labs(title="Voronoi Diagram with station as a seed")

## use patchwork package to plot 2 plots side by side
plotPoints + plotVoronoi

## All of Japan - Takes long time to draw on my machine.
station_master2 %>% 
  ggplot(aes(x=lon, y=lat)) +
  theme_void(base_family="Hiragino Sans W5") +
  geom_voronoi(aes(fill=station_cnt),size=0.05, color="#ffffff", 
               outline=jp_outline) + ## this outline feature is awesome!
  coord_quickmap() +
  scale_fill_viridis_c(end=0.8, option="magma", guide="none") 

Treemap with treemap package

With treemap, I can easily see which prefecture has more stations. Also I wanted to see which railway company are dominant in each prefecture.
## Treemap to see which prefecture has more stations.
station_master %>% 
  count(pref_name,company_type_descr,company_name_r) %>%
  add_count(pref_name,wt=n) %>%
  mutate(pref_descr = paste(pref_name,":",nn,"駅")) %>%
  treemap(index=c("pref_descr","company_type_descr","company_name_r"),
          vSize="n", vColor="company_type_descr", type="categorical",
          fontfamily.labels="Hiragino Sans W3",
          align.labels=list(c("left","top"),c("center","center"),c("right","bottom")),
          fontsize.labels=c(13,0,11),
          palette=viridis_pal(end=0.6)(4),
          border.col="white",
          bg.labels=0,
          position.legend="bottom",
          title.legend="", 
          title="Number of Stations by Prefecture\ncoloured by operating company types")

Writing Function to Plot Prefecture Level Voronoi

There are 47 prefectures in Japan. So I’ve decided to write function to draw voronoi as below. I think below can be simplified…, but for now it does the job…
## function to draw voronoi map at prefecture level
draw_pref <- function(pref_no=1,zoom=T,save_file=F,folder_name="prefecture",...){
  region <- prefs %>% filter(pref_cd==pref_no) %>% pull(pref_name_en)
  region_jp <- prefs %>% filter(pref_cd==pref_no) %>% pull(pref_name) 
  pref_summary  <- station_master %>% 
    filter(pref_cd==pref_no) %>% 
    summarise(station_cnt=n(),
              line_cnt =n_distinct(line_name), 
              company_count=n_distinct(company_name_r))
  
  tmp_df <- station_master2 %>% filter(pref_cd==pref_no)
  pref_outline <- map_data("japan", region=region)
  
  capital <-jpnprefs %>% mutate(pref_cd=row_number()) %>% filter(pref_cd==pref_no)
  
  ## calculate distance between capital city & each station so i can colour the cell of voronoi.
  tmp_df <- tmp_df %>%
    mutate(dist_from_capital = 
             sqrt((lon-capital$capital_longitude)^2 + (lat-capital$capital_latitude)^2))
  
  # finding bounding box from train station data... , so I can crop the map if I want to.
  bbox <-tmp_df  %>% ungroup() %>% 
    summarise(xmax=max(lon), xmin=min(lon), ymax=max(lat), ymin=min(lat))
  
  
  base_map <-tmp_df %>% ggplot(aes(x=lon,y=lat)) +
    theme_void(base_family="Hiragino Sans W5") +
    #geom_voronoi(aes(fill=comp_cd_min) ,size=0.1, color="#ffffff", 
    #           outline=pref_outline) +
    geom_voronoi(aes(fill=dist_from_capital) ,size=0.1, color="#ffffff", 
               outline=pref_outline) +
    #scale_fill_gradientn(colors = c("#440154FF","#000000FF","#31688EFF", "#1F9E89FF","#6DCD59FF"),
                       #breaks=c(0,7,30,100,200), limits=c(1,250), guide="none") +
    scale_fill_viridis_c(end=0.9, guide="none", option="magma") +
    labs(title=paste0(region_jp," (",region,")"), 
         caption=paste0("Capital City of ",region," is ",capital$capital, " @ (",
                        round(capital$capital_longitude,2),",", round(capital$capital_latitude,2),")"),
         subtitle=paste(pref_summary$station_cnt,"stations", 
                       pref_summary$line_cnt," lines operated by", 
                       pref_summary$company_count, "companies in", 
                       str_to_title(region))) +
    geom_point(data=capital, aes(x=capital_longitude, y=capital_latitude),shape=4, color="#ffffff") 
  
  if (zoom) {
    print(base_map + 
            coord_quickmap(xlim=c(bbox$xmin-0.1,bbox$xmax+0.1), 
                           ylim=c(bbox$ymin-0.1,bbox$ymax+0.1)))
    } else {
    print(base_map + coord_quickmap())
    }
  
  if(save_file){
    ggsave(paste0(folder_name,"/",
                  formatC(pref_no, width=2,flag="0"),"-",str_to_lower(region),".png"),
           width=9,height=9,dpi=300)
  }
  
}


## function to draw treemap at prefecture level
draw_treemap <- function(pref_no=1,...){
  station_master$color <- 
    viridis_pal(end=0.6)(nlevels(station_master$company_type_descr))[station_master$company_type_descr]
  title_text <- prefs %>% filter(pref_cd==pref_no) %>% pull(pref_name_en)
  
  station_master %>% 
    filter(pref_cd==pref_no) %>%
    count(company_type_descr,company_name_r,line_name,color,station_name) %>%
    treemap(index=c("company_type_descr","company_name_r","line_name","station_name"),
            vSize="n", vColor="color", type="color",
            fontfamily.labels="Hiragino Sans W3",
            fontfamily.title="Roboto Condensed",
            align.labels=list(c("center","center"),c("left","top"),
                              c("right","bottom"),c("center","center")),
            fontsize.labels=c(0,13,11,0),
            border.col=c("#ffffffff","#ffffff90","#ffffff30","#ffffff10"),
            border.lwds = c(3,2,1,0.2),
            bg.labels=0,
            title.legend="", title="",
            aspRatio = 16/9)
}

Tokyo!

While it’s interesting to see Voronoi map of Japan, I wanted to zoom into selected prefectures that I care maybe more about.
Firstly, Tokyo. I love looking at Tokyo’s train map such as this one. JR East Route Map PDF.
For below voronoi diagram, I’ve decided to colour the voronoi cell with distance from Shinjuku (capital city of Tokyo) to corresponding station cell. (I actually think it’s more interesting to get train usage data, and colour the cell with train usage data, but because there are so many different operating company, getting data about train usage seemed like pretty hard task to do…)
I like how dense train staions are packed around central tokyo (east side), but as you go towards the west, cell becomes bigger and bigger. In fact, far west side of Tokyo, there are NOT that many stations at all.
I’ve also created treemap for Tokyo below. Personally I was surprised that there are maybe more Tokyo metro stations than JR stations in Tokyo. I’ve also came to realize that there are so many companies…
draw_pref(13, zoom=T, save_file=F)

draw_treemap(13)

station_master %>% 
  filter(pref_cd==13) %>%
  arrange(e_sort) %>%
  ggplot(aes(x=lon, y=lat)) +
  geom_sf(data=jpn_pref(13), inherit.aes=F, color="#33333320") +
  geom_point(aes(color=company_name_r, shape=company_type_descr), alpha=0.8) +
  theme(axis.text=element_blank(),
        axis.title=element_blank()) +
  theme_minimal(base_family="Hiragino Sans W5") +
  geom_text_repel(data=station_master2 %>% filter(pref_cd==13 & station_cnt>6), 
            aes(label=station_name),
            family="Osaka", min.segment.length=0, nudge_x=0.25, segment.color="#33333350") +
  scale_color_hue(l=45) +
  coord_sf(ylim=c(35.5,35.9), xlim=c(138.8,139.9)) ## to remove islands of Tokyo

Plotting Kanagawa Prefecture

Kanagawa prefecture is where Yokohama, Also where one of my favourite place, Kamakura is. I like the shape of prefecture, as it sort of looks like a dog?! Maybe camel?!
Capital city of Kanagawa prefecture is Yokohama, and I’ve again coloured cell based on distance from Yokohama. Similar to Tokyo, east side of Kanagawa has a lot of stations but west side is pretty sparse.
draw_pref(14, zoom=T, save_file=F)

draw_treemap(14)

## See Station in Kanagawa
station_master %>% 
  filter(pref_cd==14) %>%
  arrange(e_sort) %>%
  ggplot(aes(x=lon, y=lat)) +
  geom_sf(data=jpn_pref(14), inherit.aes=F, color="#33333320") +
  geom_point(aes(color=company_name_r, shape=company_type_descr), alpha=0.8) +
  theme(axis.text=element_blank(),
        axis.title=element_blank()) +
  theme_minimal(base_family="Hiragino Sans W5") +
  geom_text_repel(data=station_master2 %>% filter(pref_cd==14 & station_cnt>3), 
            aes(label=station_name),
            family="Osaka", min.segment.length=0, nudge_x=0.25, segment.color="#33333350") +
  scale_color_hue(l=45)

Plotting Chiba Prefecture

Chiba is where Narita Airport is. I just had to plot it out, because I like the shape of prefecture 🙂 It looks like a hummingbird to me, but Chiba prefecture actually have a maskot called Chi-ba-kun, and it’s a dog character.
draw_pref(12, zoom=T, save_file=F)

draw_treemap(12)

## See Station in Chiba
station_master %>% 
  filter(pref_cd==12) %>%
  arrange(e_sort) %>%
  ggplot(aes(x=lon, y=lat)) +
  geom_sf(data=jpn_pref(12), inherit.aes=F, color="#33333320") +
  geom_point(aes(color=company_name_r, shape=company_type_descr), alpha=0.8) +
  theme(axis.text=element_blank(),
        axis.title=element_blank()) +
  theme_minimal(base_family="Hiragino Sans W5") +
  geom_text_repel(data=station_master2 %>% filter(pref_cd==12 & station_cnt>3), 
            aes(label=station_name),size=3,
            family="Osaka", min.segment.length=0, nudge_x=0.25, segment.color="#33333350") +
  scale_color_hue(l=45)

Bonus: Plotting Hokkaido Prefecture

Hokkaido is the largest prefecture in Japan, and it has 2nd most numbers of train stations. (While it has 2nd most stations in number, Hokkaido is about 37 times bigger than Tokyo in area).
Shape of Hokkaido is pretty iconic (at least in my mind.) I recently found out there’s heart-shaped lake called Toyoni lake in Hokkaido too, but I didn’t spot heart-shaped Voronoi cell…
draw_pref(1, zoom=T, save_file=F)

draw_treemap(1)

## To see station on actual map
station_master %>% 
  filter(pref_cd==1) %>%
  arrange(e_sort) %>%
  ggplot(aes(x=lon, y=lat)) +
  geom_sf(data=jpn_pref(1), inherit.aes=F, color="#33333320") +
  geom_point(aes(color=company_name_r, shape=company_type_descr), alpha=0.8) +
  theme(axis.text=element_blank(),
        axis.title=element_blank()) +
  theme_minimal(base_family="Hiragino Sans W5") +
  geom_text_repel(data=station_master2 %>% filter(pref_cd==1 & station_cnt>=3), 
            aes(label=station_name),
            family="Osaka", min.segment.length=0, nudge_x=2, segment.color="#33333350") +
  scale_color_hue(l=45, name="company name")
To leave a comment for the author, please follow the link and comment on their blog: R on Chi's Impe[r]fect Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...
Comments are closed.

Monday, November 12, 2018

El Paso Trolley Opens

Hi Folks:

 I just heard that a small system of PCC restored streetcars opened in El Paso, Texas.  I saw some clips of the trolley restoration and the first day of operation and I am quite impressed.   I believe the shells of the original PCC fleet were stored in the desert and were restored beautifully with all the modern conveniences including air-conditioning.   Even though, of course, I am at home and saw the video, you can "feel" the smooth running of the PCC cars.  Unlike the original system, the new PCC streetcars use pantographs instead of trolley wheel and bow.  They really look cool.   I wish El Paso much success and that more lines could be established.   For  New York City, I would be happy if a stationary exhibit of a streetcar, PCC or not would be available.

Good luck El Paso!
Tramway Null()

Wednesday, November 7, 2018

Visiting QGIS Again

I have not worked with QGIS is some time.   I was called upon to do an analysis of distances between certain clients and institutions and I therefore revisited QGIS.  Several years ago, I made a map with some altitude files (both vector and raster) and this map contains both types of files.  The subway lines are in green are of the vector types and the contours for the Hudson River and New York upper bay are also contour raster files.   The eastern part Staten Island is also shown and is a raster file.   It is interesting that the Bay Ridge Division of the LIRR is also shown.   Some persons want to make this line into a Triborough crosstown line to Co-Op City in the Bronx.  Also is shown next to the West End Line (fourth green line from the right) is a vertical straight line that comes from an uneven edge of an aerial photograph.  This map is not useful but it is interesting to look at and perhaps can be considered a work of art?  The Hudson River depth file comes probably comes from a New York State data source file that I extracted and used many years ago.

Tuesday, October 30, 2018

Church - McDonald Streetcar Line died 10/31/56

/Tonight is the anniversary of the end of streetcars in Brooklyn on 10/31/56 which may have been also a Wednesday.   On this date, the Church Avenue, Church - McDonald and the Cortelyou Road trolleybus (B-23) also ended.  In this shot taken off the web shows a southbound Church - McDonald streetcar along the incline of the IND subway system between Ditmas and Church Avenue stations.  To the right of the PCC streetcar is a R-1-9 subway car going south towards the Ditmas Avenue station.  The Brooklyn - Queens Tram shown below is not doing well and around a week ago it was declared that the Staten Island Ferris Wheel is also dead.   I guess trolleys in Brooklyn, old or new and a ferris wheel in Staten Island were not meant to be.

Friday, October 19, 2018

Ashland Street Connection

Currently in subchat, there is a thread dealing with the Myrtle Avenue Line in Brooklyn and its demise.  There is some discussion stating how useful would the line be today if it was still in existence and a thought / experiment dealing with a re-introduction of the line.  Where would the line end in the western part of Myrtle Avenue now that Metrotech occupies the former Myrtle -Jay Street-Bridge Street area?


  Around 1969,  before transit became fashionable and the only way to do research was to go to the Public Library, I went to the  Research Division of the New York Public Library at 42nd Street and Fifth Avenue.   I remember either looking at a early New York Regional Plan (the first? )  written in the early 1920's or the Brooklyn Manhattan Transit (BMT) document dealing with transit development, also dated in the 1920's.   Something was mentioned about an Ashland Street connection where an elevated line would be connected to the relatively new BMT subway in the area.  Dual Contracts was not finished and the BMT was having trouble building the Nassau Street Loop at this time.  For better service, it was proposed to connect one of the elevated lines to the subway and have the line go to one of the BMT branches in Manhattan.  I do not remember if the document had a map showing where this would be built.

    It would be very difficult these days to construct an elevated line in a residential area, however, something that I never saw before is happening today:   Large skyscrapers are being built near the elevated Flushing Line.   Attitudes are changing about being near rapid transit and younger persons may be less against elevated lines.

Thursday, October 18, 2018

Myrtle Avenue and Flatbush Avenue Extension: Revisited








https://pcullinan.smugmug.com/Brooklyn/The-Myrtle-Avenue-El/Myrtle-Ave-El-Oct-1969/i-z5WGrVN

  For the last few weeks, there has been a thread going on in subchat dealing with the Myrtle Avenue El, whose anniversary of it's abandonment was in early October.  This line, the last one to end service with wooden cars, ended service in October, 1969.  I rode on that line, in the early afternoon of the last day.   Growing up, my mother used to take me to downtown Brooklyn for shopping in the big department stores on Fulton Street and as we walked, at each intersection to the north,  I saw views of the line about one block away.   When I was very small, I remember seeing gate cars on the line (prior to 1958), but no matter how much I begged my mother, should would not take me on the line.
In the picture above, taken off the web at the address indicated, is  view of Flatbush Avenue Extension and Myrtle Avenue.  You can see part of of green arched structure that I remember as a child.   As you look at the left of the picture, you can see black squares.   These are the air vents to the BMT Subway at the Myrtle Avenue station which is now abandoned.  I believe that on the Myrtle Avenue El, there was no station at Flatbush Avenue because Flatbush Avenue Extension was a street created to reach the Manhattan Bridge and it did not exist when the el was constructed in the 19th Century.  Thus this intersection was between the Navy Street and Bridge Street Stations.  When elevated service ended on the Brooklyn Bridge in 1944?, the line had to be reconfigured at the western end.   I believe the Bridge Street station was extended westward towards Jay Street with a passenger walkway to the IND Jay Street - Borough Hall (Now Metrotech) station.   Many people do not know that Myrtle Avenue continued westward towards Court Street or Adams Street with the el passing the Sand Street on it's route over the Brooklyn Bridge to Park Row in Manhattan.  The Supreme Court at Adams Street is the approximate location where Myrtle Avenue and its' el continued westward.
  I remember that there was some sort of unusual transfer procedures for Myrtle El  passengers that were established.  It went something like this:  Passengers paying fares at some westerly Myrtle Avenue El Stations got a ticket to transfer to the IND Jay Street Subway station at Myrtle and Jay streets.  They used the new walkway and gave the paper transfer to the agent.   According to the ticket, this transfer was also valid for streetcars on Jay Street (1944-1951) as well.  These streetcars were Park Row bound.   The ticket was only valid until the Broadway-Nassau stations (Fulton Street today).   Theoretically, the passenger who have to exit the subway at Broadway Nassau.   Going to Brooklyn, only those paying fares at Broadway Nassau got a ticket to transfer to the Myrtle El at Jay-Bridge Street station on the Myrtle El.  At some point, probably at nights, perhaps in the 1950's, the fare agent booths were closed at night and passengers paid there fares to conductors on the el trains.  I am sorry that the el is gone even though I have no business in this part of Brooklyn.   Els were considered as "old fashioned" and transportation planners considered smelly stinkeroo diesel buses as a solution to all our transit woes.  Picture from Brooklyn Public Library.   The arch was visible from many blocks away.  Myrtle-Gold Street station should be underneath the arch.   Thus at Myrtle - Flatbush,  you have subway, trolley and elevated service.  The area looks very different today.


Tramway Null()

Thursday, August 30, 2018

New News About Waterfront Streetcar Plan

https://news.google.com/articles/CAIiEJ9dk4yabILl_q8gpCXyDqEqFwgEKg8IACoHCAowjuuKAzCWrzwwloIY?hl=en-US&gl=US&ceid=US%3Aen






Breaking News: City Announces BQX Next Steps


Breaking news about the Brooklyn Queens Connector:  Released 8/30/18 over 1 hr ago.


  1. Shorter:  To Gowanas Red Hook Only from Long Island City
  2. More Expensive to Build
  3. Longer To Build 2029
  4. Uses Overhead
  5. More Inland
  6. Depends on Government allocations for support