Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating plot in ggplot2 from manipulated data

I was hoping someone could help me out here. I’m working on a college project and have manipulated a dataset to give me the figures I want, but I’m struggling to figure out how to turn this into a readable barplot with ggplot2.

The dataset I’m using is widely available here: https://www.kaggle.com/rush4ratio/video-game-sales-with-ratings

I have managed to manipulate the data in order to determine the European sales numbers of certain video games genres below using the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

vg_action <- (videogames %>% filter(Genre == "Action"))
vg_action_s <- sum(vg_action$EU_Sales)

vg_sports <- (videogames %>% filter(Genre == "Sports"))
vg_sports_s <- sum(vg_sports$EU_Sales)

vg_shooter <- (videogames %>% filter(Genre == "Shooter"))
vg_shooter_s <- sum(vg_shooter$EU_Sales)

vg_rpg <- (videogames %>% filter(Genre == "Role-Playing"))
vg_rpg_s <- sum(vg_rpg$EU_Sales)

vg_racing <- (videogames %>% filter(Genre == "Racing"))
vg_racing_s <- sum(vg_racing$EU_Sales)

Each returns a number corresponding to the sales for that genre (eg, vg_action_s returns 284.39, vg_sports_s returns 184.41 etc). videogames is the read in .csv file.

How would I best go about turning this into a barplot using ggplot2, where the x-axis is the genre names (Action, Sports, Racing etc), and the y axis is the value?

I’m very new to R (and coding in general) so I hope I’m explaining myself correctly. Any help would be very much appreciated!

Thanks

>Solution :

Using dplyr, you don’t need to make sums of each Genre one by one.

One thing weird is sum of Action or etc is not like you said. (Although use same code with yours)

If there’s any problem, please let me know.

library(dplyr)
library(ggplot2)

videogames %>%
  group_by(Genre) %>%
  summarise(m = sum(EU_Sales)) %>%
  filter(Genre %in% c("Action", "Sports", "Shooter", "Role-Playing", "Racing")) %>%
  ggplot(aes(x = Genre, y = m, fill = Genre)) +
  geom_col()

enter image description here

Or just using your vg…things can be done like

df <- data.frame(sums = c(vg_action_s, vg_sports_s, vg_shooter_s, vg_rpg_s, vg_racing_s),
                 Genre = c("Action", "Sports", "Shooter", "Role-Playing", "Racing"))
ggplot(df, aes(x = Genre, y = sums, fill = Genre)) +
  geom_col()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading