在缩放时,在R ggplot2中保持刻度标记附加到我的轴上

时间:2015-07-28 14:30:11

标签: r ggplot2 grid

我想从RStudio以更大的分辨率导出此图形,但是当我这样做时,x轴的刻度线与轴分开。 我查看了theme()的文档,但没有找到相关的函数:http://docs.ggplot2.org/0.9.3/theme.html

那么如何让我的刻度线连接到x轴? 我尝试使用刻度线和轴标签之间的空格,但它只是改变了相关的间距,但没有改变我所说的间距。

生成我在下面描述的图像的代码:

require(data.table)
require(ggplot2)
require(grid)

setsTimeline <- data.table(Set=c("Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set"),
       StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
       EndDate=  c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))

setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
setkey(setsTimeline,StartDate)

breaks <- c(1380708900,1395100800,1402963200,1410912000,1418860800,1430985600)
labels <- as.POSIXct(breaks,tz="UTC",origin="1970-01-01")

ggplot(setsTimeline, aes(colour=Set)) + 
  geom_segment(aes(x=StartDate, xend=EndDate, y="group", yend="group"), size=10) +
  theme(panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.background = element_blank(),
    aspect.ratio=5e-02, 
    axis.text.x = element_text(colour='black', angle = 45, size = 16, hjust = 1, vjust = 1),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    legend.text = element_text(colour='black', size = 16),
    legend.title = element_text(colour='black', size = 0),
    legend.position = 'top',
    plot.title = element_text(colour='black', size = 18),
    plot.margin = unit(c(0,1,0,3.5), "cm")) +
  xlab(NULL) + 
  ylab(NULL) +
  ggtitle(NULL) +
  coord_cartesian(xlim = c(1380708900,1430985600), ylim = NULL) +
  scale_x_continuous(breaks=breaks,labels=labels)

小分辨率: No spacing between axis and ticks

高分辨率: Suddenly spacing between axis and ticks :o

0 个答案:

没有答案