JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
585 stars 82 forks source link

How to make unequal spacing graphs? #633

Open chuxinyuan opened 3 weeks ago

chuxinyuan commented 3 weeks ago

my data

data = data.frame(
  Date = c("2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04","2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04","2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04","2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04","2024-06-04"),
  Term = c(0,0.08,0.17,0.25,0.5,0.75,
           1,2,3,5,7,10,15,20,30,40,50),
  Rate = c(1.3,1.4416,1.5074,1.5435,
           1.5793,1.6091,1.6461,1.7858,1.9197,2.0708,2.2308,
           2.2854,2.405,2.4754,2.5453,2.6211,2.625),
  Term2 = c("1D","1M","2M","3M","6M",
            "9M","1Y","2Y","3Y","5Y","7Y","10Y","15Y","20Y",
            "30Y","40Y","50Y")
)

by ggplot2

library(ggplot2)
library(ggthemes)
library(dplyr)

data %>% 
  ggplot(aes(Term, Rate, group = 1)) + 
  geom_point(color = "#96363D") + 
  geom_line(color = "#96363D", linewidth = 1) +
  theme_economist() + 
  theme(
    legend.position = "none",
    plot.title = element_text(size = 12)
  ) + 
  scale_x_continuous(
    breaks = c(
      0.5, 1, 3, 5, 7, 10, 
      15, 20, 30, 40, 50
    ),
    labels = c(
      "6M", "1Y", "3Y", "5Y", "7Y", "10Y", 
      "15Y", "20Y", "30Y", "40Y", "50Y"
    )
  ) + 
  xlab("") + ylab("") +
  ggtitle("报告期:2024年06月04日")

Semi-finished code by echarts4r

library(echarts4r)
library(dplyr)

data %>%
  arrange(Term) %>% 
  e_chart(Term) %>% 
  e_line(
    serie = Rate, 
    smooth = TRUE,
    symbol = "circle",
    lineStyle = list(color = "#96363D")
  ) %>%
  e_title(
    text = "报告期:2024年6月4日", 
    left = "100",
    top = "85",
    textStyle = list(fontSize = 12)
  ) %>%
  e_legend(show = FALSE) %>% 
  e_tooltip(trigger = "axis")

I want to use the echarts4r package to implement the animation version of the above graph, because the horizontal axis is unequal spacing, I don't know how to deal with it. Please advise, thank you!