【问题标题】:Why is ggplot is turning my plot into a list为什么 ggplot 将我的情节变成列表
【发布时间】:2020-12-18 22:49:40
【问题描述】:

图像是 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(AvgPWill$Segment, AvgPWill$Values) 

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

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

【问题讨论】:

  • 或许可以试试plot(Pwill1)
  • 试试Pwill1 &lt;- ggplot(AvgPWill, aes(Values))+geom_histogram()
  • 仍然是一个使用 Pwill1 的列表
  • qplot(AvgPWill$Segment, AvgPWill$Values) 创建了一个图,但我似乎无法超越做这个散点图
  • 那行得通@Dave2e。我想将其标记为答案。感谢您对该答案和编辑的帮助。我对 Stack Overflow 还是比较陌生

标签: r list ggplot2 histogram


【解决方案1】:

是的,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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2015-11-03
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多