Cliff
Cliff

Reputation: 93

Printing an empty ggplot with a legend

I'm trying to print an empty ggplot, but still include a legend/scale and I'm running into problems.

Here's the relevant section of the code:

p <- ggplot(data=df, aes(x=factor(year_id), y=location_name)) + 

  geom_point(aes(size=count.sum), na.rm=F) +
  #geom_blank() +
  theme_bw() +
  theme(axis.text.x=element_text(angle=50, hjust=1)) +
  labs(x="Year", y="") +
  scale_size(name="Site-years of data", range=c(min(breaks),max(breaks)), breaks=breaks) + 
  ggtitle(paste0(cause_name, ", Data Availability")) +
  facet_wrap(~type) 

plots[[i]] <- p

Breaks is just the vector 1,2 and the column count.sum is all NA. What I want is to print out the chart, which has no data in it (no points), but still have a legend with a small dot representing 1 and a bigger dot representing 2.

As it is right now, I get the error "Discrete value supplied to continuous scale".

If I use geom_blank instead of geom_point, the chart successfully gets created and is empty, but there's no legend,

How can I get both the empty chart and a legend?

Thanks

Upvotes: 0

Views: 708

Answers (1)

baptiste
baptiste

Reputation: 77116

you could zoom somewhere outside the range of the data, ggplot(data.frame(x=1:10), aes(x,x,size=x)) + geom_point() + coord_cartesian(xlim=c(20,30))

Upvotes: 0

Related Questions