gain plot margins in R - r

I'm trying to fit a second ylab on the right of my plot but I'm not able to gain the extra space needed. Drawing the scale values inside the graph could be a solution if this was not possible.
My code (simulated data but the problem with the right margin is there):
dev.new( width= 16, height= 7 )
par( cex= 0.9 )
plot( 1:100, type="n", xlab = "", ylab = "", axes = F, las = 2, cex = 0.7 )
axis( 4, at = c ( 0, 30, 60, 90, 120, 150, 180 ), labels = c( "0", "30", "60", "90", "120", "150", "180" ), las = 0, cex.axis = .8,col.lab = "gray80",
tck = 0.01 )
rect( 153, -5.5, 169, 185, col = "gray80", border = NA)
rect( 246, -5.5, 272, 185, col = "gray80", border = NA)
lines( 1:200, col = "gray20" )
#text("Moon phase (º)", 330, 90, cex = .9 )
mtext(side = 4, text = "Moon phase (º)", line = 2, cex = .9 )
mtext(side = 1, text = "Dates", line = 4, cex = .9 )
par( new = T )
plot( 1:220, type = "n", lwd= 1.2, xlab= "", ylab= "Photoperiod (h)", axes = F, cex = .8, col.lab = "red" )
axis( side= 1, at = c ( 1, 20, 51, 82, 112, 143, 173, 204, 235, 264, 295, 324 ), labels = c ( "12 Jun'07", "1 Jul'07", "1 Aug'07", "1 Sep'07", "1 Oct'07", "1 Nov'07", "1 Dec'07", "1 Jan'08", "1 Feb'08", "1 Mar'08", "1 Apr'08", "30 Apr'08" ), las = 2, cex.axis = .75 )
axis ( side = 2, at = c( 12, 13, 14, 15, 16 ), labels = c( "12", "13", "14", "15", "16" ), cex.axis = .8, las = 2)
box()
Thank you for your help,
Santi

See argument mar of par. This set the margins of your plot area (bottom, left, top, right; see ?par for details).
dev.new( width= 16, height= 7 )
## set outer margins
par( mar=c(5, 4, 4, 4), cex= 0.9 )
plot( 1:100, type="n", xlab = "", ylab = "", axes = F, las = 2, cex = 0.7 )
axis( 4, at = c ( 0, 30, 60, 90, 120, 150, 180 ), labels = c( "0", "30", "60", "90", "120", "150", "180" ), las = 0, cex.axis = .8,col.lab = "gray80",
tck = 0.01 )
rect( 153, -5.5, 169, 185, col = "gray80", border = NA)
rect( 246, -5.5, 272, 185, col = "gray80", border = NA)
lines( 1:200, col = "gray20" )
#text("Moon phase (º)", 330, 90, cex = .9 )
mtext(side = 4, text = "Moon phase (º)", line = 2, cex = .9 )
mtext(side = 1, text = "Dates", line = 4, cex = .9 )
par( new = T )
plot( 1:220, type = "n", lwd= 1.2, xlab= "", ylab= "Photoperiod (h)", axes = F, cex = .8, col.lab = "red" )
axis( side= 1, at = c ( 1, 20, 51, 82, 112, 143, 173, 204, 235, 264, 295, 324 ), labels = c ( "12 Jun'07", "1 Jul'07", "1 Aug'07", "1 Sep'07", "1 Oct'07", "1 Nov'07", "1 Dec'07", "1 Jan'08", "1 Feb'08", "1 Mar'08", "1 Apr'08", "30 Apr'08" ), las = 2, cex.axis = .75 )
axis ( side = 2, at = c( 12, 13, 14, 15, 16 ), labels = c( "12", "13", "14", "15", "16" ), cex.axis = .8, las = 2)
box()

In your code use on line #2:
par( cex= 0.9,mar=c(6,4,6,4) )

Related

change second y axis color in base R

Change secondary line axis color changes send color for ggplot, but I chose to go with base R, and would like to be able to select the second y axis color.
I have the following data:
df = structure(list(A = c("Q4-17", "Q1-18", "Q2-18", "Q3-18", "Q4-18",
"Q1-19", "Q2-19", "Q3-19", "Q4-19", "Q1-20", "Q2-20", "Q3-20",
"Q4-20", "Q1-21", "Q2-21", "Q3-21", "Q4-21", "Q1-22", "Q2-22",
"Q3-22"), B = c(69.45, 71.1, 74.94, 73.87, 93.61, 91.83,
95.38, 109.8, 133.75, 125.26, 118.22, 145.65, 144.9757185, 155.3464032,
184.367033, 179.8121721, 187.235487, 189.1684376, 184.3864519,
161.5300056), C = c(70.73, 71.73, 74.33, 73.27,
95.94, 94.38, 95.38, 109.8, 115.32, 116.92, 115.9, 113.87, 106.108147,
96.84273563, 111.5150869, 110.1228567, 110.7448835, 194.9684376,
187.7241152, 167.7665553), D = c(260.3, 216.02, 203.72,
203.52, 300.96, 320.77, 330.5, 413.52, 436.7, 474.96, 463.6,
501.87, 493.8865461, 497.1760767, 514.9903459, 503.7601267, 510.8362938,
614.9915546, 603.5761107, 593.660831), E = c(NA,
NA, NA, NA, NA, NA, NA, NA, 39.237, 35.621, 32.964, NA, 152.137,
140.743023, 167.809, 170.877, 117.517, 102.691723, 88.8, 76.2445528
)), class = "data.frame", row.names = c(NA, -20L))
df = df %>%
rowwise() %>%
mutate(sums = sum(D,E, na.rm = TRUE))
df = df[8:nrow(df),]
and this to generate my plot
x <- seq(1,nrow(df),1)
y1 <- df$B
y2 <- df$D
par(mar = c(5, 4, 4, 4) + 0.3)
plot(x, y1, col = "#000000",
type = "l",
main = "title",
ylim = c(0, max(df[,2:3])),
ylab = "Y1",
xlab = "",
xaxt = "n")
axis(1,
at = seq(from = 13, by = -4, length.out = 4),
labels = df$A[seq(from = 13, by = -4, length.out = 4)])
lines(x, df$C, lty = "dashed", col = "#adadad", lwd = 2)
par(new = TRUE)
plot(x, df$sums, col = "#ffa500",
axes = FALSE, xlab = "", ylab = "", type = "l")
axis(side = 4, at = pretty(range(y2)),
ylim = c(0,max(df[,3:5], na.rm = TRUE)),
col = "#00aa00") # Add colour selection of 2nd axis
par(new = TRUE)
plot(x, df$D , col = "#0000ff",
axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 1)
mtext("y2", side = 4, line = 3)
but this does not colour my complete second y axis, nor labels, nor title
does any one have any suggestions to be able to set entire y2 axis to be #00AA00 - ticks, labels, and title?

add trace in Plotly won't plot the right colour

I am trying to make a contour plot and draw a line on top of it (which I can do). I then overlaid two other lines using add_trace. For some reason the two lines I add using add_trace comes out orange instead of grey even though I specify grey (line = list(color = 'grey', )
I don't know how to add my data, it is very big. Is there any obvious reason as to why the colour changes to orange? If I change the width or the dash, it works. it just doesn't want to use the grey colour!
Thank you
(plot <- plot_ly(df, x = ~A, y = ~B, z = ~Difference, zauto = FALSE, zmin = -250, zmax = 250,
type="contour",
colorbar = list(title = "", titleside='right',
tickvals=c(-250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250), len = 1),
colorscale = "RdBu",
contours = list(start = 0, end = 0, coloring='heatmap', coloring='lines'),
line = list(color = 'black', width = 2)) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'dash'), contours = list(start = 0, end = 0, coloring='lines')) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'solid'), contours = list(start = 0, end = 0, coloring='lines')) %>%
layout(margin = list(l = 50, r = 70, b = 50, t = 50, pad = 4),
title = "", xaxis = x, yaxis = list(title = ""), font=t))
I guess the problem in your above code is, that you aren't specifiying the trace type. Accordingly plot_ly assumes that you are adding two more contour traces. Those traces are inheriting the colorscale you defined.
To avoid this you need to specify type = "scatter", mode = "lines", inherit = FALSE.
I made a simple example based on this.
library(plotly)
fig <- plot_ly(
x = c(-9, -6, -5, -3, -1),
y = c(0, 1, 4, 5, 7),
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5),
type = "contour", colorbar = list(title = "", titleside='right'),
colorscale = "RdBu",
line = list(color = 'black', width = 2)) %>%
add_trace(x = -1:-7, y = 1:7, type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE)
fig

How to change the font size of the tick labels in contour plot colorbar in R?

I am using plot_ly to plot a contour plot. The code is as follows:
X = c(1, 2, 3, 4, 5, 6)
Y = c(seq(from = 1, to = 17, by = 1))
Z = matrix(runif(17*6), ncol=17)
m <- list(colorbar=list(title = "hi"))
plot_ly(
x = X,
y = Y,
z = t(Z),
type = "contour",
opacity = 100,
fillcolor = "FALSE",
line = list(width = 1.5),
contours = list(showlabels = TRUE,
labelfont = list(size = fs, color = 'white')),
color = I('black')
) %>%
layout(xaxis = list(title = "X", titlefont = list(size = fs),
ticktext = c("9:00", "9:30", "10:00", "10:30", "11:00", "11:30"),
tickvals = c(1, 2, 3, 4, 5, 6),
tickmode = "array",
tickfont = list(size=fs)),
yaxis = list(title = "Y", titlefont = list(size = fs),
ticktext = c("0", "30", "60", "90", "120", "150", "180", "210", "240",
"270", "300", "330", "360", "390", "420", "450", "480"),
tickvals = c(1:17),
tickmode = "array",
tickfont = list(size=fs)))
The output I get is:
How do I change the font size of the colorbar tick labels (encircled in red)?
Just include colorbar=list(tickfont=list(size=25)) in your chart setup.
I had some problems with your sample code, so this will have to do:
Plot:
Code:
library(plotly)
library(tidyr)
library(dplyr)
p <- plot_ly(
x = c(-9, -6, -5, -3, -1),
y = c(0, 1, 4, 5, 7),
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5),
type = "contour",
colorbar=list(tickfont=list(size=25, color='red')),
contours = list(
start = 0,
end = 8,
size = 2)
)
p

r hyphen in label of x axis

The x axis label has hyphen. This is not what I wrote in my code:
temp = table(res$Methode, res$Bewertung)
tempf = ftable(res$Methode, res$Bewertung)
barplot(tempf , xlab="Bewertung", ylab="absolute Häufigkeit", cex.lab = 1.4, beside=TRUE, ylim = c(0,600), xlim=c(11.75,1.7), col = colour2)
legend("top", legend=c("ohne LOD", "mit LOD"), horiz = TRUE, inset = c(0, 0), fill = rev(colour2), cex=1.4)
axis(1, at=c(11, 8, 5, 2),labels=c("sehr relevant", "mit­tel­mä­ßig relevant", "wenig relevant", "nicht relevant"), cex.axis=1.4)
How can I solve this?

Label on X-axis for each group of points

I'm am creating a script which allows me to get data from a database, and visualises it in a graph.
As you can see, there are 8 groups of data, indicated on the X-axis. Each group always contains 90 values.
Right now, the place of the labels is hard-coded like this:
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
It works perfectly fine, but I was wondering if there is a way to do this more dynamically, by just telling R to assign a label to each set of 90 values.
Example:
# Generate data ###################################################################################
x <- vector()
y <- vector()
# y[length(y)+1] <- sample(10:12, 1, replace = TRUE)
oligo_1 <- runif(62, 10.5, 11.5)
oligo_2 <- runif(62, 14, 15)
oligo_3 <- runif(62, 17, 18)
oligo_4a <- runif(64, 20.5, 22)
oligo_4b <- runif(64, 20.5, 22)
oligo_5 <- runif(62, 24, 25)
oligo_6 <- runif(62, 27, 28)
oligo_7 <- runif(62, 30, 31)
y <- c(oligo_1, oligo_2, oligo_3, oligo_4a, oligo_4b, oligo_5, oligo_6, oligo_7)
x <- c(1:500)
example <- data.frame(x, y)
# Define variables ################################################################################
xmin <- 10
xmax <- 36
# Generate graph ###################################################################################
png(filename = "graph.png", width = 1500, height = 833)
plot(x = example[,2], type="l",
xlim = c(0, nrow(example)), ylim = c(xmin, xmax),
xaxt="n", yaxt="n",
xlab = "", ylab = "")
rect(xleft = par("usr")[1], ybottom = 9.8, xright = par("usr")[2], ytop = 12.2, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 13.2, xright = par("usr")[2], ytop = 15.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 16.5, xright = par("usr")[2], ytop = 18.9, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 19.9, xright = par("usr")[2], ytop = 22.3, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 23.3, xright = par("usr")[2], ytop = 25.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 26.5, xright = par("usr")[2], ytop = 28.7, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 29.7, xright = par("usr")[2], ytop = 32.1, border = "lightgrey", col = "lightgrey")
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
axis(2, at = c(11, 14.35, 17.7, 21.1, 24.4, 27.6, 30.9), las = 1)
lines(x = example[,2])
box()
mtext(paste("QC-check for", "TEST"), side = "3", line = 1, cex = 2, font = 2)
mtext("Samples" , side = "1", line = 3, cex = 1, font = 1)
legend(x = par("usr")[1]+10, y = par("usr")[4]-1, legend = c("Cq", "Ccq"), cex=1.5, lwd = 2, col = c("black","red"))
dev.off()
Why not:
axis(1, at=31.25+(0:7)*62.5, labels=paste("ss oligo",1:8) )
Alright, I made it much more difficult than it was. So in combo with the answer from #user449060, I changed the code to:
count <- nrow(data)/8
axis(1, at = count/2+(0:7)*count, labels = paste("ss oligo",c(1:4, 4, 5:7)))
which makes it a lot more dynamic!

Resources