Shading with multiple plots using Plots.jl - julia

I would like to replicate the figure using Plots.jl.
However, I have two problems
I do not know how to shade certain period of the figure
I want to draw multiple plots as the example
How can I do that?

Maybe this can get you started:
using Plots
subplots = 3
time = 250
shade_xlims = [125,140]
data_matrix = Plots.fakedata(time, subplots)
p = plot(data_matrix, layout=(subplots,1), xlabel = "time");
for i in 1:subplots
ymin,ymax = extrema(data_matrix[:,i]) # determine ylims for shade
plot!(p[i], # apply shade to each subplot
shade_xlims, # xlims for shade
[0,0], # dummy y coordinates
fillrange = (ymin,ymax), # apply ylims
fillalpha = 0.5, # set transparency
fillcolor=:gray, # set shade color
label = "") # avoid shade labels
end
p # show the final graph
Cheers!

Related

Rotate x-axis labels at a given degree for boxplot in R

I generate a boxplot with code below:
boxplot(top10threads$affect ~ top10threads$ThreadID[], data = top10threads, xlab = "10 biggest Threads", ylab = "Affect", col=(c("gold","darkgreen")), srt=45)
But as you may notice that some labels in x-axis are missing, so I want to rotate them into 45 degrees. I added srt=45, but it doesn't work.
By setting las=2 can rotate them vertically, but it's not exactly I need.
How could I do that? Thanks.
First, store the output of boxplot() as a object. It contains names of the groups. You can use $names to get them. Then use text() to add labels on the axis. The argument srt works on text().
bp <- boxplot(y ~ x, data = df, col = c("gold", "darkgreen"), xaxt = "n")
tick <- seq_along(bp$names)
axis(1, at = tick, labels = FALSE)
text(tick, par("usr")[3] - 0.3, bp$names, srt = 45, xpd = TRUE)
Data
df <- data.frame(x = sample(100:110, 100, TRUE), y = rnorm(100))
Some test data:
mydata=lapply(1:5,function(i) rnorm(100,mean=i))
names(mydata)=c("first","second","third","fourth","fifth")
First, plot the boxplot with no x-axis:
boxplot(mydata,xaxt="n",xlab="")
Then, we make a function to add textual x-axis labels:
x_axis_labels=function(labels,every_nth=1,...) {
axis(side=1,at=seq_along(labels),labels=F)
text(x=(seq_along(labels))[seq_len(every_nth)==1],
y=par("usr")[3]-0.075*(par("usr")[4]-par("usr")[3]),
labels=labels[seq_len(every_nth)==1],xpd=TRUE,...)
}
# axis() draws the axis with ticks at positions specified by at. Again, we don't plot the labels yet.
# text() plots the labels at positions given by x and y.
# We estimate the y-positions from the values of the y-axis (using par("usr")),
# and specify xpd=TRUE to indicate that we don't want to crop plotting to within the plot area
# Note that we select the [seq_len(every_nth)==1] elements of both the x positions and the labels,
# so we can easily skip labels if there would be too many to cram in otherwise.
# Finally, we leave a ... in the function so we can pass additional arguments to text()
Finally, we call the new function to plot the axis tick labels:
x_axis_labels(labels=names(mydata),every_nth=1,adj=1,srt=45)
Here we take advantage of the ... in the function to pass the rotation/justification parameters: adj=1 specifies to right-justify the text labels, and srt=45 indicates to rotate them by 45 degrees.

How to partly colorize histogram?

I've been trying to color specific bins above a defined threshold in the following data frame (df)
df <- read.table("https://pastebin.com/raw/3En2GWG6", header=T)
I've been following this example (Change colour of specific histogram bins in R), but I cannot seem to get this to adapt their suggestions to my data, so I wanted to ask you here at stackoverflow
I would like all bins with values above 0.100 to be "red", and the rest all to be either no color, or just black (I defined black, but I would prefer no color)
Here is what I tried:
col<-(df$consumption>=0.100)
table(col) # I can see 40 points above 100, the rest below
col[which(col=="TRUE")] <- "firebrick1"
col[which(col=="FALSE")] <- "black"
hist(df$consumption, breaks = 1000, xlim = c(0,0.2), col=col,xlab= "Consumption [MG]")
However, the whole graph is red, and that doesn't make sense..?
In other words, I would like anything to the right side of the line below to be red
hist(df$consumption, breaks = 1000, xlim = c(0,0.2),xlab= "Consumption [MG]")
abline(v=c(.100), col=c("red"),lty=c(1), lwd=c(5))
Simply plot two histograms on top of each other using add=TRUE and sub-setting the second.
hist(df$consumption, breaks=1000, xlim=c(0,.2),xlab= "Consumption [MG]")
hist(df$consumption[df$consumption > .100], breaks=1000, xlim=c(0,.2), col=2, add=TRUE)
abline(v=.100, col=2, lty=3)
Here is along the lines of what you were doing. You do not want to count the points above your cutoff, but rather the number of histogram bins above your cutoff.
# store the histogram as an object
h <- hist(df$consumption, breaks = 1000)
# extract out the breaks, and assign a color vector accordingly
cols <- ifelse(h$breaks > 0.1, "firebrick1", "black")
# use the color vector
plot(h, col = cols, xlim=c(0,.2),xlab= "Consumption [MG]")
abline(v=c(.100), col=c("red"),lty=c(1), lwd=c(5))

Twosided Barplot in R with different data

I was wondering if it's possible to get a two sided barplot (e.g. Two sided bar plot ordered by date) that shows above Data A and below Data B of each X-Value.
Data A would be for example the age of a person and Data B the size of the same person. The problem with this and the main difference to the examples above: A and B have obviously totally different units/ylims.
Example:
X = c("Anna","Manuel","Laura","Jeanne") # Name of the Person
A = c(12,18,22,10) # Age in years
B = c(112,186,165,120) # Size in cm
Any ideas how to solve this? I don't mind a horizontal or a vertical solution.
Thank you very much!
Here's code that gets you a solid draft of what I think you want using barplot from base R. I'm just making one series negative for the plotting, then manually setting the labels in axis to reference the original (positive) values. You have to make a choice about how to scale the two series so the comparison is still informative. I did that here by dividing height in cm by 10, which produces a range similar to the range for years.
# plot the first series, but manually set the range of the y-axis to set up the
# plotting of the other series. Set axes = FALSE so you can get the y-axis
# with labels you want in a later step.
barplot(A, ylim = c(-25, 25), axes = FALSE)
# plot the second series, making whatever transformations you need as you go. Use
# add = TRUE to add it to the first plot; use names.arg to get X as labels; and
# repeat axes = FALSE so you don't get an axis here, either.
barplot(-B/10, add = TRUE, names.arg = X, axes = FALSE)
# add a line for the x-axis if you want one
abline(h = 0)
# now add a y-axis with labels that makes sense. I set lwd = 0 so you just
# get the labels, no line.
axis(2, lwd = 0, tick = FALSE, at = seq(-20,20,5),
labels = c(rev(seq(0,200,50)), seq(5,20,5)), las = 2)
# now add y-axis labels
mtext("age (years)", 2, line = 3, at = 12.5)
mtext("height (cm)", 2, line = 3, at = -12.5)
Result with par(mai = c(0.5, 1, 0.25, 0.25)):

How to get R plot to plot variable on heat.color scale

I'm plotting data in R. I'm running the following two commands:
plot(x = df$Latitude, df$Longitude, col = heat.colors(nrow(df)), type = "p")
plot(x = df$Latitude, df$Longitude, col = df$feature, type = "p")
The first line plots the points along a color gradient (points with higher values are red, points with lower values are yellow) and the second line plots data with color dictated by the int values given by features.
However, I want to combine both such that I'm plotting points with colors on a scale using the numeric values from feature. In some sense, I want to pass two arguments to col. How can I do this?
You can try:
# some data
set.seed(123)
x <- rnorm(100)
# Create some breaks and use colorRampPalette to transform the breaks into a color code
gr <- .bincode(x, seq(min(x), max(x), len=length(x)), include.lowest = T)
col <- colorRampPalette(c("red", "white", "blue"))(length(x))[gr]
# the plot:
plot(x, pch=16, col=col)
For a legend see solutions here or here

plotting nls fits with overlapping prediction intervals in a single figure

Say I some data, d, and I fit nls models to two subsets of the data.
x<- seq(0,4,0.1)
y1<- (x*2 / (0.2 + x))
y1<- y1+rnorm(length(y1),0,0.2)
y2<- (x*3 / (0.2 + x))
y2<- y2+rnorm(length(y2),0,0.4)
d<-data.frame(x,y1,y2)
m.y1<-nls(y1~v*x/(k+x),start=list(v=1.9,k=0.19),data=d)
m.y2<-nls(y2~v*x/(k+x),start=list(v=2.9,k=0.19),data=d)
I then want to plot the fitted model regression line over data, and shade the prediction interval. I can do this with the package investr and get nice plots for each subset individually:
require(investr)
plotFit(m.y1,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='light blue',shade=T)
plotFit(m.y2,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='pink',shade=T)
However, if I plot them together I have a problem. The shading of the second plot covers the points and shading of the first plot:
1: How can I make sure the points on the first plot end up on top of the shading of the second plot?
2: How can I make the region where the shaded prediction intervals overlap a new color (like purple, or any fusion of the two colors that are overlapping)?
Use adjustcolor to add transparency like this:
plotFit(m.y1, interval = "prediction", ylim = c(0,3.5), pch = 19,
col.pred = adjustcolor("lightblue", 0.5), shade = TRUE)
par(new = TRUE)
plotFit(m.y2, interval = "prediction", ylim = c(0,3.5), pch = 19,
col.pred = adjustcolor("light pink", 0.5), shade = TRUE)
Depending on what you want you can play around with the two transparency values (here both set to 0.5) and possibly make only one of them transparent.

Resources