Skip to content

Commit 6641469

Browse files
committed
w
1 parent 491d9aa commit 6641469

266 files changed

Lines changed: 125108 additions & 3043 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

-2 KB
Binary file not shown.

Plot/.DS_Store

0 Bytes
Binary file not shown.

Plot/1 ggplot2.qmd

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ p+ theme_fivethirtyeight()
393393
# convert ggplot to plotly
394394

395395
```{r}
396-
p=ggplot(tips, aes(tip, total_bill,color=sex)) + geom_point()+ scale_x_continuous(name="new x name")+ scale_y_continuous(name="new y name")
396+
p=ggplot(data=tips, aes(tip, total_bill,color=sex)) + geom_point()+ scale_x_continuous(name="new x name")+ scale_y_continuous(name="new y name")
397397
398398
pp=ggplotly(p)
399399
@@ -461,6 +461,92 @@ p
461461
```
462462

463463

464+
# ggplot2 extension packages
465+
466+
https://www.youtube.com/watch?v=efj6-aawubs&list=PLBnFxG6owe1FcJnrNbaIJyALHMyoKW27T
467+
468+
## patchwork
469+
470+
The goal of patchwork is to make it ridiculously simple to combine separate ggplots into the same graphic.
471+
472+
```{r}
473+
#pak::pak('patchwork')
474+
```
475+
476+
```{r}
477+
library(ggplot2)
478+
library(patchwork)
479+
480+
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
481+
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
482+
483+
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
484+
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
485+
486+
(p1 | p2 | p3) /
487+
p4
488+
```
489+
490+
## legendry
491+
492+
493+
The goal of legendry is to provide additional guide functionality to the ggplot2 ecosystem.
494+
495+
496+
```{r}
497+
#pak::pak('legendry')
498+
```
499+
500+
```{r}
501+
library(ggplot2)
502+
library(legendry)
503+
```
504+
505+
506+
```{r}
507+
508+
base <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
509+
geom_point() +
510+
labs(
511+
x = "Engine displacement",
512+
y = "Highway miles per gallon",
513+
col = "City miles\nper gallon"
514+
) +
515+
theme(axis.line = element_line())
516+
```
517+
518+
519+
```{r}
520+
# A partial guide to display a bracket
521+
efficient_bracket <- primitive_bracket(
522+
# Keys determine what is displayed
523+
key = key_range_manual(start = 25, end = Inf, name = "Efficient"),
524+
bracket = "square",
525+
# We want vertical text
526+
theme = theme(
527+
legend.text = element_text(angle = 90, hjust = 0.5),
528+
axis.text.y.left = element_text(angle = 90, hjust = 0.5)
529+
)
530+
)
531+
532+
base + guides(y = guide_axis_stack("axis", efficient_bracket))
533+
```
534+
535+
536+
## marquee
537+
538+
## ggiraph
539+
540+
## geomtextpath
541+
542+
## ggpattern
543+
544+
## ggbump
545+
546+
## gghighlight
547+
548+
## ggdist
549+
464550

465551

466552
# reference:

Plot/5 Map/.DS_Store

2 KB
Binary file not shown.

Plot/5 Map/3 leaflet US.qmd

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ format:
1818
code-block-border-left: "#31BAE9"
1919
code-copy: true
2020
---
21-
22-
# data
21+
# US gdp by State
2322

2423

2524
```{r}
@@ -37,77 +36,43 @@ library(maps)
3736
mapStates = map("state", fill = TRUE, plot = FALSE)
3837
```
3938

39+
US location data
4040
```{r}
4141
json_data=read_sf("us-states.json")
4242
```
4343

44-
4544
```{r}
4645
map_df <- as.data.frame(json_data)
4746
```
4847

4948

50-
51-
# map
52-
53-
```{r}
54-
data001=read_csv('./data/us shooting dagta001.csv')
55-
```
56-
57-
58-
5949
```{r}
60-
data001=sample_n(data001,10) %>% clean_names() %>% mutate(full_adress=paste(address,city_or_county,state))
50+
glimpse(mapStates)
6151
```
6252

63-
```{r}
64-
glimpse(data001)
65-
```
53+
US 2-22 GDP data
6654

67-
# search all address to latitude, longitude
6855
```{r}
69-
# geocode the addresses
70-
data002 <- data001 %>%
71-
geocode(full_adress, method = 'arcgis', lat = latitude , long = longitude)
72-
73-
glimpse(data002)
74-
```
56+
library(openxlsx)
57+
library(readxl)
7558
76-
```{r}
77-
data003=data002 %>% filter(is.na(latitude)==FALSE,(is.na(longitude)==FALSE))
59+
per_gdp_usd=read_excel('US state gpd 2022.xlsx') %>% mutate(names=str_replace(names,'\\*','') %>% str_trim() )
7860
```
7961

80-
# map
8162

8263
```{r}
83-
m <- leaflet(data=mapStates) %>%
84-
# Add default OpenStreetMap map tiles
85-
addTiles() %>%
86-
# add markers
87-
addMarkers( lng=data003$longitude, lat=data003$latitude, popup=data003$state)%>%
88-
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
89-
90-
m
64+
glimpse(per_gdp_usd)
9165
```
9266

9367

94-
# US gdp by State
9568

9669
```{r}
97-
glimpse(mapStates)
98-
```
99-
100-
```{r}
101-
library(openxlsx)
102-
library(readxl)
103-
104-
per_gdp_usd=read_excel('US state gpd 2022.xlsx') %>% mutate(names=str_replace(names,'\\*','') %>% str_trim() )
70+
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "names")
10571
```
10672

10773

108-
10974
```{r}
110-
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "names")
75+
glimpse(map_df002)
11176
```
11277

11378

@@ -117,14 +82,21 @@ map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
11782

11883
```{r}
11984
120-
pal <- colorNumeric("magma", NULL)
85+
#pal <- colorNumeric("magma", NULL)
86+
pal <- colorNumeric(
87+
palette = "Blues",
88+
domain = map_df002$per_gdp_usd
89+
)
12190
91+
binpal <- colorBin("Blues",map_df002$per_gdp_usd, 20, pretty = FALSE)
12292
12393
m <- leaflet(map_sf002) %>%
12494
# Add default OpenStreetMap map tiles
12595
addTiles() %>%
12696
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
127-
fillColor = ~ pal(per_2022_usd)
97+
#fillColor = ~ pal(per_2022_usd)
98+
#color = ~pal(per_2022_usd)
99+
color = ~binpal(per_2022_usd)
128100
,popup = ~ paste0(
129101
"地区:", name, "<br/>",
130102
"<hr/>",

Plot/5 Map/5 world map.qmd

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: "World map with GPD per capita"
3+
subtitle: "with leaflet"
4+
author: "Tony Duan"
5+
6+
execute:
7+
warning: false
8+
error: false
9+
10+
format:
11+
html:
12+
toc: true
13+
toc-location: right
14+
code-fold: show
15+
code-tools: true
16+
number-sections: true
17+
code-block-bg: true
18+
code-block-border-left: "#31BAE9"
19+
code-copy: true
20+
---
21+
22+
# Download data
23+
24+
```{r}
25+
library(tidyverse)
26+
library(rvest)
27+
library(chromote)
28+
library(janitor)
29+
library(tidygeocoder)
30+
library(leaflet)
31+
library(sf)
32+
```
33+
34+
35+
world geojson
36+
37+
```{r}
38+
# From http://data.okfn.org/data/datasets/geo-boundaries-world-110m
39+
#countries <- sf::read_sf("https://rstudio.github.io/leaflet/json/countries.geojson")
40+
countries <- sf::read_sf("https://raw.githubusercontent.com/johan/world.geo.json/refs/heads/master/countries.geo.json")
41+
42+
43+
```
44+
45+
world gpd per capita data
46+
47+
```{r}
48+
world_gdp_per_capita=read.csv("https://gist.githubusercontent.com/vr-23/d6a4a0aadcf3a2640091ca43c25e1955/raw/57b9cb74b24dc87b489c74cc4e11209cd5720642/world-data-2023.csv")
49+
```
50+
51+
52+
```{r}
53+
# data source: https://data.worldbank.org/
54+
download.file("https://api.worldbank.org/v2/en/indicator/NY.GDP.PCAP.CD?downloadformat=csv",destfile='world_data.zip')
55+
```
56+
```{r}
57+
unzip('world_data.zip')
58+
```
59+
60+
```{r}
61+
world_gdp_per_capita=read.csv("API_NY.GDP.PCAP.CD_DS2_en_csv_v2_76317.csv",skip = 3) |> clean_names()
62+
```
63+
64+
65+
66+
```{r}
67+
data001=countries |> left_join(world_gdp_per_capita,by = join_by(id ==country_code))
68+
```
69+
70+
71+
72+
```{r}
73+
pal <- colorNumeric(palette = c('red','orange','yellow','pink','blue', 'green'),
74+
domain = data001$x2023
75+
)
76+
77+
78+
79+
m <- leaflet(data001) %>%
80+
# Add default OpenStreetMap map tiles
81+
addTiles() %>%
82+
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
83+
fillColor = ~ pal(x2023)
84+
,color = ~pal(x2023)
85+
#color = ~binpal(x2023)
86+
,popup = ~ paste0(
87+
"地区:", name, "<br/>",
88+
"<hr/>",
89+
"人均gpd:", x2023, "(美元)", "<br/>"
90+
91+
)
92+
,label = lapply(paste0(
93+
"地区:", "<b>", data001$country_name, "</b>", "<br/>",
94+
"人均gpd 美元:", round(data001$x2023), " USD<br/>"
95+
# , "总gpd 美元:", round(data001$total_2022_usd), "M <br/>"
96+
97+
), htmltools::HTML)
98+
)%>% addLegend(
99+
position = "bottomright", title = "人均gpd(美元)",
100+
pal = pal, values = ~x2023, opacity = 1.0
101+
)
102+
```
103+
104+
105+
```{r}
106+
m |> setView(lng = -10, lat = 45, zoom = 2)
107+
```
108+
109+
# resouce:
110+

0 commit comments

Comments
 (0)