R 修剪ggplot2中的第一个和最后一个标签

R 修剪ggplot2中的第一个和最后一个标签,r,ggplot2,axis-labels,R,Ggplot2,Axis Labels,我有一个绘图,它按天将两种类型的数据制成表格,我希望只从绘图中修剪第一个和最后一个标签。以下是数据的可复制示例: library(dplyr) library(ggplot2) library(scales) dates <- paste0("2014-01-", 1:31) dat <- data.frame("Date" = sample(dates, 4918, replace=T), "Type" = sample(c('Type1',

我有一个绘图,它按天将两种类型的数据制成表格,我希望只从绘图中修剪第一个和最后一个标签。以下是数据的可复制示例:

library(dplyr)
library(ggplot2)
library(scales)
dates <- paste0("2014-01-", 1:31)
dat <- data.frame("Date" = sample(dates, 4918, replace=T), 
                  "Type" = sample(c('Type1', 'Type2'), 4918, replace=T, probs=c(.55, .45)))

p.data <- dat %>% group_by(Date, Type) %>% summarise(Freq = n())
p.data$Date <- as.Date(p.data$Date)
库(dplyr)
图书馆(GG2)
图书馆(比例尺)

日期scale\u x\u date
中的
expand
参数是一种方法。它试图通过在边缘周围留出一些额外的空间来提供帮助,但在这种情况下,它会增加一天以上的时间,因此轴标签具有这些额外的时间

p <- ggplot(data=p.data, aes(x=Date, y=Freq, fill=Type)) + 
              geom_bar(stat='identity', position='dodge') +
              labs(x='Date', y='Count', title='Frequency of Data by Day') + 
              theme_bw() + 
              theme(axis.text.x = element_text(angle=90),
                    panel.grid.minor = element_blank(),
                    plot.title = element_text(vjust=1.4),
                    legend.position='bottom') + 
              scale_x_date(labels=date_format("%a %d"), 
                           breaks=date_breaks("day"), 
                           limits=c(as.Date('2014-01-01'), as.Date('2014-01-31')),
                           expand=c(0, .9)) + 
              scale_y_continuous(limits=c(0, 150), breaks=seq(from=0, to=150, by=25)) + 
              scale_fill_manual(values=c('dark grey', 'light green'))
p
p <- ggplot(data=p.data, aes(x=Date, y=Freq, fill=Type)) + 
              geom_bar(stat='identity', position='dodge') +
              labs(x='Date', y='Count', title='Frequency of Data by Day') + 
              theme_bw() + 
              theme(axis.text.x = element_text(angle=90),
                    panel.grid.minor = element_blank(),
                    plot.title = element_text(vjust=1.4),
                    legend.position='bottom') + 
              scale_x_date(labels=date_format("%a %d"), 
                           breaks=date_breaks("day"), 
                           limits=c(as.Date('2014-01-01'), as.Date('2014-01-31')),
                           expand=c(0, .9)) + 
              scale_y_continuous(limits=c(0, 150), breaks=seq(from=0, to=150, by=25)) + 
              scale_fill_manual(values=c('dark grey', 'light green'))