【问题标题】:multiple plots in one RStudio一个 RStudio 中的多个绘图
【发布时间】:2020-04-06 10:19:43
【问题描述】:

我希望将 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 轴应该是销售额

有什么想法吗?提前致谢

【问题讨论】:

标签: r plot scatter-plot


【解决方案1】:

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

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 中有点笨拙,但它确实完成了工作。

【讨论】:

  • 嗨 Jalind,我希望创建一个具有 4 种不同颜色的绘图,这会生成 4 个不同的绘图。是否可以合并它们你知道吗?
  • 嗨,加里。我已经更新了我的答案,以允许在同一个情节上绘制多个系列。
【解决方案2】:

拥有一个 sn-p 数据真的很有帮助,无论如何我会选择 dplyrggplot

  1. 合并您的数据,每个游戏有一行,indexsales 有 2 列。
> 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()

这会给你这样的东西:

【讨论】:

    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多