r - Why is ggplot is turning my plot into a list
问题描述
The image is 5 rows and two variables I am wanting to plot and below is my code I am using.
The second image shows the results
#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
I am not familiar with the plot function but I tried this and received this error:
plot(AvgPWill$Segment, AvgPWill$Values)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -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()
推荐阅读
- c# - 如何使用 OpenXmlAttribute 更改字体大小?
- algorithm - 子串部分数量受限的最长公共子序列
- javascript - 如何使用纯javascript从spritesheet中提取精灵
- javascript - 将 Input 标签值发送到 React.js 中的另一个组件
- google-cloud-firestore - 我想做一个 Tinder 替代品。我认为我的匹配机制会占用太多的数据库空间。如何改进它?
- postman - 邮递员:请求失败,CORS:ORIGIN_NOT_ALLOWED
- javascript - Mui v5 styleOverrides 不适用于主题
- localization - Cakephp2 本地化 - 无法更改本地化文件夹名称
- python - 如何在类型化属性的类定义中使 pycharm 的自动完成工作?
- python - 使用 python 将测试发布到 API