1

I'm having issues using theme() in ggplot2.

Here's the plot:

ggplot(data=graph.data, aes(Tree, Proportion)) 
+ geom_bar(stat="identity", fill="black") 
+ geom_errorbar(data=graph.data, mapping=aes(x=Tree, ymin=Proportion-SE, ymax=Proportion+SE), width=0.255) 
+ labs(title="Corrected mean LdNPV mortality", x="Tree line", y="Percent mortality") 
+ geom_text(aes(x=Tree, y=Proportion+SE+0.05, label=Letters)) 
+ theme(text=element_text(color="blue", size=12))

Setting the color to blue worked on the plot title and axis titles, but not axis text. Further, the size of the plot title remains larger than the size of the axis titles which is larger than the axis text. Increasing it to size=20 increases the plot title, axis titles, and axis text, but again, not to uniform sizes. Any ideas what's going on?

Reproducible example:

cities <- c("New York", "Tokyo", "London")
number <- c(20, 50, 35)
letter <- c("a", "b", "ab")
dummy.data <- data.frame(cities, number, letter)
ggplot(data=dummy.data, aes(cities, number)) +
  geom_bar(stat="identity", fill="black") +
  labs(title="Plot title", x="X axis title", y="Y axis title") +
  geom_text(aes(x=cities, y=number+5, label=letter)) + 
  theme(text=element_text(color="blue", size=12))

1 Answers1

1

You can specify all of these separately from theme, not sure why it is not inheriting the color from the theme title, perhaps you can submit this to Hadley as a bug?

I prefer to set the sizes of things individually and relative to the base plotting size, e.g., the axis text and title will be twice the size of the regular text. You can choose another multiplier. You can also check out ?grid::unit.

library(ggplot2)

cities <- c("New York", "Tokyo", "London")
number <- c(20, 50, 35)
letter <- c("a", "b", "ab")
dummy.data <- data.frame(cities, number, letter)
ggplot(data=dummy.data, aes(cities, number)) +
  geom_bar(stat="identity", fill="black") +
  labs(title="Plot title", x="X axis title", y="Y axis title") +
  geom_text(aes(x=cities, y=number+5, label=letter)) + 
  theme(axis.text = element_text(colour = "blue", size = rel(2)),
        title = element_text(color = "red", size = rel(2)))
shayaa
  • 2,787
  • 13
  • 19