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()
Now, I am merging both of them using cowplot
library(cowplot)
ggdraw(dif_pk) +
draw_plot(dif_perio)
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:
I would sincerely appreciate your help. Hope an R coding expert help me!