How to plot a comb?

2019-08-27 18:40发布

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)

enter image description here

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

标签: r ggplot2
1条回答
你好瞎i
2楼-- · 2019-08-27 19:32

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

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

enter image description here

查看更多
登录 后发表回答