0

I am going to sum up some questions made before in this forum (1, 2), but in this case with my real datasets and codes, all together. I am stuck and need help to know to code it properly.

Let's start with the datasets. Download and load them from here:

perio <- read.csv(url("http://vivaelsoftwarelibre.com/wp-content/uploads/2018/05/perio.csv"), header = TRUE, sep = ",", quote = "\"", dec = ".")
pk    <- read.csv(url("http://vivaelsoftwarelibre.com/wp-content/uploads/2018/05/pk.csv"),    header = TRUE, sep = ",", quote = "\"", dec = ".")

As you see, we have two different datasets.

Now, I am going to calculate confidence limits (CL) of groups A and B in pk and A and B in perio, having been previously calculated means, sd, and legnth per group.

# Calculate mean, sd and length in pk by Comparison
mean_pk   <- aggregate(pk[, 1], list(Type=pk$Comparison), mean)
sd_pk     <- aggregate(pk[, 1], list(Type=pk$Comparison), sd)
length_pk <- aggregate(pk[, 1], list(Type=pk$Comparison), length)

# 95 confidence limits of pk by Comparison (A and B)
CL_A_pk <- qnorm(0.975)*sd_pk[1,2]/sqrt(length_pk[1,2])
CL_A_pk_lw <- mean_pk[1,2] - CL_A_pk
CL_A_pk_up <- mean_pk[1,2] + CL_A_pk

CL_B_pk <- qnorm(0.975)*sd_pk[2,2]/sqrt(length_pk[2,2])
CL_B_pk_lw <- mean_pk[2,2] - CL_B_pk
CL_B_pk_up <- mean_pk[2,2] + CL_B_pk

# Calculate mean, sd and length in perio by Site
mean_perio <- aggregate(perio[, 2], list(Type=perio$Site), mean)
sd_perio <- aggregate(perio[, 2], list(Type=perio$Site), sd)
length_perio <- aggregate(perio[, 2], list(Type=perio$Site), length)

# 95 confidence limits of pk by Site (A and B)
CL_A_perio <- qnorm(0.975)*sd_perio[1,2]/sqrt(length_perio[1,2])
CL_A_perio_lw <- mean_perio[1,2] - CL_A_perio
CL_A_perio_up <- mean_perio[1,2] + CL_A_perio

CL_B_perio <- qnorm(0.975)*sd_perio[2,2]/sqrt(length_perio[2,2])
CL_B_perio_lw <- mean_perio[2,2] - CL_B_perio
CL_B_perio_up <- mean_perio[2,2] + CL_B_perio

Now, I am ggplotting them separately, with histograms, densities and geom_rects objects, taking into account the groups (A, B)

# ggplot histogram of pk
library(ggplot2)
dif_pk <- ggplot(pk, aes(x=Count, color=Comparison, fill=Comparison)) + 
  geom_density(alpha=0.25) + 
  geom_histogram(aes(y=..density..), alpha=0.25) + 
  geom_rect(aes(xmin=CL_A_pk_lw, xmax=CL_A_pk_up, ymin=0, ymax=0.3), alpha = 0.5, colour = "darkred", fill = "red") +
  geom_rect(aes(xmin=CL_B_pk_lw, xmax=CL_B_pk_up, ymin=0, ymax=0.3), colour = "darkblue", fill = "blue", alpha = 0.5) +
  geom_vline(aes(xintercept=0), lwd=0.5, lty=2) + 
  theme_minimal() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black")) +
  lims(x=c(-50, 50), y=c(0,0.3))

# ggplot histogram of perio
library(ggplot2)
dif_perio <- ggplot(perio, aes(x=vsSAP, color=Site, fill=Site)) + 
  geom_density(alpha=0.25, kernel = "gaussian", adjust = 2) + 
  geom_histogram(aes(y=..density..), alpha=0.25) + 
  geom_rect(aes(xmin=CL_A_perio_lw, xmax=CL_A_perio_up, ymin=0, ymax=2, alpha=0.5), colour = "darkblue", fill = "blue", alpha =0.5) +
  geom_rect(aes(xmin=CL_B_perio_lw, xmax=CL_B_perio_up, ymin=0, ymax=2, alpha=0.5), colour = "darkred", fill = "red", alpha =0.5) +
  geom_vline(aes(xintercept=0), lwd=0.5, lty=2) + 
  theme_minimal() +
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black")) +
  scale_x_continuous(position = "top", limits = c(-75, 20)) +
  scale_y_reverse(position = "right",limits = c(2,0)) +
  coord_flip()

These are the results: enter image description here enter image description here

Now, I am merging both of them using cowplot

library(cowplot)
ggdraw(dif_pk) +
  draw_plot(dif_perio)

With this result: enter image description here

What do I want?

Bearing in mind the last picture, I need to correct several issues in order to obtain the perfect plot:

  • Perfect alignment of the axis of both plots using cowplot. You can see in the top left and bottom right corners how the axis are misaligned. So I would like they form a complete and uniform rectangle.
  • I want to represent the 95 confidence limits but only showing the overlapping areas of both plots. So only two overlapping rectangles might appear approximately in the center of the chart, one for the A group and one for the B group. The vertical and horizontal bars must disappear.
  • I would like previous rectangles to be alpha = .5, so that the overlapping area between both is evident.
  • Each of the previous rectangles must keep the color from each group: A (red) and B (blue).
  • I would like to remove one legend and replace the other in order to ease interpretation.

The rectangles should be something similar to these that have been done manually using Inkscape:

enter image description here

I would sincerely appreciate your help. Hope an R coding expert help me!

antecessor
  • 2,688
  • 6
  • 29
  • 61
  • I'm not totally clear how you want the rectangles to look in the end, but you can get things aligned using the `align_plots` function from **cowplot**. The example in the documentation is pretty useful. Your rectangles are being drawn many times, so, e.g., add `inherit.aes = FALSE` to those layers to get transparency. See [this vignette](http://htmlpreview.github.io/?https://github.com/wilkelab/cowplot/blob/master/inst/doc/shared_legends.html) for info on shared legends. – aosmith May 30 '18 at 21:21
  • Hi @aosmith, thanks for your comment. I edited the question adding at the end a manually-edited plot using Inkscape of the expected plot using R coding. Hope you can help me... I'll appreciate much your help! – antecessor May 30 '18 at 22:39
  • You need to align the plots as shown in the example code here: https://www.rdocumentation.org/packages/cowplot/versions/0.9.2/topics/align_plots – Claus Wilke May 31 '18 at 17:24

0 Answers0