Search code examples
rggplot2likert

Removing Vertical Mean Line and changing transparency in likert.density.plot


Using likert.density.plot from library("likert"), I am attempting to make a density plot of likert data. The plot works, but I can't seem to change all the aspects of visualization I am expecting to. Particularly I would like to change:

  • Fill transparency to opaque - pretty sure the argument to pass to density is alpha=1, but I can't figure out where it goes

  • Removing the vertical lines at the means. Attempted to use geom_vline(FALSE) but didn't work.

Minimal Working Example:

(Actual data cannot be disclosed, so generated some in the same format)

df = data.frame(Q1Pre=c(1,2,1,2,1,3,2,1),Q1Post=c(2,1,3,2,3,2,3,2))

# Covert values to factors
df = lapply(df, factor, levels = 1:3)
df = as.data.frame(df)

#Convert to likert object
likertdf = likert(df)

likert.density.plot(likertdf, facet=FALSE, bw=0.5)

Plot

I would like this to look more like this (with opaque color and no mean line):

Formatting Goals

Which can be done with ggplot as + geom_density(), so tried

likert.density.plot(likertdf,facet=FALSE,bw=0.5,aes(geom_density))

with no change.

Here is a slightly larger than working example to show how I have done some of the other formatting:

likert.density.plot(likertdf,facet=FALSE,bw=0.5) + 
  scale_x_continuous(breaks=c(1,2,3),
                     labels=c("Disagree", "Neutral", "Agree")) + 
  scale_fill_manual(values = c("red","blue"))

Which does change the colors and axes as desired:

Some formatting works

But the same types of formatting edits don't change the fill or remove the mean line.


Solution

  • Two possible approaches for consideration, both assuming your likert plot is called p.

    p <- likert.density.plot(likertdf, facet=FALSE, bw=0.5)
    

    Approach 1: get the computed dataset for density back out of p and plot it yourself

    p[["data"]] %>%
      ggplot(aes(x = x, y = y, fill = Item)) +
      geom_polygon(color = "black", linewidth = 2)  +
    
    # everything below here attempts to replicate likert.density.plot's default
    # appearance; you can change them to suit your needs
    
      labs(fill = "Item", x = "", y = "") +
      scale_x_continuous(breaks = 1:likertdf$nlevels, 
                         labels = likert:::label_wrap_mod(paste0(levels(likertdf$items[, 1]), 
                                                                 " (", 
                                                                 1:likertdf$nlevels,
                                                                 ")"), 
                                                          width = 10)) +
      theme(axis.text.y = element_blank(),
            axis.ticks.y = element_blank())
    

    Approach 2: modify / remove offending elements in p

    p$layers[[1]]$aes_params$alpha <- 1        # set 1st layer (geom_polygon)'s alpha as 1
    p$layers[[1]]$aes_params$colour <- "black" # add black outline
    p$layers[[1]]$aes_params$linewidth <- 2    # specify thickness of black outline
    p$layers[[3]] <- NULL # remove 3rd layer (geom_path)
    p$layers[[2]] <- NULL # remove 2nd layer (geom_vline)
    
    plot(p)
    

    Result from both approaches: result