library(shiny)
library(echarts4r)
# phoney data
df <- data.frame(
x = 1:100,
y = runif(100),
z = runif(100)
)
ui <- fluidPage(
actionButton("add", "Add series"),
actionButton("rm", "Remove series"),
echarts4rOutput("chart")
)
server <- function(input, output){
output$chart <- renderEcharts4r({
e_charts(df, x) |>
e_scatter(y, z)
})
observeEvent(input$add, {
echarts4rProxy("chart", data = df, x = x) |> # create a proxy
e_line(z) |>
e_loess(y ~ x, name = "fit") |>
e_execute()
})
observeEvent(input$rm, {
echarts4rProxy("chart") |> # create a proxy
e_remove_serie("z") |>
e_remove_serie("fit")
})
}
shinyApp(ui, server)
R语言绘制数据动图
随着互联网+和大数据科技的发展,VFX可视化和数据可视化越来越受到人们的喜爱,在R语言中,绘制GIF动图主要是在gganimate包中运行,制作视频主要是在av包,gganimate是图形语法的扩展,由ggplot2包实现,它增加了对使用ggplot2用户熟悉的API声明动画的支持
library(av)
library(ggplot2)
library(gganimate)
library(tidyverse)
library(lubridate)
`ggplot(mpg,aes(class,hwy,fill=class))+geom_boxplot()+transition_states(cty,transition_length=2,state_length=1)+enter_fade()+exit_shrink()+ease_aes('sine-in-out')`
小提琴图.gif
ggplot(mpg,aes(class,hwy,fill=class))+geom_violin(alpha=0.3,width=0.9)+geom_jitter(shape=21)+transition_states(year,transition_length=8,state_length=5)+enter_fade()+exit_shrink()+ease_aes('sine-in-out')
多因素散点图.gif
library(av)
library(ggplot2)
library(gganimate)
library(tidyverse)
library(lubridate)
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + geom_point(aes(colour = Species)) +transition_states(Species,transition_length = 2,state_length = 1)
散点图.gif
library(gapminder)
View(gapminder)
ggplot(gapminder,aes(gdpPercap,lifeExp,size=pop,colour=country))+geom_point(alpha=0.7,show.legend=FALSE)+scale_colour_manual(values=country_colors)+scale_size(range=c(2,12))+scale_x_log10()+facet_wrap(~continent)+labs(title='Year:{frame_time}',x='GDP per capita',y='life expectancy')+transition_time(year)+ease_aes('linear')