teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

やりたい事を書きました。

2021/12/24 19:55

投稿

amunagan
amunagan

スコア22

title CHANGED
File without changes
body CHANGED
@@ -17,4 +17,8 @@
17
17
  width=640, height=480, movie.name = "TEST.gif")
18
18
  ```
19
19
 
20
- これ点滅しちゃうのはしょうがないんですかね?0.05の数値下げるぐらいしか?
20
+ これ点滅しちゃうのはしょうがないんですかね?0.05の数値下げるぐらいしか?
21
+
22
+ 次にやりたいこととしては、
23
+ ・背景に画像を置きたい
24
+ ggplot2のggplotを使うんだと思うのですが・・・どう書けば良いのでしょうか・・・。

3

進捗を書きました。

2021/12/24 19:54

投稿

amunagan
amunagan

スコア22

title CHANGED
File without changes
body CHANGED
@@ -1,256 +1,20 @@
1
1
  タイトルの通りなのですが、いかにしていけば良いのでしょうか、
2
2
  調べましたが、目的とすることができる方法が見当たりません。
3
3
 
4
- https://mickey24.hatenablog.com/entry/20090614/1244965434
4
+ とりあえうここまでやってみた。
5
5
 
6
- ここにある
7
-
8
6
  ```R
9
- library(animation)
10
-
11
7
  wave <- function() {
12
- for(t in 1:100) {
8
+ for(t in 1:30) {
9
+ plot(3*t+10, 3*t+15, pch = "a", xlim=c(0, 100), ylim=c(0, 100), xaxt="n", yaxt="n", xlab = "", ylab = "", bty="n")
13
- plot(function(x){ sin(x + 0.08 * pi * t) },
10
+ plot(3*t-10, 3*t+5, pch = "b", xlim=c(0, 100), ylim=c(0, 100), xaxt="n", yaxt="n", xlab = "", ylab = "", bty="n")
14
- -pi, 2*pi, xlab="x", ylab="sin(x)",
11
+ plot(3*t, 3*t, pch = "c", xlim=c(0, 100), ylim=c(0, 100), xaxt="n", yaxt="n", xlab = "", ylab = "", bty="n")
15
- col="blue", lwd=3)
16
12
  }
17
13
  }
18
14
 
19
- saveMovie(wave(), interval=0.05, moviename="wave",
15
+ saveGIF(wave(), interval=0.05, moviename="wave",
20
16
  movietype="gif", outdir=getwd(),
21
- width=640, height=480)
17
+ width=640, height=480, movie.name = "TEST.gif")
22
18
  ```
23
19
 
24
- の方法であば、連続て変化する関数様子gif動画とて描れますが、
20
+ これ点滅ちゃうはしょうないんですかね?0.05の数値下げるぐらいしか
25
- 点(ただの点でなくアルファベットが好ましい)の動きではありません。
26
-
27
- あと、以前適当に、別サイト参考に作ったやつがあるのですが、
28
-
29
- ```R
30
- library(tidyverse)
31
- ## Warning: パッケージ 'tidyverse' はバージョン 3.5.3 の R の下で造られました
32
- ## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
33
- ## √ ggplot2 3.1.0 √ purrr 0.2.5
34
- ## √ tibble 2.1.3 √ dplyr 0.8.5
35
- ## √ tidyr 1.0.0 √ stringr 1.3.1
36
- ## √ readr 1.2.1 √ forcats 0.3.0
37
- ## Warning: パッケージ 'ggplot2' はバージョン 3.5.1 の R の下で造られました
38
- ## Warning: パッケージ 'tibble' はバージョン 3.5.3 の R の下で造られました
39
- ## Warning: パッケージ 'tidyr' はバージョン 3.5.3 の R の下で造られました
40
- ## Warning: パッケージ 'readr' はバージョン 3.5.1 の R の下で造られました
41
- ## Warning: パッケージ 'dplyr' はバージョン 3.5.3 の R の下で造られました
42
- ## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
43
- ## x dplyr::filter() masks stats::filter()
44
- ## x dplyr::lag() masks stats::lag()
45
-
46
- reset_df <- function(){
47
- df <- tibble(
48
- v0 = 20, #initial velocity
49
- theta = -1, #angle in radians
50
- # gravity変更済み
51
- gravity = 1, #this is just picked for the sclale
52
- adj = 0, #used in the bouncing effect
53
- decay = 0.8, #the bounciness of the ball
54
- color = "steelblue", #color of the ball
55
- cex = 2, #size of the ball
56
- t = 0, #time position of this ball
57
- xpos = 0, #current x position (will be updated)
58
- ypos = 0, #current y position (will be updated)
59
- )
60
- return(df)
61
- }
62
-
63
- generate_picture <- function(df, xrange = 1200, yrange = 900){
64
-
65
- range_xy_ratio <- xrange/yrange
66
-
67
- gg <- ggplot(df) +
68
- geom_point(aes(x = xpos, y = ypos, color = color, size = cex)) +
69
- labs(x = NULL, y = NULL) +
70
- scale_x_continuous(breaks = NULL, minor_breaks = NULL) +
71
- scale_y_continuous(breaks = NULL, minor_breaks = NULL) +
72
- theme(legend.position = "none",
73
- panel.background = element_rect(fill = "white", colour = "white")) +
74
- coord_cartesian(xlim = c(0,xrange), ylim = c(0,yrange)) +
75
- annotate("text", x=50, y=500, parse=TRUE,label="ああああああああああああ") +
76
- annotate("segment", x=170,xend=530, y=500,yend=450,colour="red", size=7, alpha = 0.1) +
77
- annotate("segment", x=530,xend=1030, y=450,yend=500,colour="red", size=7, alpha = 0.1) +
78
- annotate("segment", x=170,xend=530, y=430,yend=400,colour="red", size=7, alpha = 0.1) +
79
- annotate("segment", x=530,xend=1030, y=400,yend=430,colour="red", size=7, alpha = 0.1) +
80
- annotate("segment", x=0,xend=250, y=1000,yend=500,colour="blue", size=10, alpha = 0.1) +
81
- annotate("segment", x=30,xend=250, y=0,yend=100,colour="blue", size=10, alpha = 0.1) +
82
- annotate("segment", x=450,xend=620, y=480,yend=600,colour="blue", size=10, alpha = 0.1) +
83
- annotate("segment", x=170,xend=1030, y=600,yend=600,colour="blue", size=10, alpha = 0.1) +
84
- annotate("segment", x=400,xend=650, y=650,yend=500,colour="red", size=10, alpha = 0.1) +
85
- annotate("segment", x=400,xend=700, y=650,yend=700,colour="red", size=13, alpha = 0.1) +
86
- annotate("segment", x=700,xend=850, y=700,yend=400,colour="red", size=13, alpha = 0.1) +
87
- annotate("segment", x=500,xend=450, y=680,yend=800,colour="red", size=10, alpha = 0.1) +
88
- annotate("segment", x=580,xend=530, y=680,yend=800,colour="red", size=10, alpha = 0.1) +
89
- annotate("segment", x=660,xend=610, y=680,yend=800,colour="red", size=10, alpha = 0.1) +
90
- annotate("rect", xmin=250, xmax=900, ymin=100, ymax=500,colour="pink",fill="pink", alpha = 0.9) +
91
- annotate("segment", x=550,xend=550, y=100,yend=500,colour="red", size=20, alpha = 0.1)
92
- return(gg)
93
- }
94
-
95
- generate_picture(df)
96
-
97
- df <- reset_df()
98
-
99
- simulate_ball <- function(df, time_lapse=0.3){
100
- #new t value
101
- df <- df %>% mutate(t = t + time_lapse)
102
-
103
- #calculate new position
104
- if(df$t<30){
105
- df <- df %>%
106
- mutate(
107
- # ypos = v0 * t * sin(theta) - (gravity * t^2)
108
- ypos = v0 * t/2 * sin(theta) - (gravity * t^2) +950,
109
- xpos = v0 * t * cos(theta) + adj
110
- )
111
- }else{
112
- df <- df %>%
113
- mutate(
114
- # ypos = v0 * t * sin(theta) - (gravity * t^2)
115
- ypos = v0 * t/2 * sin(theta) + (gravity * t^2) +950,
116
- xpos = v0 * t * cos(theta) + adj
117
- )
118
- }
119
-
120
- # check for anything bouncing
121
- for (x in seq(nrow(df))) {
122
- if (df$ypos[x] < 0) {
123
- # reset the bounce
124
- df$adj[x] <- df$xpos[x]
125
- df$v0[x] <- df$v0[x] * df$decay[x]
126
- df$t[x] <- -time_lapse
127
- }
128
- }
129
- # if stuck, settle it.
130
- df$v0 <- ifelse(df$v0 < 0.01, 0, df$v0)
131
- df$t <- df$t + time_lapse
132
-
133
- return(df)
134
- }
135
-
136
- for(i in 1:5){
137
- print(i)
138
- df <- df %>% simulate_ball(.,time_lapse = 0.3)
139
- generate_picture(df) %>% print()
140
- }
141
-
142
- av::av_capture_graphics(expr = {
143
- df <- reset_df()
144
- #1:280が初期値
145
- for(i in 1:100){
146
- print(i)
147
- df <- df %>% simulate_ball(.,time_lapse = 0.3)
148
- generate_picture(df) %>% print()
149
- }
150
- }, output = "boundballwithlines2.mp4", framerate = 40)
151
- ```
152
- これは、画像が大量生成されてしまい、重く、かつアルファベットでなくボールであり、かつgif動画にならず、なんか仰々しくて扱いずらく感じ、もっとシンプルにできると思っています。
153
-
154
- どのようにすれば良いのでしょうか。
155
-
156
- てか、gifにするには最後のファイル形式を変えるだけでいいんですね、
157
- 画像生成しないパターンなら、別サイトのをほぼそのままで・・・
158
-
159
- ```R
160
- library(tidyverse)
161
- ## Warning: パッケージ 'tidyverse' はバージョン 3.5.3 の R の下で造られました
162
- ## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
163
- ## √ ggplot2 3.1.0 √ purrr 0.2.5
164
- ## √ tibble 2.1.3 √ dplyr 0.8.5
165
- ## √ tidyr 1.0.0 √ stringr 1.3.1
166
- ## √ readr 1.2.1 √ forcats 0.3.0
167
- ## Warning: パッケージ 'ggplot2' はバージョン 3.5.1 の R の下で造られました
168
- ## Warning: パッケージ 'tibble' はバージョン 3.5.3 の R の下で造られました
169
- ## Warning: パッケージ 'tidyr' はバージョン 3.5.3 の R の下で造られました
170
- ## Warning: パッケージ 'readr' はバージョン 3.5.1 の R の下で造られました
171
- ## Warning: パッケージ 'dplyr' はバージョン 3.5.3 の R の下で造られました
172
- ## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
173
- ## x dplyr::filter() masks stats::filter()
174
- ## x dplyr::lag() masks stats::lag()
175
- reset_df <- function(){
176
- df <- tibble(
177
- v0 = 100, #initial velocity
178
- theta = 1.4, #angle in radians
179
- gravity = 5, #this is just picked for the sclale
180
- adj = 0, #used in the bouncing effect
181
- decay = 0.8, #the bounciness of the ball
182
- color = "steelblue", #color of the ball
183
- cex = 2, #size of the ball
184
- t = 0, #time position of this ball
185
- xpos = 0, #current x position (will be updated)
186
- ypos = 0, #current y position (will be updated)
187
- )
188
- return(df)
189
- }
190
-
191
-
192
- generate_picture <- function(df, xrange = 1200, yrange = 900){
193
-
194
- range_xy_ratio <- xrange/yrange
195
-
196
- gg <- ggplot(df) +
197
- geom_point(aes(x = xpos, y = ypos, color = color, size = cex)) +
198
- labs(x = NULL, y = NULL) +
199
- scale_x_continuous(breaks = NULL, minor_breaks = NULL) +
200
- scale_y_continuous(breaks = NULL, minor_breaks = NULL) +
201
- theme(legend.position = "none",
202
- panel.background = element_rect(fill = "white", colour = "white")) +
203
- coord_cartesian(xlim = c(0,xrange), ylim = c(0,yrange))
204
-
205
- return(gg)
206
- }
207
-
208
-
209
- generate_picture(df)
210
-
211
- df <- reset_df()
212
-
213
- simulate_ball <- function(df, time_lapse=0.3){
214
- #new t value
215
- df <- df %>% mutate(t = t + time_lapse)
216
-
217
- #calculate new position
218
- df <- df %>%
219
- mutate(
220
- ypos = v0 * t * sin(theta) - (gravity * t^2),
221
- xpos = v0 * t * cos(theta) + adj
222
- )
223
-
224
- # check for anything bouncing
225
- for (x in seq(nrow(df))) {
226
- if (df$ypos[x] < 0) {
227
- # reset the bounce
228
- df$adj[x] <- df$xpos[x]
229
- df$v0[x] <- df$v0[x] * df$decay[x]
230
- df$t[x] <- -time_lapse
231
- }
232
- }
233
- # if stuck, settle it.
234
- df$v0 <- ifelse(df$v0 < 0.01, 0, df$v0)
235
- df$t <- df$t + time_lapse
236
-
237
- return(df)
238
- }
239
-
240
- for(i in 1:5){
241
- print(i)
242
- df <- df %>% simulate_ball(.,time_lapse = 0.3)
243
- generate_picture(df) %>% print()
244
- }
245
-
246
- av::av_capture_graphics(expr = {
247
- df <- reset_df()
248
- for(i in 1:280){
249
- print(i)
250
- df <- df %>% simulate_ball(.,time_lapse = 0.3)
251
- generate_picture(df) %>% print()
252
- }
253
- }, output = "test2.gif", framerate = 40)
254
- ```
255
- このようにしたらできますが・・・あとはボールをアルファベットに変え、
256
- ボール数を増やし、かつ背景をつけたいが・・・。

2

一部進展。

2021/12/24 19:26

投稿

amunagan
amunagan

スコア22

title CHANGED
File without changes
body CHANGED
@@ -151,4 +151,106 @@
151
151
  ```
152
152
  これは、画像が大量生成されてしまい、重く、かつアルファベットでなくボールであり、かつgif動画にならず、なんか仰々しくて扱いずらく感じ、もっとシンプルにできると思っています。
153
153
 
154
- どのようにすれば良いのでしょうか。
154
+ どのようにすれば良いのでしょうか。
155
+
156
+ てか、gifにするには最後のファイル形式を変えるだけでいいんですね、
157
+ 画像生成しないパターンなら、別サイトのをほぼそのままで・・・
158
+
159
+ ```R
160
+ library(tidyverse)
161
+ ## Warning: パッケージ 'tidyverse' はバージョン 3.5.3 の R の下で造られました
162
+ ## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
163
+ ## √ ggplot2 3.1.0 √ purrr 0.2.5
164
+ ## √ tibble 2.1.3 √ dplyr 0.8.5
165
+ ## √ tidyr 1.0.0 √ stringr 1.3.1
166
+ ## √ readr 1.2.1 √ forcats 0.3.0
167
+ ## Warning: パッケージ 'ggplot2' はバージョン 3.5.1 の R の下で造られました
168
+ ## Warning: パッケージ 'tibble' はバージョン 3.5.3 の R の下で造られました
169
+ ## Warning: パッケージ 'tidyr' はバージョン 3.5.3 の R の下で造られました
170
+ ## Warning: パッケージ 'readr' はバージョン 3.5.1 の R の下で造られました
171
+ ## Warning: パッケージ 'dplyr' はバージョン 3.5.3 の R の下で造られました
172
+ ## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
173
+ ## x dplyr::filter() masks stats::filter()
174
+ ## x dplyr::lag() masks stats::lag()
175
+ reset_df <- function(){
176
+ df <- tibble(
177
+ v0 = 100, #initial velocity
178
+ theta = 1.4, #angle in radians
179
+ gravity = 5, #this is just picked for the sclale
180
+ adj = 0, #used in the bouncing effect
181
+ decay = 0.8, #the bounciness of the ball
182
+ color = "steelblue", #color of the ball
183
+ cex = 2, #size of the ball
184
+ t = 0, #time position of this ball
185
+ xpos = 0, #current x position (will be updated)
186
+ ypos = 0, #current y position (will be updated)
187
+ )
188
+ return(df)
189
+ }
190
+
191
+
192
+ generate_picture <- function(df, xrange = 1200, yrange = 900){
193
+
194
+ range_xy_ratio <- xrange/yrange
195
+
196
+ gg <- ggplot(df) +
197
+ geom_point(aes(x = xpos, y = ypos, color = color, size = cex)) +
198
+ labs(x = NULL, y = NULL) +
199
+ scale_x_continuous(breaks = NULL, minor_breaks = NULL) +
200
+ scale_y_continuous(breaks = NULL, minor_breaks = NULL) +
201
+ theme(legend.position = "none",
202
+ panel.background = element_rect(fill = "white", colour = "white")) +
203
+ coord_cartesian(xlim = c(0,xrange), ylim = c(0,yrange))
204
+
205
+ return(gg)
206
+ }
207
+
208
+
209
+ generate_picture(df)
210
+
211
+ df <- reset_df()
212
+
213
+ simulate_ball <- function(df, time_lapse=0.3){
214
+ #new t value
215
+ df <- df %>% mutate(t = t + time_lapse)
216
+
217
+ #calculate new position
218
+ df <- df %>%
219
+ mutate(
220
+ ypos = v0 * t * sin(theta) - (gravity * t^2),
221
+ xpos = v0 * t * cos(theta) + adj
222
+ )
223
+
224
+ # check for anything bouncing
225
+ for (x in seq(nrow(df))) {
226
+ if (df$ypos[x] < 0) {
227
+ # reset the bounce
228
+ df$adj[x] <- df$xpos[x]
229
+ df$v0[x] <- df$v0[x] * df$decay[x]
230
+ df$t[x] <- -time_lapse
231
+ }
232
+ }
233
+ # if stuck, settle it.
234
+ df$v0 <- ifelse(df$v0 < 0.01, 0, df$v0)
235
+ df$t <- df$t + time_lapse
236
+
237
+ return(df)
238
+ }
239
+
240
+ for(i in 1:5){
241
+ print(i)
242
+ df <- df %>% simulate_ball(.,time_lapse = 0.3)
243
+ generate_picture(df) %>% print()
244
+ }
245
+
246
+ av::av_capture_graphics(expr = {
247
+ df <- reset_df()
248
+ for(i in 1:280){
249
+ print(i)
250
+ df <- df %>% simulate_ball(.,time_lapse = 0.3)
251
+ generate_picture(df) %>% print()
252
+ }
253
+ }, output = "test2.gif", framerate = 40)
254
+ ```
255
+ このようにしたらできますが・・・あとはボールをアルファベットに変え、
256
+ ボール数を増やし、かつ背景をつけたいが・・・。

1

gifでしたー。

2021/12/24 17:19

投稿

amunagan
amunagan

スコア22

title CHANGED
@@ -1,1 +1,1 @@
1
- Rで、png動画を作りたい、画像を作成せずにプログラム内で、アルファベットを動かす。
1
+ Rで、gif動画を作りたい、画像を作成せずにプログラム内で、アルファベットを動かす。
body CHANGED
File without changes