如何将 ggplot2 中的数组与 R 中的文本一起绘制?

How to plot array in ggplot2 together with texts in R?

我为此苦苦挣扎。所以,如果有人告诉我解决方案,我将不胜感激。

我们可以使用以下代码绘制数组:

library(ggplot2)
a <- c(1,2,3,4,5)
qplot(seq_along(a), a)

我们还可以在 x 值上添加标签,如下所示:

names(a) <- c("a","b","c","d","e")

但是,我找不到如何在 x 轴上绘制数组中的变量及其名称。

您可以使用 scale_x_discrete.

a <- c(1,2,3,4,5)
names(a) <- c("a","b","c","d","e")
qplot(seq_along(a), a) + 
        scale_x_discrete(labels = names(a))