R中使用scale_fill_gradientn时如何调整ggplot2图例的字体大小与粗体
你需要在现有代码的theme()配置项中,新增图例相关的样式参数即可:
- 调整图例标题的样式用
legend.title参数 - 调整图例刻度文本的样式用
legend.text参数
修改后的完整代码如下:
yellowgreen <- c("yellow", "dark green") pal <- colorRampPalette(yellowgreen)(100) library(ggplot2) data <- read.table("file_directory", sep=";", header = T) v <- ggplot(data, aes(x = reorder(A, B) , y = Pourcentage, fill = B)) v + geom_tile() + geom_text(aes(label = round(B,1)), color = "black", size = 4, fontface=2) + scale_fill_gradientn(colors = pal, name = expression("Legend Title")) + labs(y = "Title for Y", x ="Title for X") + theme(axis.title.x = element_text(face="bold",size=24, color="black"), axis.text.x = element_text(face="bold",size=24, color="black"), axis.title.y = element_text(face="bold",size=24, color="black"), axis.text.y = element_text(face="bold", size=24, color="black"), # 新增以下两行配置图例样式,可自行调整size参数修改字号 legend.title = element_text(face="bold", size=24, color="black"), legend.text = element_text(face="bold", size=22, color="black"))
内容的提问来源于stack exchange,提问作者iforire
相关产品推荐
相关产品推荐

