1

It looks like something simple I am missing but have no idea how to deal with this.

So I used a layout() function and I managed to get the layout as I wanted as below picture. Iris data was used in my coding.

enter image description here

Problem is, it does not show me the x label and y label on the output when I use plot() functions after this. And xaxis and yaxis for plot() looks overlapping. I am not sure how to deal with this problem.

There was no problem for x and y labelling before introducing plot.new() and par() to set up the main name of my diagram. (i.e. before I use the code from plot.new() to title(), xlab and ylab were shown)

I used 6 different plots in my original code, including, the plot.new() for title(), but I omitted the rest of them for convenience

enter image description here

Here is my code below,

x <- iris$Sepal.Length
y <- iris$Species


x_min <- min(iris$Sepal.Length)
x_max <- max(iris$Sepal.Length)
y_min <- min(iris$Sepal.Width)
y_max <- max(iris$Sepal.Width)


layout(matrix(c(1,1,1,1,1,1,
            2,2,3,3,4,4,
            5,5,5,6,6,6), nc=6, byrow = TRUE), heights=c(lcm(1),1,1,1,1))
layout.show(6)
par("mar"=c(1,1,1,1,1,1))
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
text(x=0.5,y=0.5,"scatter and density plots for Sepal and Length and Sepal Width" ,font=2, cex=1.5)
plot(...)
neilfws
  • 32,751
  • 5
  • 50
  • 63
G.Lee159
  • 39
  • 5
  • (1) `par(mar=...)` takes a vectof of 4, not 6. (2) You explicitly set the margins to "1" which, in the case of base R plots, typically means the default axis numbers are hidden. (3) If after changing `mar=` you still have questions, it would really help to have some reproducible code (ref: https://stackoverflow.com/questions/5963269/). – r2evans Sep 11 '18 at 02:33
  • If par() function hides the axis, what is the alternative way to use this layout? Because if I leave out par() function, the error says "figure margins are too large" – G.Lee159 Sep 11 '18 at 03:43
  • Do you get the "too large" error on all plots or just the top (`text(...)`) plot? That's the only one that (from the image) I would expect that behavior. Realize that `par(mar=)` is per "pane", so-to-speak, so your plots 2-6 can have different margins. – r2evans Sep 11 '18 at 03:53

1 Answers1

0

You can use the xlab and ylab arguments in title. However, the way you have constructed the plot means that when you reset par at the end, these are drawn off the page due ti their position relative to your custom axis. If you simply leave par alone, you get:

den1 = density(CDE1$V1)
den2 = density(CDE1$V2)

col1 = hsv(h = 0.65, s = 0.6, v = 0.8, alpha = 0.5)
col2 = hsv(h = 0.85, s = 0.6, v = 0.8, alpha = 0.5)

plot.new()
plot.window(xlim = c(25,65), ylim = c(0, 0.14))
axis(side = 1, pos = 0, at = seq(from = 25, to = 65, by = 5), col = "gray20", 
     lwd.ticks = 0.25, cex.axis = 1, col.axis = "gray20", lwd = 1.5)
axis(side = 2, pos = 25, at = seq(from = 0, to = 0.14, by = 0.02), 
     col = "gray20", las = 2, lwd.ticks = 0.5, cex.axis = 1, 
     col.axis = "gray20", lwd = 1.5)
polygon(den1$x, den1$y, col = col1, border ="black",lwd = 2)
polygon(den2$x, den2$y, col = col2, border ="black",lwd = 2)
text(52, 0.10, labels ="CDET", col =col1, cex = 1.25,font=2)
text(35, 0.03, labels ="SDFT", col =col2, cex = 1.25,font=2)

title(main = "Gestational Day 100/283", 
      xlab = "Fibril Diameter (nm)",
      ylab = "density")

enter image description here

Of course, you could get a similar plot with less code and much easier adjustments using ggplot:

library(ggplot2)

ggplot(tidyr::pivot_longer(CDE1, 1:2), aes(value, fill = name)) +
  geom_density() +
  scale_fill_manual(values = c(col1, col2), labels = c("CDET", "SDFT")) +
  scale_x_continuous(breaks = seq(25, 65, 5), limits = c(25, 65)) +
  scale_y_continuous(breaks = seq(0, 0.14, 0.02), limits = c(0, 0.14)) +
  theme_classic(base_size = 16) +
  labs(title = "Gestational Day 100/283", x = "Fibril Diameter (nm)", 
       fill = NULL) +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here

Data used

Obviously, we don't have your data, so I had to create a reproducible approximation:

set.seed(123)
CDE1 <- data.frame(V1 = rnorm(20, 47.5, 4), V2 = rnorm(20, 44, 5))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87