我有一个数据集,其中 x 轴标签不唯一且非数字。这是一个示例:
0 )
0 )
0 .
0 )
1 )
420 )
474 )
518 )
567 )
580 )
当我使用以下代码使用 ggplot2 绘制此图时:
ggplot(data=Figure3b, aes(x=Bracket, y=Counts)) + geom_line()
我明白了:
而我追求的是这样的东西:
据我所知,它以某种方式将所有值与相同的 x 轴标签分组,而我希望它只是按顺序绘制点。
最佳答案
为括号分配一个索引,然后绘制
dat$index <- 1:nrow(dat)
ggplot(dat, aes(index, x)) + geom_line(lwd=2, col="blue") +
scale_x_discrete(labels=dat$y) + xlab("brackets")
数据如下:
> head(dat)
# x y index
# 1 0 ) 1
# 2 0 . 2
# 3 0 ) 3
# 4 1 ) 4
# 5 420 ) 5
# 6 474 ) 6
关于r - 如何在ggplot2中绘制带有重复x轴标签的折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515519/