1

I can't seem to get the % labels to plot on each of these bars when I group my data by factors in R. Any ideas? Thank you!

# Load libraries
library(ggplot2)        # for plotting
library(likert)         # for analyzing likert data
library(plyr)           # for using plot.percents=TRUE

# Create dataset
q1_pre <- c("Strongly agree", "Strongly agree", "Strongly agree", "Strongly agree", "Disagree", "Neither", "NA", "Somewhat agree", "Somewhat disagree","Neither")
q2_pre <- c("Strongly agree", "Strongly agree", "Strongly agree", "Strongly agree", "Disagree", "Neither", "NA", "Somewhat agree", "Somewhat disagree","Neither")

lab_pre <- rep("pre", length(q1_pre))
pre <- cbind(lab_pre, q1_pre, q2_pre)

q1_post <- c("Strongly disagree", "Strongly disagree", "Strongly disagree", "Strongly disagree", "Strongly disagree", "Neither", "NA", "Somewhat disagree", "Somewhat agree","Neither")
q2_post <-c("Strongly disagree", "Strongly disagree", "Strongly disagree", "Strongly disagree", "Strongly disagree", "Neither", "NA", "Somewhat disagree", "Somewhat agree","Neither")
lab_post <- rep("post", length(q1_post))
post <- cbind(lab_post, q1_post, q2_post)

df <- data.frame(rbind(pre, post))
df_lev <- rev(c("Strongly agree", "Somewhat agree", "Neither", "Somewhat disagree", "Strongly disagree", "NA"))

for (i in 2:ncol(df)) {
    df[ , i] <- factor(df[, i], levels=df_lev)
}

# Convert to likert structure
df.likert <- likert(df[2:ncol(df)], grouping=df$lab_pre)

# Plot
quartz(width=8, height=6)                   
plot <- plot(df.likert, text.size=4, plot.percents=T, centered=FALSE,   
                      colors=c("gray25","#CA0020", "#F4A582", "gray80", "#92C5DE", "#0571B0"))  +
         theme(text=element_text(size=14), legend.box.background=element_rect(color="white"),                          legend.title=element_text(size=8), legend.position="bottom", plot.title = element_text(hjust=0.5)) + 
         ylab("% of Participants") +
         guides(fill=guide_legend(nrow=2, title="", reverse=T)) +
         ggtitle("Title") +
         theme(axis.text.x=element_text(color="black"))

plot

My plot looks like this: Resulting plot

r2evans
  • 141,215
  • 6
  • 77
  • 149
eduscholar
  • 11
  • 2
  • 1
    Hi eduscholar. If you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) its way easier for others to find a solution to your problem. That way you can help others to help you! – dario Feb 22 '20 at 16:01
  • 2
    Your code seems to be mixing base `plot` with various ggplot2 functions, which shouldn't work. As dario said, can you provide a reproducible example, i.e., a small demonstration of runnable code that reproduces your problem? – jdobres Feb 22 '20 at 16:03
  • Just edited my code above with an example. Thank you for that idea! – eduscholar Feb 22 '20 at 16:30

1 Answers1

0

Looking at the code of likert.bar.plot, it seems like there is a weird part that should have warned about group + plotting percentages:

if (FALSE & plot.percents) {
            warning("plot.percents is not currenlty supported for grouped analysis.")
        }

Basically you can recalculate the positions of the text, and put it back using geom_text().Try something like below, take note you don't need the library plyr for this, if you have loaded it, make sure it is loaded before dplyr, otherwise it causes a lot of problems:

library(ggplot2)
library(likert)
#library(plyr)
library(dplyr)

func = function(X){
offset=sum(X[X<0])
cumsum(abs(X))+offset - abs(X)/2
}

p <- plot(df.likert, text.size=4, plot.percents=TRUE)

label_data = p$data %>% 
group_by(Group,Item) %>% 
mutate(perc=round(abs(value),digits=1),value=func(value)) %>%
filter(perc!=0)

p + geom_text(data=label_data,aes(label=paste0(perc,"%")),size=2)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72