Plotting a curved line directly through some datapoints in R (ggplot) - r

I am struggling with plotting a figure on the motion profile of a simulator.
What I'm trying to show are the displacements of the simulator over time.
Some sample data:
Time = c(0, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44)
Displacement = c(0, 0, 7, 0, 0, 7, 0, 0, -7, 0, 0)
DD = as.data.frame(Time, Displacement)
I want to plot a curved/smoothed line that goes directly through these datapoints.
enter image description hereUsing geom_line off course generates a spiky line.
The closest I have gotten to get a smoother line is using this piece of code:
ggplot(DD, aes(x=Time, y=Displacement, c(0,7))) +
geom_smooth(method = "loess", se = FALSE, span = 0.2, colour="black")
enter image description hereHowever, the curves are still quite spiky and I am hoping to get a more beautiful plot.
Hoping anyone can be of help :)
Anne

Try with a polynomial fit:
library(ggplot2)
#Code
ggplot(DD, aes(x=Time, y=Displacement, c(0,7))) +
geom_smooth(method = "lm",formula = y~poly(x,3), se = FALSE, span = 0.2, colour="black")
Output:

Related

Adding title and subheadings to histogram in r keeps failing

I am brand new to R and I've been trying to look around StackOverflow for an answer but have not found something that works. I would like to add a title and color to my histogram graph but it keeps on failing for some reason. I've made my data in descending order and I'd like to title both the main heading and the x-axis and y-axis with differing names. Thank you so much in advance!
Here is the code that I am using:
CompaniesOrder=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)
GpMean=c(9905.5789474, 8794.1052632, 4893.0526316, 3723.1052632,
3045.6069474,1518.0444211,1200.4994211,842.4464737,765.5630588,
647.6224211,543.739875,324.5206316,217.9081579,213.0212857,
168.1743158,149.2178947,136.6547895,90.5400526,66.8915333,
57.7370526,8.3272143,3.3801053,0.2194286,0,0,0)
GpMeanTreatment <- data.frame(CompaniesOrder, GpMean)
library(ggplot2)
ggplot(GpMeanTreatment, aes(x = CompaniesOrder, y = GpMean)) +
geom_bar(stat="identity")
ggplot(GpMeanTreatment, aes(x = CompaniesOrder, y = GpMean, fill = CompaniesOrder)) +
geom_col() +
scale_fill_gradient(low='red', high='yellow') +
labs(x = "Your X axis title", y = "Your Y axis title", title = "Your Main Title")
Hint: geom_bar(stat="identity") is just a long way of using geom_col()

ggplot2 - geom_histogram / scale_fill_manual

I am working the following dataframe (df):
df$GP<-c(0,0,0,1,1,2,3,3,3,3,4,4,9,15,18,18,19,19,20,20,21,22,22,23)
df$colour<-c("g","g","g","g","g","g","g","g","g","g","g","g","t","t","g","g","g","g","g","g","g","g","g","g")
I want the histogram below, but showing a different fill for colour=="g" and colour=="t".
However, running the following code, the bars labelled colour=="t", go out of scale (up to 1 - plot2) whereas should be at 0.25 (plot1).
ggplot(data=df,aes(x=GP,y=..ndensity..))+geom_histogram(bins=25,aes(fill=colour))+scale_fill_manual(values=c("black","grey"))
Do you have any idea of how this could be achieved?
Thank you very much for your help with this one!
I used a tibble as the data type for dataset, with different tibble variable names.
the result is just as you want.
tb <- tibble(
tbx = c(0, 0, 0, 1, 1, 2, 3, 3, 3, 3, 4, 4, 9, 15, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23),
tby = c("g","g","g","g","g","g","g","g","g","g","g","g","t","t","g","g","g","g","g","g","g","g","g","g")
)
ggplot(tb, aes(tbx, tby = ..ndensity..)) +
geom_histogram(bins = 25, aes(fill = tby)) +
scale_fill_manual(values = c("red", "grey"))
and this is the output plot:
I hope this addresses your question

Plot smooth graph from user defined points

I would like to draw a smooth graph, with just specifying some points. An example would be the following graph:
[desired graph]
I would like to draw it with the following points:
x <- c(7, 8, 9, 11, 12, 13, 16, 17, 18)
y <- c(0.05, 0.95, 0.3, 0.3, 0.7, 0.3, 0.3, 0.95, 0.2)
How can I estimate the missing points, so that it results in a smooth graph that looks similar to the figure?
A good way to do this is with a spline function. That will give you not only the curve, but a function to estimate y for any x.
SF = splinefun(x,y)
curve(SF, xlim=c(7,18))
points(x,y, pch=16, col="red")
For 10 data points you can add a 9th order polynomial to obtain something like it. This can be fit directly to your data with geom_smooth from ggplot2 in the following way
x <- c(7, 8, 9, 11, 12, 13, 16, 17, 18)
y <- c(0.05, 0.95, 0.3, 0.3, 0.7, 0.3, 0.3, 0.95, 0.2)
df <- data.frame(x,y)
library(ggplot2)
ggplot(df, aes(x,y)) +
geom_point() +
stat_smooth(method="lm",
formula=y ~ poly(x, 9, raw=TRUE),
colour="red")

Changing legend labels in ggplotly()

I have a plot of polygons that are colored according to a quantitative variable in the dataset being cut off at certain discrete values (0, 5, 10, 15, 20, 25). I currently have a static ggplot() output that "works" the way I intend. Namely, the legend values are the cut off values (0, 5, 10, 15, 20, 25). The static plot is below -
However, when I simply convert this static plot to an interactive plot, the legend values become hexadecimal values (#54278F, #756BB1, etc.) instead of the cut off values (0, 5, 10, 15, 20, 25). A screenshot of this interactive plot is shown below -
I am trying to determine a way to change the legend labels in the interactive plot to be the cut off values (0, 5, 10, 15, 20, 25). Any suggestions or support would be greatly appreciated!
Below is the code I used to create the static and interactive plot:
library(plotly)
library(ggplot2)
library(RColorBrewer)
set.seed(1)
x = abs(rnorm(30))
y = abs(rnorm(30))
value = runif(30, 1, 30)
myData <- data.frame(x=x, y=y, value=value)
cutList = c(5, 10, 15, 20, 25)
purples <- brewer.pal(length(cutList)+1, "Purples")
myData$valueColor <- cut(myData$value, breaks=c(0, cutList, 30), labels=rev(purples))
# Static plot
sp <- ggplot(myData, aes(x=x, y=y, fill=valueColor)) + geom_polygon(stat="identity") + scale_fill_manual(labels = as.character(c(0, cutList)), values = levels(myData$valueColor), name = "Value")
# Interactive plot
ip <- ggplotly(sp)
Label using the cut points and use scale_fill_manual for the colors.
cutList = c(5, 10, 15, 20, 25)
purples <- brewer.pal(length(cutList)+1, "Purples")
myData$valueLab <- cut(myData$value, breaks=c(0, cutList, 30), labels=as.character(c(0, cutList)))
# Static plot
sp <- ggplot(myData, aes(x=x, y=y, fill=valueLab)) + geom_polygon(stat="identity") + scale_fill_manual(values = rev(purples))
# Interactive plot
ip <- ggplotly(sp)

Animate ggplot2 stacked line chart in R

I'm trying to animate a stacked line chart in ggplot2.
Here's the plot I'd like to animate:
Here's the code to generate a similar plot:
#Data
mydata <- data.frame(year=rep(1:6, times=4),
activity=as.factor(rep(c("research","coursework","clinical work","teaching"), each=6)),
time=c(40, 35, 40, 60, 85, 90,
50, 40, 10, 0, 5, 0,
5, 20, 20, 40, 10, 10,
5, 5, 30, 0, 0, 0))
mydata$activity <- ordered(mydata$activity, levels = c("research","clinical work","coursework","teaching"))
labels <- data.frame(activity=c("research","coursework","clinical work","teaching"),
xaxis=c(5, 1.8, 2.5, 2.97),
yaxis=c(25, 70, 48, 90))
#Plot
ggplot(mydata, aes(x=year, y=time, fill=activity)) +
geom_area(stat="smooth", span=.35, color="black") +
theme(legend.position = "none") +
geom_text(data=labels, aes(x=xaxis, y=yaxis, label=activity)) +
ggtitle("Time in Different Activities by Year in Program") +
ylab("Percentage of Time") +
xlab("Year in Program")
I'm looking for the first image to display all axes and text. The second iteration, I'd like to gradually reveal over time, from left to right, the "Research" stacked line (including color and border). The third iteration, I'd like to gradually reveal, from left to right, the "Clinical Work" stacked line. Fourth, the "Coursework" stacked line. And finally, the "Teaching" stacked line.
Ideally, the output format would be very smooth (no jagged jumps) and would be compatible with PowerPoint.
Here is an R-based solution. It saves individual figures (.png) that can be iterated through within a presentation.
Alternatively,you could create an animation (for example converting to .gif) using ImageMagick http://www.imagemagick.org/
#Data
mydata <- data.frame(year=rep(1:6, times=4),
activity=as.factor(rep(c("research","coursework","clinical work","teaching"), each=6)),
time=c(40, 35, 40, 60, 85, 90,
50, 40, 10, 0, 5, 0,
5, 20, 20, 40, 10, 10,
5, 5, 30, 0, 0, 0))
#order the activities and then the dataframe
mydata$activity <- ordered(mydata$activity, levels = c("research","clinical work","coursework","teaching"))
mydata <- mydata[order(mydata$activity),]
#labels
labels <- data.frame(activity=c("research","coursework","clinical work","teaching"),
xaxis=c(5, 1.8, 2.5, 2.97),
yaxis=c(25, 70, 48, 90))
#creates a function to draws a plot for each activity
draw.stacks<-function(leg){
int <- leg*6
a<-ggplot(data=mydata[1:int,], aes(x=year, y=time, fill=activity))+
geom_area(stat="smooth", span=.35, color="black") +
theme_bw()+
scale_fill_discrete(limits = c("research","clinical work","coursework","teaching"), guide="none")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
coord_cartesian(xlim=c(1,6),ylim=c(0,100))+
geom_text(data=labels, aes(x=xaxis, y=yaxis, label=activity)) +
ggtitle("Time in Different Activities by Year in Program") +
ylab("Percentage of Time") +
xlab("Year in Program")
print(a)
}
# save individual png figures
for (i in 0:4) {
png(paste("activity", i, "png", sep="."))
draw.stacks(i)
dev.off()
}
Sorry for bringing in a non-programmer solution, but I would simply generate plots for each iteration separately, put them in power point (one plot on one slide), and use some fancy slide transition effects (I tried the Random Bars effect on your example, and it looked nice).
If you determined to find an R-based solution, you can take a look at the animate package (see a Strategic Zombie Simulation example here).

Resources