This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Can't increase font size for x axis

Hi all,

The words in the x axis are too small to read, so I try to increase by use size = 20. However the x axis didn't change size regardless of input in size and it is not bold:

ggplot(data, aes(x = Pathway, y = Beta, fill = Genotype)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 20, face = "bold"))

Would you please have a suggestion? I appreciate it!

r ggplot2

2 answers

ggplot(mtcars, aes(x = rownames(mtcars), y = mpg, fill = cyl)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 20, face = "bold"))

Your code works for me (I just modified it to plotting sample data). Which plotting device do you use and which version of ggplot? Does the 45-degree rotation work?

example

Hi Matt, angle 45 didn't work. ggplot2 3.5.1. I just plot in Rstudio.

data <- data.frame(
  Pathway = c("BIOCARTA_IL12_PATHWAY", "CONRAD_STEM_CELL"),
  group1 = c(0.027233015, -0.025555915),
  group2 = c(-0.01753847, 0.01116901)
)
data <- melt(data, id.vars = "Pathway", variable.name = "Genotype", value.name = "Beta")

If the angle45 also didn't work, then there is something odd with the plotting device, either a missing library like Cairo or maybe a lack of permissions. You can try reading through library(help = "grDevices") to learn more and change the plotting device. Which plotting device is the default depends on your OS, but the first thing to try would be switching that temporarily.

myplot <- ggplot(mtcars, aes(x = rownames(mtcars), y = mpg, fill = cyl)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 20, face = "bold"))

    png("Test-cairo.png",type="cairo")
    myplot
    dev.off()

    png("Test-xlib.png",type="Xlib")
    myplot
    dev.off()

    png("Test-quarz.png",type="quarz")
    myplot
    dev.off()

A quick reference for the different devices is usually available with ?png, ?tiff, ?pdf, ?svg etc. I suspect particularly an issue with the vector devices and the fonts.

Thank you so much! Your example code works fine. I guess it is because of my data? Would you please run with the data I provide above?

I really don't see why that should depend on the data being plotted. But I nonetheless tried, and the formatting was applied as well.

Sorry, but I can't help you beyond the dreaded "Works for me / on my machine". If you have e.g. Docker or Singularity, you could try the containerized version of Tidyverse just to double-check that it is not a problem with the Graphics Device.

PS: mind that melt is superseded by pivot_longer in the meantime.

Sometimes, I see mysterious results from familiar code. I guess somethings hidden changed that I don't noticed. Thank you for your help!

I got this result. Didn't you?

data <- data.frame(
    Pathway = c("BIOCARTA_IL12_PATHWAY", "CONRAD_STEM_CELL"),
    group1 = c(0.027233015, -0.025555915),
    group2 = c(-0.01753847, 0.01116901)
    )
    data <- melt(data, id.vars = "Pathway", variable.name = "Genotype", value.name = "Beta")
    #Warning message:
    #The melt generic in data.table has been passed a data.frame and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is #superseded and is no longer actively developed, and this redirection is now deprecated. To continue using melt methods from reshape2 while both libraries #are attached, e.g. melt.list, you can prepend the namespace, i.e. reshape2::melt(data). In the next version, this warning will become an error. 
     data
                    Pathway Genotype        Beta
    1 BIOCARTA_IL12_PATHWAY   group1  0.02723301
    2      CONRAD_STEM_CELL   group1 -0.02555591
    3 BIOCARTA_IL12_PATHWAY   group2 -0.01753847
    4      CONRAD_STEM_CELL   group2  0.01116901

 ggplot(data, aes(x = Pathway, y = Beta, fill = Genotype)) +
     geom_bar(stat = "identity", position = position_dodge()) +
     theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 20, face = "bold"))

enter image description here

It turns out the reason is because:

ggplot(data_melted, aes(x = Pathway, y = Beta, fill = Genotype)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 4, face = "bold")) +
  labs(title = "Comparison of Beta Values Between group 1 and group 2",
       x = "Pathway",
       y = "Beta Value",
       fill = "Genotype") +
  theme_minimal() + theme(plot.title = element_text(hjust = 0.5))+
  scale_fill_manual(values = c("group1" = "green", "group2" = "red"))

I thought the extra modification can't cause the issue but it is. So how can I still increase font size when using extra modification like this? Thank you.

Update. The conflict in theme cause the issue. I figure it out. Thank you.

Do not use theme() function twice. The following code will give you the result you wanted.

ggplot(data_melted, aes(x = Pathway, y = Beta, fill = Genotype)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    labs(title = "Comparison of Beta Values Between group 1 and group 2",
         x = "Pathway",
         y = "Beta Value",
         fill = "Genotype") +
    theme_minimal() +
    scale_fill_manual(values = c("group1" = "green", "group2" = "red"))+theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 8, face = "bold"), plot.title = element_text(hjust = 0.5))

Technically, you can use theme() as often as you want, but the order matters. Contrary to math, where a + b = b + a applies, ggplot2 plots are built up in a layered fashion.

Starting from the base plot, you apply additional geometries or selective theme modifications with increasing specificity. Adding theme_minimal() after your desired modification was resetting all prior theme attributes, because it is a complete theme that comprises style settings for each element.

Great info! Very helpful. Thank you, Matt!

Log in to answer this question.