嗨,这个简单的代码(以及今天早上的所有脚本)已经开始在ggplot2中给我一个偏离中心的标题
Ubuntu version: 16.04
R studio version: Version 0.99.896
R version: 3.3.2
GGPLOT2 version: 2.2.0
Run Code Online (Sandbox Code Playgroud)
今天早上我刚刚安装了上面这个以试图解决这个问题....
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
# Add title, narrower bars, fill color, and change axis labels
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") +
guides(fill=FALSE) +
xlab("Time of day") + ylab("Total bill") +
ggtitle("Average bill for 2 people")
Run Code Online (Sandbox Code Playgroud)
我正在尝试绘制多个绘图,使用ggplot2它们进行排列grid.arrange().由于我设法找到某人描述我遇到的确切问题,因此我引用了链接中的问题描述:
当我使用ggsave()后grid.arrange(),即
Run Code Online (Sandbox Code Playgroud)grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) ggsave("sgcirNIR.jpg")
我不保存网格图,而是保存最后一个ggplot.是否有任何方法可以通过grid.arrange()使用
ggsave()或类似的方式实际保存绘图?除了使用旧的方式
Run Code Online (Sandbox Code Playgroud)jpeg("sgcirNIR.jpg") grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) dev.off()
相同的链接提供以下解决方案:
require(grid)
require(gridExtra)
p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device
ggsave("saving.pdf", p) # need to specify what to save explicitly
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何使用以下代码ggsave()保存grid.arrange()调用的输出,该代码取自链接:
library(ggplot2)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == …Run Code Online (Sandbox Code Playgroud) 在 ggplot 中居中对齐绘图标题的“lege artis ”方式 - plot.title = element_text(hjust = 0.5) - 将标题置于绘图区域的中心,不包括轴标签。
当轴标签很长时,这可能会变得很难看,例如 Mary Poppins Soundtrack 中的歌曲与角色长度的关系图。
library(tidyverse)
mary_poppins <- data_frame(song = c("Overture", "Sister Suffragette", "The Life I Lead", "The Perfect Nanny", "A Spoonful of Sugar", "Pavement Artist", "Jolly Holiday", "Supercalifragilisticexpialidocious", "Stay Awake", "I Love to Laugh", "A British Bank", "Feed the Birds ", "Fidelity Fiduciary Bank", "Chim Chim Cher-ee", "Step in Time", "A Man Has Dreams", "Let's Go Fly a Kite"
))
mary_poppins <- mary_poppins %>% …Run Code Online (Sandbox Code Playgroud) 我遵循了讨论的建议
使用grid.arrange在multiplot ggplot2中更改标题
但我的2行标题不会改变字体大小.
1-我应该在grid.arrange中声明main还是在没有main的情况下使用grid.arrange然后添加以下脚本
main=textGrob(paste("titleLine1", "titleLine2", sep = "\n"),gp=gpar(fontsize=20))
Run Code Online (Sandbox Code Playgroud)
谢谢
颂歌