Skip to content

Instantly share code, notes, and snippets.

@perlatex
Created April 3, 2020 09:33
Show Gist options
  • Save perlatex/8a43c719c0fd6cf55f3a94be252f6d6e to your computer and use it in GitHub Desktop.
Save perlatex/8a43c719c0fd6cf55f3a94be252f6d6e to your computer and use it in GitHub Desktop.
plot chinese tone curve
library(easyuse) # devtools::install_github("perlatex/easyuse")
library(tidyverse)
library(showtext)
showtext_auto()
data(ChineseTones)
d <- ChineseTones
d
# 统计
m <- d %>%
group_by(type) %>%
summarise(
across(is.numeric, mean),
across(p0:p100, log10)
)
m
# 宽表格变长表格
d_longer <- m %>%
pivot_longer(
cols = starts_with("p"),
names_to = "id",
values_to = "value"
) %>%
mutate(time = 1000 * time * seq(0, 1, by = 0.1))
d_longer
## 方法1
std <- m %>%
mutate(p100 = if_else(p100 < p90, NA_real_, p100)) %>%
summarise(
mean = mean(c_across(p10:p100), na.rm = T),
sd = sd(c_across(p10:p100), na.rm = T)
)
std
standard <- function(x) {
(x - std$mean) / std$sd
}
d_longer %>% mutate(value = standard(value))
## 方法2
std <- m %>%
mutate(p100 = if_else(p100 < p90, NA_real_, p100)) %>%
select(-p0, -time) %>%
pivot_longer(
cols = -type,
names_to = "pitch",
values_to = "value"
) %>%
summarise(
logmean = mean(value, na.rm = TRUE),
logsd = sd(value, na.rm = TRUE)
)
standard <- function(x) {
(x - std$logmean) / std$logsd
}
d_longer %>% mutate(value = standard(value))
## 方法3
mat <- m %>%
mutate(p100 = if_else(p100 < p90, NA_real_, p100)) %>%
select(-type, -p0, -time) %>%
as.matrix()
#{c(mean(., na.rm = T), sd(., na.rm = T))}
logmean <- mean(mat, na.rm = T)
logsd <- sd(mat, na.rm = T)
standard <- function(x) {
(x - logmean) / logsd
}
d_longer %>% mutate(value = standard(value))
## 方法4
## 使用easyuse宏包
all <- d_longer %>%
chop_standard_at(.var = value, .group = type, .pitch = id) %>%
mutate(logm5 = five_step_standard(value))
all
# 可视化
all %>%
ggplot(aes(x = time, y = logm5, color = type)) +
geom_point() +
geom_line() +
gghighlight::gghighlight() +
labs(
title = "成都话声调图(五度标调法)",
subtitle = "你觉得我这样美吗"
) +
theme_light() +
theme(legend.position = "none")
# 保存
ggsave("TonesPlot.pdf", width = 8, height = 6)
@perlatex
Copy link
Author

perlatex commented May 18, 2020

library(tidyverse)
library(showtext)
showtext_auto()

d <- easyuse::ChineseTones
d

# 统计
m <- d %>%
  group_by(type) %>%
  summarise(
    across(is.numeric, mean),
    across(p0:p100, log10)
  )
m


std <- m %>% 
  mutate(p100 = if_else(p100 < p90, NA_real_, p100)) %>%
  summarise(
    mean = mean(c_across(p10:p100), na.rm = T),
    sd =   sd(c_across(p10:p100), na.rm = T)
  )
std

standard <- function(x) {
  (x - std$mean) / std$sd
}


five_step_standard <- function(x) {
  (x - min(x)) *5 / (max(x) - min(x))
}

# 宽表格变长表格
d_longer <- m %>%
  pivot_longer(
    cols = starts_with("p"),
    names_to = "id",
    values_to = "value"
  ) %>%
  mutate(time = 1000 * time * seq(0, 1, by = 0.1))

d_longer


all <- d_longer %>% 
  mutate(value = standard(value)) %>%
  mutate(logm5 = five_step_standard(value))
all

# 可视化
all %>%
  ggplot(aes(x = time, y = logm5, color = type)) +
  geom_point() +
  geom_line() +
  gghighlight::gghighlight() +
  labs(
    title = "成都话声调图(五度标调法)",
    subtitle = "你觉得我这样美吗"
  ) +
  theme_light() +
  theme(legend.position = "none")


# 保存
ggsave("TonesPlot.pdf", width = 8, height = 6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment