Converting a plot to a square plot [duplicate] - r

I am plotting correlation coefficients (values = 0.0:1.0) for two isotopes measured in each individual from two populations. I would like to have a fixed aspect-ratio for my scatter-plot so that the x- and y-axis are exactly the same size no matter the graphics device. Suggestions?
This is my first plot in R, any comments on refinements to my code is appreciated? Finally, is it worth investing in learning the basic plotting techniques or should I jump right to ggplot2 or lattice?
My plot script:
## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")
opar <- par()
## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
"\u0028","\u2030","\u0029")),
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
"\u0028","\u2030","\u0029")))
points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25,
bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23),
col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
horiz=TRUE, bty = "n")
par(opar)

par(pty="s")
plot(...)
sets the plot type to be square, which will do the job (I think) in your case because your x and y ranges are the same. Fairly well hidden option documented in ?par.

Using asp=1 as a parameter to plot will get interpreted by the low-level plot.window call and should give you a unitary aspect ratio. There is the potential that a call using ylim and xlim could conflict with an aspect ratio scpecification and the asp should "prevail". That's a very impressive first R graph, by the away. And an excellent question construction. High marks.
The one jarring note was your use of the construction xlim=c(0:1.0). Since xlim expects a two element vector, I would have expected xlim=c(0,1). Fewer keystrokes and less subject to error in the future if you changed to a different set of limits, since the ":" operator would give you unexpected results if you tried that with "0:2.5".

Related

How to get the same ratio of plot and the picture in R? [duplicate]

I am plotting correlation coefficients (values = 0.0:1.0) for two isotopes measured in each individual from two populations. I would like to have a fixed aspect-ratio for my scatter-plot so that the x- and y-axis are exactly the same size no matter the graphics device. Suggestions?
This is my first plot in R, any comments on refinements to my code is appreciated? Finally, is it worth investing in learning the basic plotting techniques or should I jump right to ggplot2 or lattice?
My plot script:
## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")
opar <- par()
## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
"\u0028","\u2030","\u0029")),
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
"\u0028","\u2030","\u0029")))
points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25,
bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23),
col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
horiz=TRUE, bty = "n")
par(opar)
par(pty="s")
plot(...)
sets the plot type to be square, which will do the job (I think) in your case because your x and y ranges are the same. Fairly well hidden option documented in ?par.
Using asp=1 as a parameter to plot will get interpreted by the low-level plot.window call and should give you a unitary aspect ratio. There is the potential that a call using ylim and xlim could conflict with an aspect ratio scpecification and the asp should "prevail". That's a very impressive first R graph, by the away. And an excellent question construction. High marks.
The one jarring note was your use of the construction xlim=c(0:1.0). Since xlim expects a two element vector, I would have expected xlim=c(0,1). Fewer keystrokes and less subject to error in the future if you changed to a different set of limits, since the ":" operator would give you unexpected results if you tried that with "0:2.5".

How can I produce a "symmetrical" contour plot in R? [duplicate]

I am plotting correlation coefficients (values = 0.0:1.0) for two isotopes measured in each individual from two populations. I would like to have a fixed aspect-ratio for my scatter-plot so that the x- and y-axis are exactly the same size no matter the graphics device. Suggestions?
This is my first plot in R, any comments on refinements to my code is appreciated? Finally, is it worth investing in learning the basic plotting techniques or should I jump right to ggplot2 or lattice?
My plot script:
## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")
opar <- par()
## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
"\u0028","\u2030","\u0029")),
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
"\u0028","\u2030","\u0029")))
points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25,
bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23),
col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
horiz=TRUE, bty = "n")
par(opar)
par(pty="s")
plot(...)
sets the plot type to be square, which will do the job (I think) in your case because your x and y ranges are the same. Fairly well hidden option documented in ?par.
Using asp=1 as a parameter to plot will get interpreted by the low-level plot.window call and should give you a unitary aspect ratio. There is the potential that a call using ylim and xlim could conflict with an aspect ratio scpecification and the asp should "prevail". That's a very impressive first R graph, by the away. And an excellent question construction. High marks.
The one jarring note was your use of the construction xlim=c(0:1.0). Since xlim expects a two element vector, I would have expected xlim=c(0,1). Fewer keystrokes and less subject to error in the future if you changed to a different set of limits, since the ":" operator would give you unexpected results if you tried that with "0:2.5".

How to automate bar chart labels

I would like data labels to appear at the end of each bar, with one space between the label and the end of the bar. I would like the labels to be expressed as percents. Please note, the graph was constructed using raw numbers, not percentage values. I would like the solution to this problem to be in base R. Answers to similar prior questions have not worked for me.
table(cont$cont9) # 1=0, 2=0,3=2, 4=2, 5=21
#1=0%, 2=0%,3=8%, 4=8%, 5=84%
par(mar=c(0, 5, 0, 2.1))
H <- c(0, 0, 2, 2, 21) # Create the data for the chart, cont3.#
M <- c("Very Low", "Low", "Medium", "High", "Very High")
barplot(H, col =c("slategray1", "slategray1","slategray1",
"slategray1", "steelblue3"),
horiz = TRUE,
family="Arial", border = NA, names.arg = M,
xlim = range(0,100), ylim = range(0, 0.08),
axes = FALSE, width = 0.01, las=1, xaxt='n')
I would like percentage data labels at the end of each bar. With the solution in base R.
To do what the question asks for, keep the return value of barplot and use it as the y coordinates of the labels. The return value is:
A numeric vector (or matrix, when beside = TRUE), say mp, giving the
coordinates of all the bar midpoints drawn, useful for adding to the
graph.
The x coordinates are the plotted values, with the position adjusted, pos = 4.
In the code that follows, I start by also keeping the return value of the call to par. This is a general purpose good habit. When done plotting, reset the defaults as they were.
op <- par(mar=c(0, 5, 0, 2.1))
bp <- barplot(H, col =c("slategray1", "slategray1","slategray1",
"slategray1", "steelblue3"),
horiz = TRUE,
family = "Arial", border = NA, names.arg = M,
xlim = range(0,100), ylim = range(0, 0.08),
axes = FALSE, width = 0.01, las=1, xaxt='n')
text(H, bp, labels = H/sum(H), pos = 4)
par(op)
Note:
Though I have not change it, I find
xlim = c(0, 30), ylim = c(0, 0.08),
more natural.
It uses c, not range.
The xlim values agree with range(H).

Legend disappaers when plotting in R

I have plotted five graphs and a legend. The graphs work just fine, however the legens disappears without an error.
My preview in RStudio looks like this
When I zoom in, the area where the legend should be is blank.
I use the following code:
opar <- par (no.readonly = TRUE)
par (mfrow = c(3, 2))
library(deSolve)
# Plot A
LotVmod <- function (Time, State, Pars) {
with(as.list(c(State, Pars)), {
dx = (b*x) - (b*x*x/K) - (y*(x^k/(x^k+C^k)*(l*x/(1+l*h*x))))
dy = (y*e*(x^k/(x^k+C^k)*(l*x/(1+l*h*x)))) - (m*y)
return(list(c(dx, dy)))
})
}
Pars <- c(b = 1.080, e = 2.200, K = 130.000, k = 20.000, l = 2.000,
h = 0.030, C = 2.900, m = 0.050)
State <- c(x = 0.25, y = 2.75)
Time <- seq(1, 9, by = 1)
out <- as.data.frame(ode(func = LotVmod, y = State, parms = Pars, times = Time))
matplot(out[,-1], type = "l", xlim = c(1, 9), ylim = c(0, 45),
xlab = "time",
ylab = "population",
main = "Compartment A")
mtext ( "Coefficient of Variance 4.96", cex = 0.8 )
x <- c(# Validation data)
y <- c(# Validation data)
lines (Time, x, type="l", lty=1, lwd=2.5, col="black")
lines (Time, y, type="l", lty=1, lwd=2.5, col="red")
# Legend
plot.new()
legend("center", c(expression (italic ("F. occidentalis")*" observed"),
expression (italic ("M. pygmaeus")*" observed"),
expression (italic ("F. occidentalis")*" simulated"),
expression (italic ("M. pygmaeus")*" simulated")),
lty = c(1, 1, 1, 2),
col = c(1, 2, 1, 2),
lwd = c(2.5, 2.5, 1, 1),
box.lwd = 0, bty = "n")
# Plot C to F = same as A
par(opar)
My output doesn't give an error. I have used the exact same code before without any trouble, thus I restarted R, removed all objects, cleared all plots and restarted both RStudio and my computer.
Try to add xpd=TRUE in your legend statement. I.e.
legend("center", c(expression (italic ("F. occidentalis")*" observed"),
expression (italic ("M. pygmaeus")*" observed"),
expression (italic ("F. occidentalis")*" simulated"),
expression (italic ("M. pygmaeus")*" simulated")),
lty = c(1, 1, 1, 2),
col = c(1, 2, 1, 2),
lwd = c(2.5, 2.5, 1, 1),
box.lwd = 0, bty = "n", xpd=TRUE)
By default, the legend is cut off by the plotting region. This xpd parameter enables plotting outside the plot region. See e.g. ?par for more on xpd.
This is due to how the plot canvas is set up and how rescaling that device works. The way you do it, you add the legend in the plotting region of the top right plot. The plotting region is however not the complete device, but only the part inside the space formed by the axes. If you rescale, that plotting region will be rescaled as well. The margins around the plotting region don't change size though, so zooming in makes your plotting region so small that it doesn't fit the legend any longer. It is hidden by the margins around the plotting region.
For that reason AEBilgrau is very right you need to add xpd = TRUE. This allows the legend to extend outside of the plotting region, so it doesn't disappear behind the margins when resizing the plotting device.

How to define fixed aspect-ratio for (base R) scatter-plot

I am plotting correlation coefficients (values = 0.0:1.0) for two isotopes measured in each individual from two populations. I would like to have a fixed aspect-ratio for my scatter-plot so that the x- and y-axis are exactly the same size no matter the graphics device. Suggestions?
This is my first plot in R, any comments on refinements to my code is appreciated? Finally, is it worth investing in learning the basic plotting techniques or should I jump right to ggplot2 or lattice?
My plot script:
## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")
opar <- par()
## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
"\u0028","\u2030","\u0029")),
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
"\u0028","\u2030","\u0029")))
points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25,
bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23),
col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
horiz=TRUE, bty = "n")
par(opar)
par(pty="s")
plot(...)
sets the plot type to be square, which will do the job (I think) in your case because your x and y ranges are the same. Fairly well hidden option documented in ?par.
Using asp=1 as a parameter to plot will get interpreted by the low-level plot.window call and should give you a unitary aspect ratio. There is the potential that a call using ylim and xlim could conflict with an aspect ratio scpecification and the asp should "prevail". That's a very impressive first R graph, by the away. And an excellent question construction. High marks.
The one jarring note was your use of the construction xlim=c(0:1.0). Since xlim expects a two element vector, I would have expected xlim=c(0,1). Fewer keystrokes and less subject to error in the future if you changed to a different set of limits, since the ":" operator would give you unexpected results if you tried that with "0:2.5".

Resources