Search code examples
rggplot2density-plot

Rescale ylimits density plot ggplot r


I made a Boxplot with connected data and I am trying to add a density plot. The problem is that the density plot seems to be on a different scale.

This is what the plot looks likeenter image description here

It's clear that the density plots should have limits from 0 to somewhere around 300.

This is my datasets reproduced with dput:

structure(list(progNum = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 40L, 41L, 42L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L), Condition = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), levels = c("Sum_noaction", "Sum_csm"), class = "factor"), 
    nRes = c(97.5, 96.5, 97.5, 94, 97, 89.5, 96.5, 245.5, 232.5, 
    233.5, 168.5, 222.5, 232.5, 195.5, 241.5, 201, 215.5, 189, 
    197.5, 238.5, 231, 219, 217.5, 225.5, 199.5, 299, 282, 294, 
    314, 238, 293, 289, 299.5, 252, 297.5, 278.5, 256.5, 297.5, 
    276.5, 214.5, 188, 217.5, 92.5, 98, 98.5, 89, 97, 92.5, 97, 
    247, 218.5, 227, 175.5, 227.5, 232, 187, 240.5, 201.5, 221.5, 
    86.5, 197.5, 232.5, 231.5, 224.5, 212, 220.5, 197.5, 309, 
    275, 287.5, 310.5, 226, 298, 283, 223, 50, 294.5, 293, 260.5, 
    301, 281.5, 215.5, 189.5, 216.5), pairings = c(1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
    29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
    41L, 42L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
    13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
    25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 
    37L, 38L, 39L, 40L, 41L, 42L)), row.names = c(NA, -84L), class = "data.frame")

This is my code so far:

library(readxl)
library(ggplot2)
library(dplyr)
library(psych)
library(tidyr)
library(patchwork)
###################################################################################################
###                          Setup a theme for Boxplots
###################################################################################################
# define theme to make plots look pretty
plotThemeBoxplots = theme(
  text = element_text(size = 18),
  axis.title.x=element_blank(),
  # axis.text.x=element_blank(),
  axis.ticks.x=element_blank(),
  axis.title.y = element_text(size = 18),
  axis.text = element_text(size = 18),
  # axis.text.x = element_text(angle = 0, vjust = 0.5),
  legend.title=element_text(size=18),
  legend.text=element_text(size=18),
  legend.position = "none",
  plot.title = element_text(lineheight=.8, face="bold", size = 16),
  panel.border = element_blank(),
  panel.background = element_blank(),
  axis.line.x = element_line(colour = 'black', size=1, linetype='solid'),
  axis.line.y = element_line(colour = 'black', size=1, linetype='solid'))


###################################################################################################
#                  CONNECTED BOXPLOTS DATA Action Vs CS- N RES
###################################################################################################
#boxplotData = data %>% 
  #dplyr::select(progNum, Sum_noaction, Sum_csm) %>% 
  #melt(id.vars = c("progNum"), measure.vars = c("Sum_noaction", "Sum_csm"),
       #variable.name = "Var", value.name = "nRes")


pairings = c(c(1:42), c(1:42))
boxplotData$pairings = pairings

names(boxplotData)[2] = "Condition"
names(boxplotData)[3] = "nRes"

connected_dots_03 = boxplotData %>%
  ggplot(aes(Condition, nRes, fill=Condition)) +
  geom_boxplot() +
  geom_line(aes(group=pairings), position = position_dodge(0.2)) +
  geom_point(aes(fill=Condition,group=pairings),size=2,shape=21, position = position_dodge(0.2)) +
  theme(legend.position = "none")+
  scale_fill_manual(name="Condition",labels=c("No Action", "CS-"),
                    values=c("hotpink1", "green"))+
  scale_y_continuous(breaks = round(seq(0, 600, by = 100),1), limits = c(0, 600))+
  plotThemeBoxplots+
  ylab("Number of Responses")+
  scale_x_discrete(labels=c('No Action', 'CS-'))

connected_dots_03

densConnectdBox <- ggplot(boxplotData, aes(x = nRes, fill=Condition)) + 
  geom_density(alpha = .5, colour = NA) + # , fill="turquoise1"
  theme_void() + 
  theme(legend.position = "none") +
  coord_flip()+
  scale_fill_manual(values=c("hotpink1", "green"))
 

connected_dots_03 + densConnectdBox + 
  plot_layout(ncol = 2, nrow = 1, widths = c(2, 0.5), heights = c(1, 4))

I've tried several things such as addying:

 coord_cartesian(ylim = c(0, 600))

Nothing seems to work...

Thank you very much in advance!


Solution

  • You must specify the limits of the x axis of the density plot using the scale_x_continuous function such that it matches the limits of the other plot.

    You can do it when you generate densConnectdBox or even afterwards as follows:

    connected_dots_03 + 
      (densConnectdBox + scale_x_continuous(limits = c(0, 600))) + 
      plot_layout(ncol = 2, nrow = 1, widths = c(2, 0.5), heights = c(1, 4))
    

    enter image description here