简体   繁体   English

用 R 绘制一段时间内的销售额

[英]Plotting sales over time in R

I am trying to show the top 100 sales on a scatterplot by year.我试图在散点图上按年份显示前 100 名的销售额。 I used the below code to take top 100 games according to sales and then set it as a data frame.我使用下面的代码根据销量取前 100 款游戏,然后将其设置为数据框。

top100 <- head(sort(games$NA_Sales,decreasing=TRUE), n = 100)
as.data.frame(top100)

I then tried to plot this with the below code:然后我尝试使用以下代码绘制它:

ggplot(top100)+
  aes(x=Year, y = Global_Sales) +
    geom_point()

I bet the below error when using the subset top100 Error: data must be a data frame, or other object coercible by fortify() , not a numeric vector我打赌在使用子集 top100 错误时会出现以下错误: data必须是数据框,或由fortify()强制的其他对象,而不是数字向量

if i use the actual games dataseti get the plot attached.如果我使用实际的游戏数据集,我会附上图。

Any ideas?有任何想法吗?

阴谋

As pointed out in comments by @CMichael, you have several issues in your code.正如@CMichael 在评论中指出的那样,您的代码中有几个问题。 In absence of reproducible example, I used iris dataset to explain you what is wrong with your code.在没有可重现的示例的情况下,我使用iris数据集向您解释您的代码有什么问题。

top100 <- head(sort(games$NA_Sales,decreasing=TRUE), n = 100) top100 <- 头(排序(游戏$NA_Sales,递减=真),n = 100)

By doing that you are only extracting a single column.通过这样做,您只提取一列。

The same command with the iris dataset:iris数据集相同的命令:

> head(sort(iris$Sepal.Length, decreasing = TRUE), n = 20)
 [1] 7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 7.2 7.1 7.0 6.9 6.9 6.9 6.9 6.8 6.8 6.8

So, first, you do not have anymore two dimensions to be plot in your ggplot2 .因此,首先,您不再需要在ggplot2绘制两个维度。 Second, even colnames are not kept during the extraction, so you can't after ask for ggplot2 to plot Year and Global_Sales .其次,即使colnames不提取过程中保持,所以你不能后索要ggplot2绘制YearGlobal_Sales

So, to solve your issue, you can do (here the example with the iris dataset):因此,要解决您的问题,您可以执行以下操作(这里是iris数据集的示例):

top100 = as.data.frame(head(iris[order(iris$Sepal.Length, decreasing = TRUE), 1:2], n = 100))

And you get a data.frame of of this type:你会得到一个这种类型的 data.frame:

> str(top100)
'data.frame':   100 obs. of  2 variables:
 $ Sepal.Length: num  7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 ...
 $ Sepal.Width : num  3.8 3.8 2.6 2.8 3 3 2.8 2.9 3.6 3.2 ...

> head(top100)
    Sepal.Length Sepal.Width
132          7.9         3.8
118          7.7         3.8
119          7.7         2.6
123          7.7         2.8
136          7.7         3.0
106          7.6         3.0

And then if you are plotting:然后,如果您正在绘图:

library(ggplot2)
ggplot(top100, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()

在此处输入图片说明

Warning Based on what you provided in your example, I will suggest you to do:警告根据您在示例中提供的内容,我建议您执行以下操作:

top100 <- as.data.frame(head(games[order(games$NA_Sales,decreasing=TRUE),c("Year","Global_Sales")], 100))

However, if this is not satisfying to you, you should consider to provide a reproducible example of your dataset How to make a great R reproducible example但是,如果这对您不满意,您应该考虑提供数据集的可重现示例How to make a great R reproducible example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM