在R基本图形或ggplot中使用动画散点图是否可能?
我一直在用Shiny做一个动画滑块项目,我几乎得到了我想要的东西,但还不完全。它显示的不是动画序列中的每个连续图形,而是包含所有数据的图形(未排序)。我不太确定我的错误在哪里,但我怀疑它在响应函数调用或服务器部分的renderPlot函数调用中。我尝试过搜索网页,尝试过在不同的位置放置不同的代码块,但我似乎无法让动画在不同的位置工作。最后,我想将数字月份(1,2,3…)更改为日期对象,以获得更清晰的效果,但我会在动画开始工作后处理这个问题 请注意-我已经能够使用googleVic、gvisMotionChart和Shinny为这些数据获得成功的运动图,但我发现通过这种方法,我无法控制气泡图的颜色或气泡大小(由于重叠,我想要一个比google气泡图默认大小小得多的常量大小)。所以,我希望用R的基本图形或ggplot来完成这个动画 下面是一组小数据,表示我正在使用的内容:在R基本图形或ggplot中使用动画散点图是否可能?,r,animation,ggplot2,shiny,R,Animation,Ggplot2,Shiny,我一直在用Shiny做一个动画滑块项目,我几乎得到了我想要的东西,但还不完全。它显示的不是动画序列中的每个连续图形,而是包含所有数据的图形(未排序)。我不太确定我的错误在哪里,但我怀疑它在响应函数调用或服务器部分的renderPlot函数调用中。我尝试过搜索网页,尝试过在不同的位置放置不同的代码块,但我似乎无法让动画在不同的位置工作。最后,我想将数字月份(1,2,3…)更改为日期对象,以获得更清晰的效果,但我会在动画开始工作后处理这个问题 请注意-我已经能够使用googleVic、gvisMot
d1 <- data.table( id = 1:21,
Region = rep(c("R1","R2","R3"), each=7),
Month = 1,
Spend = round(runif(21,100,500)),
Age = round(runif(21,72,100)),
Color = rep(c("#E69F00","#D55E00","#009E73"),each=7))
d2 <- copy(d1)
d2[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d3 <- copy(d2)
d3[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d4 <- copy(d3)
d4[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d5 <- copy(d4)
d5[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d6 <- copy(d5)
d6[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d7 <- copy(d6)
d7[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
d8 <- copy(d7)
d8[,Month:=Month+1][,Age:=Age+1][,Spend:=Spend+round(runif(21,0,4))]
dat <- rbindlist(list(d1,d2,d3,d4,d5,d6,d7,d8))
这是我当前的非工作代码
library(shiny)
library(ggplot2)
# Shiny app with slider and animation
# ui section
ui = fluidPage(
# Title
titlePanel("Spend vs Age by Region"),
# Sidebar with slider and controls for animation
sidebarLayout(
# sidebar with slider
sidebarPanel(
# Slider with looping
sliderInput("theMonth", "Month", 1, 8, 1, step = 1,
animate=animationOptions(interval=1000, loop = T,
playButton = T, pauseButton = T))
),
# Show the animated graph
mainPanel(
plotOutput(outputId="case_age_plot")
)
)
)
# server section
server = function(input, output) {
# Reactive expression to create data frame and graph
aniGraph <- reactive({
# subset the data frame into the portion that has the data for the
# graph in the animation sequence
dat[Month==input$theMonth,]
# create the graph
plot(dat[,Age],dat[,Spend],col=dat[,Color],
pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1),
ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1),
xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7)
legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"),
pch=16,col=c("#E69F00","#D55E00","#009E73"),cex=.8)
})
# Show the graph
output$case_age_plot <- renderPlot({
aniGraph()
})
}
# run the app
runApp(list(ui = ui, server = server))
库(闪亮)
图书馆(GG2)
#带滑块和动画的闪亮应用程序
#用户界面部分
ui=fluidPage(
#头衔
titlePanel(“按地区划分的支出与年龄”),
#带有滑块和动画控件的侧栏
侧边栏布局(
#带滑块的边栏
侧栏面板(
#带循环的滑块
滑块输入(“theMonth”,“Month”,1,8,1,step=1,
动画=动画选项(间隔=1000,循环=T,
播放按钮=T,暂停按钮=T)
),
#显示动画图形
主面板(
plotOutput(outputId=“case\u age\u plot”)
)
)
)
#服务器部分
服务器=功能(输入、输出){
#用于创建数据帧和图形的反应式表达式
aniGraph问题在于您没有保存dat
的子集。我稍微修改了您的代码以获得与gif动画相同的动画
library(shiny)
library(ggplot2)
# Shiny app with slider and animation
# ui section
ui = fluidPage(
# Title
titlePanel("Spend vs Age by Region"),
# Sidebar with slider and controls for animation
sidebarLayout(
# sidebar with slider
sidebarPanel(
# Slider with looping
sliderInput("theMonth", "Month", 1, 8, 1, step = 1,
animate=animationOptions(interval=1000, loop = T,
playButton = T, pauseButton = T))
),
# Show the animated graph
mainPanel(
plotOutput(outputId="case_age_plot")
)
)
)
# server section
server = function(input, output) {
# Reactive expression to create data frame and graph
aniGraph <- reactive({
# subset the data frame into the portion that has the data for the
# graph in the animation sequence
# Save subset of 'dat' and pass it to the plot
dat_sub <- dat[Month==input$theMonth,]
# create the graph
plot(dat_sub[,Age],dat_sub[,Spend],col=dat_sub[,Color],
pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1),
ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1),
xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7)
legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"),
pch=16,col=c("#E69F00","#D55E00","#009E73"),cex=.8)
})
# Show the graph
output$case_age_plot <- renderPlot({
aniGraph()
})
}
# run the app
runApp(list(ui = ui, server = server))
库(闪亮)
图书馆(GG2)
#带滑块和动画的闪亮应用程序
#用户界面部分
ui=fluidPage(
#头衔
titlePanel(“按地区划分的支出与年龄”),
#带有滑块和动画控件的侧栏
侧边栏布局(
#带滑块的边栏
侧栏面板(
#带循环的滑块
滑块输入(“theMonth”,“Month”,1,8,1,step=1,
动画=动画选项(间隔=1000,循环=T,
播放按钮=T,暂停按钮=T)
),
#显示动画图形
主面板(
plotOutput(outputId=“case\u age\u plot”)
)
)
)
#服务器部分
服务器=功能(输入、输出){
#用于创建数据帧和图形的反应式表达式
幻灯机
library(shiny)
library(ggplot2)
# Shiny app with slider and animation
# ui section
ui = fluidPage(
# Title
titlePanel("Spend vs Age by Region"),
# Sidebar with slider and controls for animation
sidebarLayout(
# sidebar with slider
sidebarPanel(
# Slider with looping
sliderInput("theMonth", "Month", 1, 8, 1, step = 1,
animate=animationOptions(interval=1000, loop = T,
playButton = T, pauseButton = T))
),
# Show the animated graph
mainPanel(
plotOutput(outputId="case_age_plot")
)
)
)
# server section
server = function(input, output) {
# Reactive expression to create data frame and graph
aniGraph <- reactive({
# subset the data frame into the portion that has the data for the
# graph in the animation sequence
# Save subset of 'dat' and pass it to the plot
dat_sub <- dat[Month==input$theMonth,]
# create the graph
plot(dat_sub[,Age],dat_sub[,Spend],col=dat_sub[,Color],
pch=16, xlim=c(min(dat$Age)*.95,max(dat$Age)*1.1),
ylim=c(min(dat$Spend)*.95,max(dat$Spend)*1.1),
xlab="Age in Months",ylab="Dollars", las=1, cex.axis=.7)
legend("topright",inset=.05,c("Reg 1","Reg 2","Reg 3"),
pch=16,col=c("#E69F00","#D55E00","#009E73"),cex=.8)
})
# Show the graph
output$case_age_plot <- renderPlot({
aniGraph()
})
}
# run the app
runApp(list(ui = ui, server = server))