一个 RStudio 中的多个图

multiple plots in one RStudio

我希望在同一张图中绘制 4 个类别(欧盟、日本、北美和其他)销量排名前 20 位的游戏。

我使用以下代码为每个人拉出了前 20 名:

# creating a scatterplot of the top 20 in each category
top20NA <- head(sort(games$NA_Sales,decreasing=TRUE), n = 20)
top20EU <- head(sort(games$EU_Sales,decreasing=TRUE), n = 20)
top20JP <- head(sort(games$JP_Sales,decreasing=TRUE), n = 20)
top20other <- head(sort(games$Other_Sales,decreasing=TRUE), n = 20)

然后我尝试 运行 下面的块,但它似乎 运行 最后一个情节:

plot(top20NA, col ="Blue")
plot(top20EU, col = "Black")
plot(top20JP, col = "Yellow")
plot(top20other, col = "Green")

x 轴应该是排名,y 轴应该是销量

有什么想法吗?提前致谢

您可以使用 par() 函数在同一个 window 中绘制多个散点图。

par(mfrow=c(2,2))
plot(top20NA, col ="Blue", ylab="Sales", xlab="Ranking")
plot(top20EU, col = "Black", ylab="Sales", xlab="Ranking")
plot(top20JP, col = "Yellow", ylab="Sales", xlab="Ranking")
plot(top20other, col = "Green", ylab="Sales", xlab="Ranking")

如果您想在同一张图上绘制所有系列,可以使用 lines() 和 points() 函数。

plot(top20NA, ylim = c(0,15), col = "Blue", type = "b",
 ylab="Sales", xlab="Ranking")
points(top20EU, col = "Black")
lines(top20EU, col = "Black")
points(top20JP, col = "Yellow")
lines(top20JP, col = "Yellow")
points(top20other, col = "Green")
lines(top20other, col = "Green")

不可否认,这在基础 R 中有点笨拙,但它确实完成了工作。

有一小段数据会很有帮助,无论如何我会选择 dplyrggplot:

  1. 合并您的数据,每场比赛有 2 列 indexsales.
> library(dplyr)
> iris %>% 
+     select(Species, Petal.Length, Sepal.Length) %>% 
+     head()
  Species Petal.Length Sepal.Length
1  setosa          1.4          5.1
2  setosa          1.4          4.9
3  setosa          1.3          4.7
4  setosa          1.5          4.6
5  setosa          1.4          5.0
6  setosa          1.7          5.4
  1. 现在您可以在美学中使用colour = yourvarshape = yourvar绘制您的散点图以区分不同的游戏。
library(dplyr)
library(ggplot2)
iris %>% 
    select(Species, Petal.Length, Sepal.Length) %>% 
    ggplot(aes(Petal.Length, Sepal.Length, colour = Species, shape = Species)) + 
    geom_point()

这会给你这样的东西: