Can't increase font size for x axis
2
1
Entering edit mode
4 months ago
Chris ▴ 340

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 • 826 views
ADD COMMENT
2
Entering edit mode
4 months ago
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

ADD COMMENT
0
Entering edit mode

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")
ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode
4 months ago
bk11 ★ 3.0k

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

ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
1
Entering edit mode

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))
ADD REPLY
1
Entering edit mode

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.

ADD REPLY
1
Entering edit mode

Great info! Very helpful. Thank you, Matt!

ADD REPLY

Login before adding your answer.

Traffic: 1640 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6