简体   繁体   中英

Using geom_vline with facet_wrap

I've got a line plot with facets in it using the following code:

vLines <- data.frame('Date'=as.Date('2014/1/1'))

p <- ggplot(toPlot[1:296,], aes( Date, value)) + theme_bw() +ylab('Transactions') + xlab('') +
  scale_x_date(breaks=x_breaks, labels=x_labels)
p <- p + geom_line(aes(colour = variable, fill= variable),size=1.5) +
  theme(axis.text.y=element_text(hjust=0, angle=0), 
        axis.text.x = element_text(hjust=1, angle=45),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x=element_line(color='grey90',linetype='dashed'),
        panel.grid.major.y=element_line(color='grey90',linetype='dashed'),
        plot.title=element_text(size=20),
        axis.text=element_text(size=10),
        legend.text=element_blank(),
        legend.key=element_blank(),
        legend.title=element_blank(),
        legend.position="none") +
  scale_y_continuous(label=thousand_formatter) +
  ggtitle('Title')+
  scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6")) +
  facet_wrap(~OrderType,nrow=3,scales="free") +
  geom_vline(data=vLines,aes(xintercept=Date))
p

Note that I'm trying to only plot one vertical line on each facet, I would like to plot multiple vertical lines on each facet.

The output of dput(head(toPlot)) is:

structure(list(Date = structure(c(14853, 14914, 15034, 15187, 
15309, 15340), class = "Date"), OrderType = structure(c(1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Delivery", "eFiling", "Filing", 
"ProcessServing", "Research"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Orders", "Revenue"), class = "factor"), 
    value = c(1, 1, 1, 1, 18, 37)), .Names = c("Date", "OrderType", 
"variable", "value"), row.names = c(NA, 6L), class = "data.frame")

When I try to execute the aforementioned code, I get the following error:

Error: Discrete value supplied to continuous scale

Any pointers on solving this would be highly appreciated.

geom_vline位更改为:

geom_vline(xintercept=as.numeric(vLines$Date))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM