我想将图例标题sex稍微移动到图例框的水平中心.我试过theme和guide_legend,但失败了.两种方式都不会改变图例标题的位置.
# example data from http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
library(ggplot2)
p <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) +
geom_line() + geom_point()
# no change
p + theme(legend.title = element_text(hjust = 0.5))
p + guides(color=guide_legend(title.hjust=0.5))
Run Code Online (Sandbox Code Playgroud)
另外,我使用的是ggplot2_2.2.0.
在对齐网格图形对象时遇到一些麻烦 - 已经阅读了我能找到的所有文档,包括Murrell书籍,但没有成功.我认为我想做的事情非常简单,所以希望我很简单.
这是一个可重现的例子,它将在Hadley的hflights包中按目的地制作所有航空承运人的PDF (反映我在不同的数据集上尝试做的事情).
require(hflights)
require(gridExtra)
require(Cairo)
make_table <- function(df) {
p <- tableGrob(
df
,padding.h=unit(.25, "mm")
,show.rownames=FALSE
,gpar.coretext = gpar(fontsize=8, lineheight=0)
#this doesn't seem to justify the table
,just = c("bottom")
,show.box = T
)
return(p)
}
dests <- unique(hflights$Dest)
#list to hold the plots
plot_list <- list()
#loop over destinations and make a simple report
for (i in dests) {
#just this destination
this_dest <- hflights[hflights$Dest == i, ]
#the title
title <- textGrob(label = …Run Code Online (Sandbox Code Playgroud)