0

I am looking to plot the top 20 games in terms of their sales for 4 categories (EU, JP, NA and other) on the one plot.

I pulled the top 20 for each using the below code:

# 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)

i then tried to run the below block but it just seems to run the last plot:

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

x axis should be ranking and y axis should be sales

any ideas? Thanks in advance

plot

Gary
  • 39
  • 1
  • 5

2 Answers2

1

You can plot multiple scatterplots in the same window using the par() function.

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")

If you want to plot all of your series on the same plot, you can use the lines() and points() functions.

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")

Admittedly, this is a bit clunky in base R, but it does get the job done.

jalind
  • 491
  • 1
  • 5
  • 11
  • Hi Jalind, i was hoping to create one plot with 4 different colours, this generates 4 different plots. Is it possible to merge them do you know? – Gary Dec 13 '19 at 12:41
  • Hi Gary. I've updated my answer to allow for plotting multiple series on the same plot. – jalind Dec 13 '19 at 18:14
0

Having a snippet of data would be really helpful, anyway I'd go for dplyr and ggplot:

  1. Merge your data, have one row per game with 2 columns for index and sales.
> 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. Now you can plot your scatterplot using colour = yourvar or shape = yourvar in the aesthethics to distinguish different games.
library(dplyr)
library(ggplot2)
iris %>% 
    select(Species, Petal.Length, Sepal.Length) %>% 
    ggplot(aes(Petal.Length, Sepal.Length, colour = Species, shape = Species)) + 
    geom_point()

Which will give you something like this: enter image description here

anddt
  • 1,589
  • 1
  • 9
  • 26