0

I am trying to make the axis labels in ggplot2 bold and size 12. I am using theme(axis.title = element_text(face = "bold", size=12)) to change axis titles and theme(axis.text = element_text(face= "bold", size=12)) to change axix text. However, in 2 geom_plots the text does not become bold or change size. There is no warning message that the function is unused or unrecognized or anything, it just doesn't change the text.

These lines worked perfectly fine in other ggplots I have made but are not working now and I can not figure out why. Below are two examples of code where I have used the element_text function. The only real difference I can discern between the two is that the one that works uses geom_line to build the plot and the one that doesn't work uses geom_bar. Is there anything that is noticeable in the code that is causing one to work and one not?

#this code works, text becomes size 12 and bold

    ggplot(spec, aes(x=Frequency)) + 
    geom_line(aes(y = AT_avg), color = "blue", size=0.75) +
    geom_line(aes(y = avg), color="red", size=0.75)+
    ylab("Average Amplitude (dB)")+
    xlab("Frequency (Hz)")+
    theme_bw()+
    theme(axis.title = element_text(face="bold", size=12))+
    theme(axis.text = element_text(face="bold",size=12))

#this code does not change the size or face of the text

    ggplot(count) + geom_bar(aes(x=freq, y=D_count), stat="identity", fill="red") + 
    scale_x_continuous(limits=c(0,7000)) +
    scale_y_continuous(limits=c(0,15))+
    xlab("Frequency (Hz)")+
    ylab("")+
    theme_bw() + 
    theme(axis.title = element_text(face = "bold", size=12))+
    theme(axis.text = element_text(face= "bold", size=12))+
    geom_hline(yintercept=2.23, color ="black", size= 1)

sample data set of count (from code that does not work) included below

> dput(head(count))
structure(list(freq = c(100, 300, 500, 700, 900, 1100), AT_count = c(7, 
7, 13, 8, 7, 1), AT_exp = c(3.26, 3.26, 3.26, 3.26, 3.26, 3.26
), D_count = c(4, 5, 9, 4, 2, 2), D_exp = c(2.23, 2.23, 2.23, 
2.23, 2.23, 2.23)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
JLM
  • 1
  • 1
  • Does it make a difference if you call `geom_hline()` before your `theme()` commands? – ktiu Jun 03 '21 at 17:30
  • Can you provide a sample of the dataset with `dput(head(count))` that we can copy and paste to better understand the issue and test solutions? (See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)) – ktiu Jun 03 '21 at 17:33
  • @ktiu I have edited the post to include sample data, I did not know how to do that before so thank you – JLM Jun 03 '21 at 18:49
  • changing the order of the calls did not change anything – JLM Jun 03 '21 at 18:50
  • Thanks! Interestingly, it works for me, the axis text is changed according to the `theme()` command. Are you sure you are looking at the right output? – ktiu Jun 03 '21 at 18:57
  • @kitu hmm that is interesting! That makes it even more puzzling why it won't work for me. I am simply looking at the plot that is created in the plot window of R studio. I even tried exporting the image to see if it was the window that not displaying it correctly, and I tried copying the code into a new workbook to see if the workbook was causing the problem but still no luck. – JLM Jun 03 '21 at 20:32

0 Answers0