我很难弄清楚如何在ggplot条形图中匹配字体大小。轴标签和构面条标签周围的元素似乎是正确的,但绘图区域中数据标签的字体大小似乎更小。有没有办法指定字体大小,使所有字体的大小相同?
我想要制作的图表的工作示例如下。
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
答案 0 :(得分:1)
以下是否解决了您的问题?我使用了ggthemes
包中的另一个主题[tufte]并更改了主题(文本)大小。有趣的是,似乎ggplot_build(p)
没有给出文本元素的大小。这里似乎轴标签大于轴文本。
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