为什么 ggplot 将我的情节变成列表

Why is ggplot is turning my plot into a list

图像有 5 行和两个我想要绘制的变量,下面是我正在使用的代码。

第二张图显示结果

#histogram plot
Pwill1 <- ggplot(AvgPWill, aes(Segment))+geom_histogram()

#Structure of AvgPWill
str(AvgPWill)
'data.frame':   5 obs. of  2 variables:
 $ Segment: chr  "Costcutter" "Innovator" "Mercedes" "Workhorse" ...
 $ Values : num  2084 3595 4695 2994 3422

我不熟悉 plot 函数,但我尝试了这个并收到了这个错误:

plot(AvgPWill$Segment, AvgPWill$Values) 

plot.window(...) 中的错误:需要有限的 'xlim' 个值

另外:警告信息:
1:在 xy.coords(x, y, xlabel, ylabel, log) 中:强制引入的 NAs
2:在 min(x) 中:min 没有非缺失参数;返回 Inf
3:在 max(x) 中:max 没有非缺失参数;返回 -Inf

是的,ggplot 函数的输出是一个包含绘图结构的列表。要显示绘图,请使用:print(Pwill1).

此外,由于您只有 5 行数据,我相信您想使用 geom_col() 而不是 geom_histogram()

Values= runif(5, 2000, 3000)
AvgPWill = data.frame(Segment=LETTERS[1:5], Values)

library(ggplot2)
Pwill1 <- ggplot(AvgPWill, aes(x=Segment, y=Values))+geom_col()
print(Pwill1)

如果您想使用基本图形,请尝试 barplot()