MH765
MH765

Reputation: 410

Likert package R

It is possible to change the font size in a plot using the Likert package in R using the addition of ggplot "theme" commands. However, when a bar plot and histogram are plotted together at the same time, these changes do not affect the resulting graphics. Is there a way to do modify the font size and still plot both the bar plot and histogram at the same time? For instance, the following code will successfully modify the axis text:

likert.bar.plot(items, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
       axis.text.y = element_text(colour = "blue",
                                  family = "Courier"))`

... but the following code will not:

plot(items, include.histogram=T, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
       axis.text.y = element_text(colour = "blue",
                                  family = "Courier"))`

This question explains the basics of How do I change the text font, size and colour of all the different texts in the R package, Likert?

Upvotes: 0

Views: 1945

Answers (1)

Mark Peterson
Mark Peterson

Reputation: 9570

The issue is that plot.likert is using the grid pacakge to combine the plots. This means that it is not returning any object that can be modified (try it yourself, saving the output -- it returns NULL). When only one plot is printed, however, it returns the plot with the class ggplot (among others) allowing it to be manipulated by theme and other ggplotfunctions.

If you want this to work in a single function call, you will probably need to edit the code for plot.likert yourself. Alternatively, and likely more robust/flexible: you may want to look into the grid package yourself to combine the plots.

E.g.,:

data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
names(items29) <- c("Magazines", "Comic books", "Fiction", 
                    "Non-fiction books", "Newspapers")
l29 <- likert(items29)



a <- 
  likert.bar.plot(l29, legend.position = "none") +
  theme(text = element_text(size = rel(6), colour = "red"),
        axis.text.y = element_text(colour = "blue",
                                   family = "Courier"))

b <-
  likert.histogram.plot(l29, legend.position = "none") +
  theme(text = element_text(size = rel(6), colour = "red"),
        axis.text.y = element_text(colour = "blue",
                                   family = "Courier")) +
  theme(axis.text.y = element_blank())


library(gridExtra)
grid.arrange(a,b,widths=c(2,1))

(Note the inclusion of an MWE so that others can run the code too.)

(Thanks @eipi10 for the cleaner code to combine them)

enter image description here

Upvotes: 1

Related Questions