How to plot a comb?

2019-08-27 18:53发布

问题:

This type of plot might have a different name, if so kindly point me to a relevant post.

I am trying to plot simple "comb" plot using geom_segment():

require(ggplot2)

#dummy data
set.seed(1)
dat <- data.frame(x=sample(1:100,20))

#plot comb
ggplot(data=dat,
       aes(x=x,y=0,xend=x, yend=10)) +
  geom_segment() +
  theme(panel.grid.major.x=element_blank(),
        panel.grid.minor.x=element_blank(),
        axis.title.x=element_blank()) +
  #hide y ticks and label
  scale_y_continuous(breaks=NULL) +
  ylab(NULL)

It seems, so many lines of code for a simple plot, is there other ggplot function I am missing?

回答1:

Dunno about ggplot, but why not use graphics::rug ?

set.seed(1)
dat <- sample(1:100,20)
plot(dat,dat,t='n')
rug(dat)



标签: r ggplot2