【问题标题】:How do I make my axis ticks face Inwards in ggplot2如何让我的轴刻度在 ggplot2 中面向内
【发布时间】:2014-12-09 15:06:57
【问题描述】:

我使用 ggplot2 制作了一个条形图,我需要提交的期刊要求轴刻度面朝内。

这是我的数据(dput)的文本表示

Mean.Inc.melt<-structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Harvest","Pre-Harvest"), class = "factor"), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label =c("Dip A", "Trip A", "Trip B"), class = "factor"), value = c(2, 34, 1, 36, 3, 46)), .Names =c("Var1", "Var2", "value"), row.names = c(NA, -6L), class = "data.frame")

包括标准误差

SEM.Inc.melt<-structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Harvest", "Pre-Harvest"), class = "factor"), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Dip A", "Trip A", "Trip B"), class = "factor"), value = c(1, 12, 1, 2, 1, 6)), .Names = c("Var1", "Var2", "value"), row.names = c(NA, -6L), class = "data.frame")

这是我目前用来创建情节的脚本:

ggplot(Mean.Inc.melt,aes(x=Var2,y=value,fill=Var1))+
  geom_bar(stat='identity',position=position_dodge(),colour='black')+
  scale_fill_manual(values=c('#000000','#FFFFFF'))+
  geom_errorbar(aes(ymin=Mean.Inc.melt$value-SEM.Inc.melt$value,
                    ymax=Mean.Inc.melt$value+SEM.Inc.melt$value),width=.1,
                    position=position_dodge(.9))+
  xlab('Treatment')+
  ylab('Percentage Incidence (%)')+
  ylim(0,60)+
  scale_y_continuous(expand=c(0,0),limits=c(0,60))+
  scale_x_discrete(expand=c(0,0))+
  theme_bw()+
  theme(axis.line=element_line(colour='black'),panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),panel.border=element_blank(),
        panel.background=element_blank())+
  geom_vline(xintercept=0)+theme(legend.position='none')

我想重点是 - 有谁知道是否有办法让我的轴面向内?

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    虽然我不理解期刊希望在内部有刻度标记,但使用ggplot 实现这一点非常简单。

    themeaxis.ticks.length 参数允许您设置刻度线的长度。如果将其设置为负值,则会向内绘制刻度线。例如(转发 Dennis Murphy here 的解决方案):

    library(ggplot2)
    library(grid)
    ggplot(mtcars, aes(disp, mpg)) + geom_point() + 
      theme(axis.ticks.length=unit(-0.25, "cm"))
    

    请注意,该值应作为unit 对象传递,这需要加载grid 包(R 预装)。

    【讨论】:

    • 在 ggplot 2.0.0 中有一个 Warning message: 'axis.ticks.margin' is deprecated. Please set 'margin' property of 'axis.text' instead -- 似乎 axis.text = element_text(margin=5) 没有帮助......如果解决方案搜索到了,我会回复。
    • 所以,根据test-and-try的方法,你现在是这样做的:theme(axis.ticks.length=unit(-0.25, "cm"), axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")) )——不知何故它没有从axis.text继承设置,并且错误信息提示边距为长度为 4 的向量。
    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2017-07-18
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多