2

我是 ggplot2 的新手,我有一个平均响应的条形图,我从示例代码中提取出来。

dput() 输出以重现图形:

> dput(mainerrors.df)
structure(list(sex = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Male", 
"Female"), class = "factor"), condition = structure(c(1L, 2L, 
3L, 1L, 2L, 3L), .Label = c("Weather", "Negative", "Positive"
), class = "factor"), N = c(19, 18, 26, 55, 50, 49), willingness = c(5.47368421052632, 
5.48148148148148, 5.97435897435897, 6.37575757575758, 5.88666666666667, 
6.2312925170068), sd = c(1.3067525929499, 1.41524021482219, 1.05797742854762, 
0.865560893471263, 1.13611104180873, 0.969927645336604), se = c(0.299789605098861, 
0.333575317636226, 0.207486444350194, 0.116712207066377, 0.160670364368774, 
0.138561092190943), ci = c(0.629834588787353, 0.703782401529606, 
0.427326331312926, 0.233993886626096, 0.32287918557602, 0.278595748013486
)), .Names = c("sex", "condition", "N", "willingness", "sd", 
"se", "ci"), row.names = c(NA, 6L), class = "data.frame")

重现图形的代码:

figure.4 <- ggplot(mainerrors.df, aes(x = condition, y = willingness)) +
                facet_wrap(~sex) +
                geom_bar(stat="identity", colour="black", aes(fill=sex)) +
                geom_errorbar(aes(ymin=willingness-ci, ymax=willingness+ci),
                              size=.3,
                              width=.2,
                              position=position_dodge(.9)) +
                scale_fill_brewer(palette="Set3", name="Sex",
                                  breaks=c("Male", "Female"),
                                  labels=c("Male", "Female")) +
                scale_x_discrete("Peer Comment Frame") +
                scale_y_continuous("Willingness to use a condom (95% CI)", breaks=1:7)
                theme(plot.background = element_rect(fill = "transparent", colour = NA),
                      legend.position = "none",
                      axis.text.x = element_text(size=16),
                      axis.text.y = element_text(size=16),
                      axis.title.x = element_text(face="bold", colour="#7f7f7f", size=16, vjust=0.1),
                      axis.title.y = element_text(face="bold", colour="#7f7f7f", size=16),
                      strip.text.x = element_text(size = 16, colour = "black"))

一切看起来都很好,除了无论我玩多少 vjust,我都无法按照我想要的方式获得轴标题。所以我想:为什么不把轴标题放在情节本身,而不是挂在那里呢?刻面标题是我希望图表外观的完美示例(似乎我无法直接发布图像): http: //postimg.org/image/cnma6zp99/

我希望 x 轴/y 轴标题沿着图的底部/左侧运行,就像男性/女性在顶部一样,因为它看起来更加连贯并避免所有定位问题。有没有办法做到这一点(或者比 vjust 允许的更精确地控制轴标题定位的方法,但这将是最佳的)?

编辑:我将我如何设想我的图表的一个粗略的例子拼凑在一起: 在此处输入图像描述

4

1 回答 1

1

目前尚不清楚您想做什么,但这里是受@baptiste 启发的解决方案的开始。解决方案在这里。主要思想是定义一个自定义元素来保存 x 和 y 标题的自定义。解决方案远未完成,但它显示了这个想法。你也可以使用格子。这种定制很简单lattice

在此处输入图像描述

require(ggplot2)
require(grid)

# user-level interface to the element grob
my_axis = function(text,rot=0) {
  structure(
    list(text=text,rot=rot),
    class = c("element_custom","element_blank", "element") # inheritance test workaround
  )
}
# returns a gTree with two children: the text label, and a rasterGrob below
element_grob.element_custom <- function(element,...)  {
  g2 <- rectGrob(gp=gpar(fill='red',alpha=0.5))
  g1 <- textGrob(element$text, x=0.5,vjust=0.5,rot  =element$rot)
  gTree(children=gList(g2,g1), cl = "custom_axis")
}
# gTrees don't know their size and ggplot would squash it, so give it room
 grobHeight.custom_axis = heightDetails.custom_axis = function(x, ...)
   unit(1, "lines")


ggplot(mtcars,aes(mpg,disp))+geom_point()+facet_grid(.~vs)+
  theme_bw() +
  theme(axis.title.x = my_axis('my custom x title'),
        axis.title.y = my_axis('my custom y title',rot=90))
于 2013-03-26T15:08:06.593 回答