如何让我的轴在ggplot2中面向内部

Mis*_*lee 16 r ggplot2 axis-labels

我使用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")
Run Code Online (Sandbox Code Playgroud)

包括标准错误

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")
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止用来创建剧情的剧本:

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')
Run Code Online (Sandbox Code Playgroud)

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

jba*_*ums 17

虽然我不理解期刊在内部有刻度标记的愿望,但实现这一目标非常简单ggplot.

允许您设置刻度线长度的axis.ticks.length参数theme.如果将其设置为负值,则将向内绘制刻度线.例如(Dennis Murphy 在这里重新提交解决方案):

library(ggplot2)
library(grid)
ggplot(mtcars, aes(disp, mpg)) + geom_point() + 
  theme(axis.ticks.length=unit(-0.25, "cm"), axis.ticks.margin=unit(0.5, "cm"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如上例所示,您还需要调整刻度标签的位置axis.ticks.margin.

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

  • 所以,根据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`继承设置,并且错误消息提示margin是长度为4的向量. (6认同)
  • 在 ggplot 2.0.0 有一个`警告消息:'axis.ticks.margin' 已被弃用。请改为设置“axis.text”的“margin”属性——似乎“axis.text = element_text(margin=5)”没有帮助......如果解决方案在谷歌上搜索到,我会回帖。 (2认同)