如何在 R 中构建斜率图?
How to build slope plots in R?
我有一个数据框,它代表了 2 年不同型号的销售额。 'change' 列代表 2020 年到 2021 年各型号的绝对变化,而 'chng.percent' 衡量百分比变化。
df <- data.frame (model = c("A", "A", "B","B"),
year = c(2020,2021,2020,2021),
sale =c(105,190,110,180),
chang = c(85,NA,70,NA),
chng.percent = c(80.9,NA, 63.6,NA))
现在我正在努力在 R 中构建斜率图:
(CODE)
我想建立坡地 + 根据模型分类变量添加绝对和百分比变化 + 线条颜色
预期输出:
这个怎么样:
library(tidyverse)
df <- data.frame (model = c("A", "A", "B","B"),
year = c(2020,2021,2020,2021),
sale =c(105,190,110,180),
chang = c(85,NA,70,NA),
chng.percent = c(80.9,NA, 63.6,NA))
labs <- df %>%
group_by(model) %>%
slice_head(n=1)
ggplot(df, aes(x=year, y=sale, colour = model)) +
geom_line(show.legend = FALSE) +
geom_point() +
geom_text(data = subset(df, year==2020), aes(x=year, y=sale, label=sale), hjust=1, nudge_x = -.05, show.legend = FALSE, inherit.aes = FALSE) +
geom_text(data = subset(df, year==2021), aes(x=year, y=sale, label=sale), hjust=0, nudge_x = .05, show.legend = FALSE, inherit.aes = FALSE) +
geom_text(aes(x=2020.75, y=140, label = sprintf("+%.1f, %.1f%%", labs$chang[2], labs$chng.percent[2]), colour=labs$model[2]), show.legend = FALSE) +
geom_text(aes(x=2020.75, y=175, label = sprintf("+%.1f, %.1f%%", labs$chang[1], labs$chng.percent[1]), colour=labs$model[1]), hjust=1, show.legend = FALSE) +
geom_vline(xintercept = unique(df$year)) +
scale_x_continuous(breaks = c(2020,2021), labels=c(2020, 2021)) +
coord_cartesian(xlim=c(2019.75, 2021.25)) +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid = element_blank()) +
labs(colour ="", y="", x="")
由 reprex package (v2.0.1)
创建于 2022-05-12
我有一个数据框,它代表了 2 年不同型号的销售额。 'change' 列代表 2020 年到 2021 年各型号的绝对变化,而 'chng.percent' 衡量百分比变化。
df <- data.frame (model = c("A", "A", "B","B"),
year = c(2020,2021,2020,2021),
sale =c(105,190,110,180),
chang = c(85,NA,70,NA),
chng.percent = c(80.9,NA, 63.6,NA))
现在我正在努力在 R 中构建斜率图:
(CODE)
预期输出:
这个怎么样:
library(tidyverse)
df <- data.frame (model = c("A", "A", "B","B"),
year = c(2020,2021,2020,2021),
sale =c(105,190,110,180),
chang = c(85,NA,70,NA),
chng.percent = c(80.9,NA, 63.6,NA))
labs <- df %>%
group_by(model) %>%
slice_head(n=1)
ggplot(df, aes(x=year, y=sale, colour = model)) +
geom_line(show.legend = FALSE) +
geom_point() +
geom_text(data = subset(df, year==2020), aes(x=year, y=sale, label=sale), hjust=1, nudge_x = -.05, show.legend = FALSE, inherit.aes = FALSE) +
geom_text(data = subset(df, year==2021), aes(x=year, y=sale, label=sale), hjust=0, nudge_x = .05, show.legend = FALSE, inherit.aes = FALSE) +
geom_text(aes(x=2020.75, y=140, label = sprintf("+%.1f, %.1f%%", labs$chang[2], labs$chng.percent[2]), colour=labs$model[2]), show.legend = FALSE) +
geom_text(aes(x=2020.75, y=175, label = sprintf("+%.1f, %.1f%%", labs$chang[1], labs$chng.percent[1]), colour=labs$model[1]), hjust=1, show.legend = FALSE) +
geom_vline(xintercept = unique(df$year)) +
scale_x_continuous(breaks = c(2020,2021), labels=c(2020, 2021)) +
coord_cartesian(xlim=c(2019.75, 2021.25)) +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid = element_blank()) +
labs(colour ="", y="", x="")
由 reprex package (v2.0.1)
创建于 2022-05-12