ggplot2绘图

GGPLOT: 美化线图文本注释展示

2023-06-18  本文已影响0人  倪桦

1、线图线条上插入注释


更多样式和代码参考 : AllanCameron/geomtextpath: Create curved text paths in ggplot2 (github.com)
geom_textline 简单用法:
data %>% 
    ggplot(aes(x, y)) +
    geom_textline(label = "Mercury vapor pressure", color = "deepskyblue4", linewidth=2)

2、副轴添加注释标签 -- sec.axis


示例代码
dt <- readr::read_tsv("https://bit.ly/3dqO4V9") ### readr可以 自动解析 date 变量
dt_label <- dt %>%  group_by(company) %>% summarize(last = dplyr::last(price))
dt %>%
    ggplot(aes(x=date, y=price, color=company)) +
    geom_step(linewidth = 0.8,direction = "vh") +
    scale_x_date(expand = c(0,0)) +
    scale_color_brewer(palette = "Set1",guide  = NULL) +
    scale_y_continuous(
        limits = c(0, 3600),expand = c(0,0),
        sec.axis = dup_axis(breaks = dt_label$last,labels = dt_label$company,name = NULL)
    ) +
    theme_bw() + theme(,axis.text.y.right = element_text(size = 15,color = "#0d569f")) +
    labs(title = "sec.axis label", x = "" ,y  ="")

代码参考:
How to Add Labels Directly in ggplot2. Hint: Use Secondary Axis Trick - Data Viz with Python and R (datavizpyr.com)
Line graph in ggplot2 [geom_line and geom_step] | R CHARTS (r-charts.com)

上一篇下一篇

猜你喜欢

热点阅读