2
votes

I'm having a hard time figuring out how to match font size in a ggplot bar chart. The elements around the axis labels and facet strip labels seem to be right, but the font size for the data labels in the plot area seem smaller. Is there a way to specify that font size such that all the fonts will be the same size?

working example of the chart I'm trying to produce is below.

library(ggplot2)
library(reshape2)
library(data.table)
library(scales)

d <- data.frame(A=c(6.8, 15.1, 7.3), B=c(2.1, 5.9, 1))
#d$ind <- seq_along(d$A)
d$hyp <- c("Rule of law", "Function", "Seniority")
d$hyp <- factor(d$hyp, levels = c("Rule of law", "Function", "Seniority"))

d.m <- melt(d, id.var='hyp')
d.m <- d.m[order(d.m$hyp),]
d.m$variable <- c("Stronger", "Weaker", "Finance", "Non-finance", "Senior", "Junior")
d.m$variable <- factor(d.m$variable, levels = c("Stronger", "Weaker", "Finance", "Non-finance", "Senior", "Junior"))
d.m
d.m$ci_hi <- d.m$value + 1.96 * c(2.1, 9.3, 6.2, 2.5, 3.2, 5.3)
d.m$ci_lo <- d.m$value - 1.96 * c(2.1, 9.3, 6.2, 2.5, 3.2, 5.3)
d.m <-data.table(d.m)


p <- ggplot(d.m, aes(x=variable, y=value, fill=variable)) 
        p <- p + theme_bw() 
p <- p + geom_bar(stat='identity', position='dodge')
p <- p + geom_text(aes(label=value), position=position_dodge(width=0.9), vjust = -.25, size = element_text(size = 18))

p <- p + facet_grid(. ~ hyp, scales = "free_x")
p <- p + geom_hline(aes(yintercept=5.8))
p <- p + geom_text(aes(0,5.8,label = "5.8", vjust = -.2, hjust = -0.2), size = element_text(size = 18))
p <- p + scale_fill_manual(values=alpha(c("red4", "steelblue", "red4", "steelblue", "red4", "steelblue"), .5))
#p <- p + scale_y_continuous("yaxis", limits = c(-2,20))
#p <- p + geom_errorbar(aes(ymin= ci_lo, ymax= ci_hi), width=.1, position = "dodge")
p <- p + theme(legend.position="none")
#p <- p + theme(axis.text.x=element_text(angle=-45, hjust = 0))
p <- p + theme(axis.text.x = element_text(size = 18))
p <- p + theme(axis.text.y = element_text(size = 18))
p <- p + theme(axis.title.x = element_text(size = 18))
p <- p + theme(axis.title.y =  element_text(size = 18))
p <- p + theme(text =  element_text(size = 50))
p <- p +  theme(strip.text.x = element_text(size = 18, colour = "red4", angle = 0))
p <- p + xlab("Scandal effect")
p <- p + ylab("Percentage point impact on compensation")
p
1

1 Answers

1
votes

Does the following address your issue? I used another theme [tufte], from the ggthemes package and changed your theme(text) size. Interestingly, it seems that ggplot_build(p) does not give the sizes of text elements. It seems here that the axis labels are larger than the axis text.

library("ggthemes")
p <- ggplot(d.m, aes(x=variable, y=value, fill=variable)) 
p <- p + theme_tufte() 
p <- p + geom_bar(stat='identity', position='dodge')
p <- p + geom_text(aes(label=value), position=position_dodge(width=0.9), vjust = -.25, size = element_text(size = 18))

p <- p + facet_grid(. ~ hyp, scales = "free_x")
p <- p + geom_hline(aes(yintercept=5.8))
p <- p + geom_text(aes(0,5.8,label = "5.8", vjust = -.2, hjust = -0.2), size = element_text(size = 18))
p <- p + scale_fill_manual(values=alpha(c("red4", "steelblue", "red4", "steelblue", "red4", "steelblue"), .5))
p <- p + theme(legend.position="none")
# p <- p + theme(axis.text.x = element_text(size = 18))
# p <- p + theme(axis.text.y = element_text(size = 18))
# p <- p + theme(axis.title.x = element_text(size = 18))
# p <- p + theme(axis.title.y =  element_text(size = 18))
p <- p + theme(text =  element_text(size = 18)) # changed from size = 50
p <- p +  theme(strip.text.x = element_text(size = 18, colour = "red4", angle = 0))
p <- p + xlab("Scandal effect")
p <- p + ylab("Percentage point impact on compensation")
p

enter image description here