This question already has an answer here:
Control speed of a gganimation
(1 answer)
Closed 3 years ago.
I have set up an animation using gganimate in R. This animation is just too fast. How to set the speed of the frames?
Also the points disappears when showing the next dot. Is it possible that the points are show one by one so at the end I have a full scatter plot?
How can I fix this?
This is the testdata and my code so far:
#Test data
ID Weight Color Time
A 27 Red 1
A 11 Red 2
A 37 Red 3
A 49 Red 4
A 10 Red 5
A 25 Blue 6
A 49 Blue 7
A 20 Blue 8
A 21 Blue 9
A 36 Blue 10
A 24 Green 11
A 32 Green 12
A 47 Green 13
A 35 Green 14
A 24 Green 15
A 49 Yellow 16
A 42 Yellow 17
A 39 Yellow 18
A 22 Yellow 19
A 47 Yellow 20
#R code
library(plyr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(gganimate)
p <- ggplot(dataset, aes(x=Color, y=Weight)) + geom_point()
order <- as.numeric(dataset$Time)
p +
transition_time(order) +
labs(title = "TIME: {frame_time}") + enter_fade()
Possible duplicate of: Control speed of a gganimation
In any case, I think this does what you want, if you play around with the fps argument a litte:
p <- ggplot(dataset, aes(x=Color, y=Weight)) + geom_point()
order <- as.numeric(dataset$Time)
gif <- p +
transition_time(order) +
labs(title = "TIME: {frame_time}") + enter_fade()
animate(gif, fps = 2)
Related
Please help me;
I made a plot comprising some red and blue points using ggplot.
Now I want to connect the red points to each other with a line and connect the blue points to each other with another line
These are my codes
m <- as.factor(c(7,"12 PCA", 21, "24 PCA", "31 PCA", 38, 70))
## Then we plot the points
ggplot(pH, aes(x= m, y=All))+ ylim(60,100)+
scale_x_discrete(limits=c(7,"12 PCA", 21, "24 PCA", "31 PCA", 38, 70))+
geom_point(data=pH, aes(y=All), colour = 'red', size =1)+
geom_point(data=pH, aes(y=Test), colour = 'blue', size=1)
And this is my plot
How can I do that?
Thanks
I think it's generally best to not work with independent vectors of data when possible, instead placing it in a single frame. In this case, one column will be used to indicate which "group" the dots belong to.
dat <- data.frame(m=c(m,m), All=c(94,95,96,95,94,95,96, 74,67,74,67,68,73,74), grp=c(rep("red",7), rep("blue",7)))
dat
# m All grp
# 1 7 94 red
# 2 12 PCA 95 red
# 3 21 96 red
# 4 24 PCA 95 red
# 5 31 PCA 94 red
# 6 38 95 red
# 7 70 96 red
# 8 7 74 blue
# 9 12 PCA 67 blue
# 10 21 74 blue
# 11 24 PCA 67 blue
# 12 31 PCA 68 blue
# 13 38 73 blue
# 14 70 74 blue
Plot code:
library(ggplot2)
ggplot(dat, aes(m, All, group=grp, color=grp)) +
geom_point() +
geom_line() +
scale_color_manual(values = c(blue = "blue", red = "red"))
I would like to compile some data into a ggplot() line plot of different colors.
It's rainfall in various places over 100 days, and the data is quite different between locations which is giving me fits.
I've tried using different suggestions from this forum and they don't seem to be working well for this data. Sample data:
Time Location1 Location2 Location3
0 48 99.2966479761526 2
1 51 98.7287820735946 4
2 58 98.4803262236528 4.82842712474619
3 43 97.8941490454599 5.46410161513775
4 47 96.6091435402632 6
5 47 95.207282404881 6.47213595499958
6 41 94.8696538619697 6.89897948556636
7 34 94.6514389757067 7.29150262212918
8 40 93.7297335476615 7.65685424949238
9 57 93.2440731907263 8
My code thus far is
ggplot(Rain) +
geom_line(aes(x=Time,y=Location1,col="red")) +
geom_line(aes(x=Time,y=Location2,col="blue")) +
geom_line(aes(x=Time,y=Location3,col="green")) +
scale_color_manual(labels = c("Location 1","Location 2","Location 3"),
values = c("red","blue","green")) +
xlab("Time (Days)") + ylab("Rainfall (Inches)") + labs(color="Locations") +
ggtitle("Rainfall Over 100 Days In Three Locations")
So far it gives me everything that I want but for some reason the colors are wrong when I plot it, i.e. it plots location 1 in green while I told it red in my first geom_line.
library(tidyr)
library(ggplot2)
df_long <- gather(data = df1, Place, Rain, -Time)
ggplot(df_long) +
geom_line(aes(x=Time, y=Rain, color=Place))
Data:
df1 <- read.table(text="Time Location1 Location2 Location3
0 48 99.2966479761526 2
1 51 98.7287820735946 4
2 58 98.4803262236528 4.82842712474619
3 43 97.8941490454599 5.46410161513775
4 47 96.6091435402632 6
5 47 95.207282404881 6.47213595499958
6 41 94.8696538619697 6.89897948556636
7 34 94.6514389757067 7.29150262212918
8 40 93.7297335476615 7.65685424949238
9 57 93.2440731907263 8",
header=T, stringsAsFactors=F)
I have a problem with drawing stacked barplot with ggplot. My data looks like this:
timeInterval TotalWilling TotalAccepted SimID
1 16 12 Sim1
1 23 23 Sim2
1 63 60 Sim3
1 69 60 Sim4
1 61 60 Sim5
1 60 54 Sim6
2 16 8 Sim1
2 23 21 Sim2
2 63 52 Sim3
2 69 64 Sim4
2 61 45 Sim5
2 60 32 Sim6
3 16 14 Sim1
3 23 11 Sim2
3 63 59 Sim3
3 69 69 Sim4
3 61 28 Sim5
3 60 36 Sim6
I would like to draw a stacked barplot for each simID over a timeInterval, and Willing and Accepted should be stacked. I achieved the barplot with the following simple code:
dat <- read.csv("myDat.csv")
meltedDat <- melt(dat,id.vars = c("SimID", "timeInterval"))
ggplot(meltedDat, aes(timeInterval, value, fill = variable)) + facet_wrap(~ SimID) +
geom_bar(stat="identity", position = "stack")
I get the following graph:
Here my problem is that I would like to put percentages on each stack. Which means, I want to put percentage as for Willing label: (Willing/(Willing+Accepted)) and for Accepted part, ((Accepted/(Accepted+Willing)) so that I can see how many percent is willing how many is accepted such as 45 on red part of stack to 55 on blue part for each stack. I cannot seem to achieve this kind of labeling.
Any hint is appreciated.
applied from Showing data values on stacked bar chart in ggplot2
meltedDat <- melt(dat,id.vars = c("SimID", "timeInterval"))
meltedDat$normvalue <- meltedDat$value
meltedDat$valuestr <- sprintf("%.2f%%", meltedDat$value, meltedDat$normvalue*100)
meltedDat <- ddply(meltedDat, .(timeInterval, SimID), transform, pos = cumsum(normvalue) - (0.5 * normvalue))
ggplot(meltedDat, aes(timeInterval, value, fill = variable)) + facet_wrap(~ SimID) + geom_bar(stat="identity", position = "stack") + geom_text(aes(x=timeInterval, y=pos, label=valuestr), size=2)
also, it looks like you may have some of your variables coded as factors.
I have a df like this.
> te1.m.comb
temp variable value
1 35 Light.180.1.x.MAX1 10.398333
3 35 Dark.180.1.x.MAX1 -4.337142
5 35 Light.288.5.x.MAX3 17.825376
7 35 Dark.288.5.x.MAX3 -4.331998
9 35 Light.D125.x.K1 15.150205
11 35 Dark.D125.x.K1 -4.376553
13 35 Light.SO443WL.x.SO479WL 11.003542
15 35 Dark.SO443WL.x.SO479WL -3.216878
17 35 Light.SO450WL.x.SO465WL 15.970640
19 35 Dark.SO450WL.x.SO465WL -3.109330
21 35 Light.SO459WL.x.SO469WL 11.393617
23 35 Dark.SO459WL.x.SO469WL -3.857454
2 40 Light.180.1.x.MAX1 8.589651
4 40 Dark.180.1.x.MAX1 -5.569157
6 40 Light.288.5.x.MAX3 15.977499
8 40 Dark.288.5.x.MAX3 -5.582502
10 40 Light.D125.x.K1 13.651815
12 40 Dark.D125.x.K1 -5.243391
14 40 Light.SO443WL.x.SO479WL 8.518077
16 40 Dark.SO443WL.x.SO479WL -4.861841
18 40 Light.SO450WL.x.SO465WL 13.691814
20 40 Dark.SO450WL.x.SO465WL -4.514559
22 40 Light.SO459WL.x.SO469WL 9.262019
24 40 Dark.SO459WL.x.SO469WL -5.138836
I would like to plot the relative proportions using ggplot. For example, instead of plotting each of the variable and its value, i would like to plot the ratio value of Light.180.1.x.MAX1 / Dark.180.1.x.MAX1 i.e 10.398333/-4.337142 and so on. How can i do that in ggplot?
Here is my boxplot code which just plots each of the variable and its value..
ggplot(te1.m.comb, aes(variable, value)) + geom_boxplot() + facet_grid(temp ~.)
I renamed your data.frame df so that the reading can be easy and added the ratio column:
df$ratio = with(df, c(value/c(value[-1],NA)))
Here is the plot:
library(ggplot2)
ggplot(df, aes(variable, ratio)) +
geom_bar(stat = "identity") +
facet_grid(temp~.) + ยจ
scale_y_reverse()
I have a data frame imported in excel with the following values:
> dt <- read.csv(file="teste1.csv",head=TRUE,sep=";")
> dt
hour occur time tt
1 1 one 00:00:59 59
2 2 one 08:40:02 31202
3 3 one 07:09:59 25799
4 4 one 01:22:16 4936
5 5 one 01:30:28 5428
6 6 one 01:28:57 5337
7 7 one 19:05:34 68734
8 8 one 01:57:47 7067
9 9 one 00:13:17 797
10 10 one 12:14:48 44088
11 11 one 23:24:43 84283
12 12 one 13:23:14 48194
13 13 one 02:28:51 8931
14 14 one 14:21:24 51684
15 15 one 13:26:14 48374
16 16 one 00:27:24 1644
17 17 one 15:56:51 57411
18 18 one 11:07:50 40070
19 19 one 07:18:18 26298
20 20 one 07:33:13 27193
21 21 one 10:02:03 36123
22 22 one 11:30:32 41432
23 23 one 21:21:27 76887
24 24 one 00:49:18 2958
25 1 two 21:01:11 75671
26 2 two 11:00:40 39640
27 3 two 21:40:09 78009
28 4 two 01:05:37 3937
29 5 two 00:44:17 2657
30 6 two 12:43:21 45801
31 7 two 10:53:49 39229
32 8 two 08:29:09 30549
33 9 two 05:07:46 18466
34 10 two 17:32:37 63157
35 11 two 09:35:16 34516
36 12 two 03:04:19 11059
37 13 two 23:09:13 83353
38 14 two 01:15:49 4549
39 15 two 14:24:33 51873
40 16 two 01:12:53 4373
41 17 two 21:20:11 76811
42 18 two 02:25:21 8721
43 19 two 01:17:37 4657
44 20 two 15:07:50 54470
45 21 two 22:27:32 80852
46 22 two 01:41:07 6067
47 23 two 09:40:23 34823
48 24 two 05:31:17 19877
I want to create a circular time with stacked rose based on the data frame, ie, each stacked rose are grouped by column occur, and the size is defined by column time.
The column hour indicates the x position of each rose.
So I tried in this way but the result doesn't match with what I want:
ggplot(dt, aes(x = hour, fill = occur)) + geom_histogram(breaks = seq(0,
24), width = 2, colour = "grey") + coord_polar(start = 0) + theme_minimal() +
scale_fill_brewer() + scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0,
24))
What I'm doing wrong? I want something like this http://blog.odotech.com/Portals/57087/images/French%20landfill%20wind%20rose.png
I hope I've explained correctly. Thank you!
Not sure, but hope it helps:
Convert your time value to numeric (I used chron package, but there are numerous other ways, so you don't have to call this library, but it's just to make it more straighforward):
library(chron)
x$tt<-hours(times(x$time))*3600+minutes(times(x$time))*60+seconds(times(x$time))
And make a graph:
p<-ggplot(x, aes(x = hour, y=tt,fill = occur)) +
geom_bar(breaks = seq(0,24), width = 2, colour="grey",stat = "identity") +
theme_minimal() +
scale_fill_brewer()+coord_polar(start=0)+
scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0,24))
Is that ok?
Here some cases have only 1 colors, but it's due to the scaling issues, as some have time near 24 hours, while others are in seconds only.
You can try separate graphs using facet (it's better to play with colors afterwards :))
p+facet_grid(~occur)+ theme(axis.title.y = theme_blank(),
axis.text.y = theme_blank())
The circular graph is good if you're comparing data by hours, but if you also want to compare differences in occur variable, think it's better to show in old fashion bar graphs.