Add Min/Max Values as Horizontal Lines in R Plot - r

I have made a line chart in R and now I want to add a two more lines of min and max values that are already exist in seperate column in same csv file. Here is my code that i used
plot(MedTemp$Loggers.distance, MedTemp$Temperature..C...2017., xaxt = "n", xlab = "",ylab = "", main = "Median Temperature 2017", col.main = "darkred", col = "blue", pch = 19, ylim = c(-5,25), fill = "blue", type = "o")
axis(1, at = c(0, 4698.91, 5424.74, 8731.54, 10103.81, 14361.29 ), cex.axis = 0.55, las = 2)
lines(MedTemp$Loggers.distance[order(MedTemp$Loggers.distance)], MedTemp$Temperature..C...2017.[order(MedTemp$Loggers.distance)], xlim = range(MedTemp$Loggers.distance), ylim = range(MedTemp$Temperature..C...2017.), pch = 16, col = "firebrick", lwd = 1.5)
text(MedTemp$Temperature..C...2017.~ MedTemp$Loggers.distance, labels = c(" ","Orchar\nHill","Roland\nRoad","Gilligan\nRoad","Wetaskiwin"," "), cex = 0.5, font = 2, pos = 1, srt = 0)
text(MedTemp$Temperature..C...2017.~ MedTemp$Loggers.distance, labels = c("Rice\nRoad"," "," "," "," "," "), cex = 0.5, font = 2, pos = 1, srt = 0)
text(MedTemp$Temperature..C...2017.~ MedTemp$Loggers.distance, labels = c(" "," "," "," "," ","Decew\nRoad"), cex = 0.5, font = 2, pos = 1, srt = 0)
mtext(1, text = "Thermal Loggers Chainage Distance(m)", line = 3, cex = 0.9)
mtext(2, text = "Temperature (C)", line = 3, cex = 0.9)
Here is my data:
Here is my chart:
Any help would be appreciated.

The command you're looking for is abline. something along the lines of
abline(h = c(min_value, max_value))
You can add similar instructions for color, linetype, linewidth to the function as did with other plot functions.

Related

base R: how to keep axis box in each plot of a 2 x 2 figure

I am making a 4-panel figure where each column represents a different variable and each row represents a different location. When I add the first two plots (i.e., the top row) there is a box surrounding each plot. However, when I add the bottom two figures (i.e., the bottom row) the box around the top row plots disappears. Any idea how to make sure there is a box around each plot?
Example
Make the two figures of the top row first
library(biwavelet)
par(mfrow = c(2,2), xpd=FALSE)
par(mar = c(1,1,1.1,1.1) + 0.1,
oma = c(3,3,0.5,0.5) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = ""); par(xpd = NA)
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1); par(xpd = NA)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')
Now add bottom two plots and watch the black box around the top two plots disappear
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5,
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5); par(xpd = NA)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)
I have tried using box(lty = "solid", col = 'black'), which has not worked. I have also tried using fg = 'black' and bty = "o" inside of plot(), which has not worked.
How can I keep the black box around the top two plots?
With par(xpd=NA) before your text() commands you are doing the xpd=NA setting globally, hence influencing all subsequent plotting, and your bottom row graphs are somehow overdrawing the top row ones. Just move the xpd=NA inside your text() functions (e.g. text('Location 1', ..., xpd=NA)) and everything should work as expected:
library(biwavelet)
par(mfrow = c(2,2), xpd=F)
par(mar = c(1,1,1.1,1.1) + 0.1,
oma = c(3,3,0.5,0.5) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5,
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)

How to remove the zero labels in Histogram plot in R?

Any tips to remove the zero labels in between the histogram bars?
hist(links$Survey_Duration, breaks = seq(0,50,5), main = "Survey Duration",
labels = TRUE, border = "black",
xlab = "Survey", ylim = c(0, 15), col = "gray", las = 1, xaxt='n')
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright", c("Mean", "Median"), col = c("royalblue","red"),
lwd = c(1.5,1.5))
How about this?
# modify data so there's zero in one of the bins
mtcars$mpg <- ifelse(mtcars$mpg >= 25 & mtcars$mpg <= 30, NA, mtcars$mpg)
# save plot parameters
h <- hist(mtcars$mpg, plot = FALSE)
# produce plot
plot(h, ylim = c(0, 14))
# add labels manually, recoding zeros to nothing
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
A slightly different answer using the labeling in hist instead of adding text afterwards.
You do not provide your data, so I will use some data that is handy to illustrate.
The labels argument can specify the individual labels
H1 = hist(iris$Sepal.Length, breaks = 3:8, plot=FALSE)
BarLabels = H1$counts
BarLabels[BarLabels == 0] = ""
hist(iris$Sepal.Length, breaks = 3:8, labels = BarLabels)
Thanks #Daniel Anderson, it Ok now (Thumbs Up)
links$Survey_Duration <- ifelse(links$Survey_Duration > 15 &
links$Survey_Duration <= 25,
NA,
links$Survey_Duration)
h <- hist(links$Survey_Duration, breaks = seq(0,50,5), plot = FALSE)
plot(h, ylim = c(0, 14), main = "Survey Duration", xlab = "Time", col = "gray", las = 1)
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright",
c("Mean", "Median"),
col = c("royalblue","red"),
lwd = c(1.5,1.5))

R Shiny plot won't render

Hi can anyone see what is the problem with the code below?
output$zscore_chart <- renderPlot({
xvals <- 2:186
req(input$countrySelectZScore)
idx_country = which(input$countrySelectZScore == esiCountries)
max_comp_z <- max(esiData_DF[idx_country, 2:186], na.rm = TRUE)
overall_max_z <- max(max_comp_z, na.rm = TRUE)
foo = ts(esiData_DF[idx_country, 2:186], frequency = 12, start = 2001)
dates = seq(as.Date("2001-01-01"), by = "month", along = foo)
plot(x = 2:186, y = esiData_DF[idx_country, 2:186], type = "l",
xlab = "", ylab = "", col = "grey20", ylim = c(-2, overall_max_z),
lwd=3,las=2)
mtext("Quarterly percentage changes", side = 3, adj = 0, line = 0.1,
cex = 1, font = 0.5)
axis(1, at = xvals, label = dates, cex.axis = 1, las = 2)
mtext("Economic Sentiment Indicators", side = 3, adj = 0,
line = 1.2, cex = 2, font = 2)
legend(
"bottom",
lty = c(1,1),
lwd = c(3,3),
col = c("grey20", "green2"),
legend = c("Economic Sentiment Indicator", "GDP growth"),
bty = "n",
xjust = 0.5,
yjust = 0.5,
horiz = TRUE
)
}, height = 525)
esiData_DF is the DF used to index and plot the correct data. The dataframe has the country names down the left hand side with the dates, monthly across the top. I need the plot to render but it wont when I run the app. Any ideas?
The data continues to the right, up to May 2017 monthly.

R - Plot with two y axis, bars and points not aligned

For a customer I'm trying to do a combined barplot and lineplot (with points) with two y axis.
Problem: My bars and points are not aligned.
Background: We have several machines and are measuring their number of on/of switches and the amount of time that each machine is running. We want both information together in one plot to save space, because we have several machines.
The data is aggregated by day or hour. Here's some sample data:
date <- seq(as.Date("2016-10-01"), as.Date("2016-10-10"), "day")
counts <- c(390, 377, 444, NA, NA, NA, NA, 162, 166, 145)
runtime <- c(56.8, 59.4, 51.0, NA, NA, NA, NA, 38.5, 40.9, 43.4)
df <- data.frame(date = date, counts = counts, runtime = runtime)
Here's what I tried so far:
par(mar = c(3,4,4,4) + 0.3)
barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]",
ylim = c(0,100), font.lab = 2)
par(new = TRUE)
ymax <- max(df$counts, na.rm = TRUE) * 1.05
plot(df$date, df$counts, type = "n", xlab = "", ylab = "", yaxt = "n",
main = "Machine 1", ylim = c(0, ymax))
abline(v = date, col = "red", lwd = 2.5)
lines(df$date, df$counts, col = "blue", lwd = 2)
points(df$date, df$counts, pch = 19, cex = 1.5)
axis(4)
mtext("Number of switching operations", side = 4, line = 3, font = 2)
I found some inspiration for two axis here: http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/
What can I do to get bars with their middle aligned with the points of the lineplot?
The problem you are running into is the call to the second plot function after the barplot. This is shifting/resizing the plotting canvas which is causing the shift in the subsequent points.
Here is a quick work-around that just rescales the points and lines onto the barplot. It saves the barplot as an object, which stores x-axis locations for the mid-points of the bars. Then, when you plot the abline, lines and points using 'bp' as the x-axis variable, they will be correctly aligned.
ymax <- max(df$counts, na.rm = TRUE) * 1.05
par(mar=c(4.1,5.1,2.1,5.1))
bp <- barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]",
ylim = c(0,100), font.lab = 2, xlim=c(0.2,12), )
barplot(df$runtime, col = "palegreen2", ylab = "runtime in [%]", border="NA",
ylim = c(0,100), font.lab = 2)
abline(v = bp, col = "red", lwd = 2.5)
lines(bp, df$counts/ymax*100, col = "blue", lwd = 2)
points(bp, df$counts/ymax*100, pch = 19, cex = 1.5)
axis(4,at=c(0,20,40,60,80,100), labels=c("0","100","200","300","400","500"))
mtext("Number of switching operations", side = 4, line = 3, font = 2)
axis(1, at=bp, labels=df$date)
#emilliman: Thank you for your patience and input! Your plot is not completely correct, because the scaling of the second y-axis does not fit the points' values, but your idea helped me to find a solution!
Here's my new code:
library(plyr)
ymax <- max(df$counts, na.rm = TRUE)
ymax_up <- round_any(ymax, 100, f = ceiling)
ylab <- ymax_up/5 * c(0:5)
par(mar = c(3,4,4,4) + 0.3)
bp <- barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]",
ylim = c(0,100), font.lab = 2, main = "Machine 1")
abline(v = bp, col = "red", lwd = 2.5)
lines(bp, 100/ymax_up * df$counts, col = "blue", lwd = 2)
points(bp, 100/ymax_up * df$counts, pch = 19, cex = 1.5)
axis(4,at=c(0,20,40,60,80,100), labels= as.character(ylab))
mtext("Number of switching operations", side = 4, line = 3, font = 2)
xlab <- as.character(df$date, format = "%b %d")
axis(1, at=bp, labels = xlab)
abline(h = c(0,100))
(http://i.imgur.com/9YtYGSD.png)
Maybe this is helpful for others who run into this problem.

How can I change font type in r-plot

In the following plot I am trying to have all text in Arial font but axis (numbering in both x and y axis are in Times New Roman, and legend and mtext is in Arial. Please can anyone suggest me how can I fix it.
ace_Obs= c(1.80,0.99,4.82,5.03,2.04,1.96,5.89,7.73,5.53,2.03,4.36,2.20,
1.83,12.98,9.50,2.33,10.39, 10.08,6.80,6.28,3.84,10.08,12.96,
14.29,4.49,4.23,8.33,3.00,9.52,8.33,3.00,9.52)
# plot
z = c(" "," "," "," "," "," "," "," "," "," "," ")
x1 = seq(0.5, 32.5, 1)
y1 = rep(0,33)
x <- seq(1, 32, 1)
the_plot <- function()
{
plot(x1, y1, ylim = c(0,25),xlab = " ",ylab = " ",type = "l", xaxt='n',
yaxt='n', main = " ")
points(x = x, y = ace_Obs, col = "black", cex = 1.5)
legend(x = -1.3, y = 25, "Nov", cex = 2.2, text.font=2, box.lwd = 1)
x2 = seq(1982,2013,3)
mtext(expression("ACE" ~ (m^{2} ~ s^{-1} ~ "")),
side=2,line=2.7,
font = 6, cex = 2)
axis(1,at=seq(1, 32, by=3), labels=x2, las=2, font = 6)
axis(2,at=seq(0,25, 5), labels=c("0","5","10","15","20","25"),
las=2, font = 6)
}
png("TEST.png",width = 6,height = 4,units = "in",res = 1000, pointsize = 6)
par(mfrow = c(1,1),
oma = c(5,5,0,0) + 0.1,
mar = c(0.5,0.5,1,1) + 0.1, xaxs = "i", yaxs = "i", cex.axis = 2,
cex.lab = 2,cex.main = 2)
the_plot()
dev.off()
It might be helpful to someone, for the problem asked above (for png out put) use type = "cairo" as follows:
png("GFDL_ace_Forecast11.png",width = 6,height = 8,units = "in",res = 1000, pointsize = 7, type = "cairo")

Resources