使用R中的qplot生成矢量条形图

1 r ggplot2

我的载体如下:

x = c(1:10)
y = c(1, 8, 87, 43, 67, 22, 99, 14, 75, 56)
Run Code Online (Sandbox Code Playgroud)

我想生成一个条形图,其中x轴标记为1-10,y轴是上面y向量中每个值的高度.我尝试了几个与此类似的命令:

qplot(x, y, geom= "bar")
Run Code Online (Sandbox Code Playgroud)

这会导致错误

Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
Run Code Online (Sandbox Code Playgroud)

所以,我尝试了这条消息中的两条建议.第一:

qplot(x, stat="bin", geom= "bar")
Run Code Online (Sandbox Code Playgroud)

但是这导致了一个图表,其中所有10个柱都是高度为1的.第二:

qplot(x, stat="identity", geom= "bar")
Run Code Online (Sandbox Code Playgroud)

但这会导致错误:as.environment(where)中的错误:'where'缺失

作为一个附带问题,我想让每个栏都不同(或至少是随机颜色).这有点直截了当吗?

Ana*_*nta 5

有什么理由使用qplot?ggplot提供了更大的灵活性,尽管在这个简单的情况下不需要.

x = c(1:10)
y = c(1, 8, 87, 43, 67, 22, 99, 14, 75, 56)
df <- data.frame(x,y)
library(ggplot2)
ggplot(df, aes(x, y, fill = as.factor(x))) + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述