首页 > 解决方案 > Why is ggplot is turning my plot into a list

问题描述

Dataframe wanting to plot

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
List of AvgPwill

#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

标签: rlistggplot2histogram

解决方案


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


推荐阅读