enter image description hereI have a dataset with 7 columns in which I need to create a function such that -> it takes one column as a parameter to create a barchart of the top ten values in that column -> with having a certain clause comparing with third column
considering sample data as the image
material as the parameter for the function and speed determines top 10 values, where material is wodden.
I just need to create the function, I know how to create the barplot and it works fine
example data head(subset_coaster)
dput(subset_coaster[1:30, c("coaster_Data.name","coaster_Data.material_type","coaster_Data.speed")])
structure(list(coaster_Data.name = c("Goudurix", "Dream catcher",
"Alucinakis", "Anaconda", "Azteka", "Bat Coaster", "Batman : Arkham Asylum",
"Big Thunder Mountain", "EqWalizer", "Calamity Mine", "Casey Jr, le Petit Train du Cirque",
"Cobra", "Coccinelle", "Coleoz'Arbres", "Comet", "Course de Bobsleigh",
"Cumbres", "Le Dragon de Bei Hai", "Euro Mir", "Eurosat", "Expedition Ge Force",
"Le Grand canyon", "Indiana Jones et le Temple du Péril", "Jaguar",
"Cop Car Chase (1)", "Loup Garou", "Magnus Colossus", "Oki Doki",
"SOS Numerobis", "Poseïdon"), coaster_Data.material_type = structure(c(1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L), levels = c("Steel",
"Wooden", "Hybrid", "na"), class = "factor"), coaster_Data.speed = c(75,
45, 30, 85, 55, 70, 80, 60, 76, 48, 30, 76, 36, 60, 64, 65, NA,
NA, 80, 60, 120, 50, 58, 83, 60, 80, 92, 58, 32, 70)), row.names = c(NA,
30L), class = "data.frame")
CodePudding user response:
From the description of your post, it is still unclear what you want from the function. Thus, here is my approach to what I think you want.
df <- structure(list(coaster_Data.name = c("Goudurix", "Dream catcher",
"Alucinakis", "Anaconda", "Azteka", "Bat Coaster", "Batman : Arkham Asylum",
"Big Thunder Mountain", "EqWalizer", "Calamity Mine", "Casey Jr, le Petit Train du Cirque",
"Cobra", "Coccinelle", "Coleoz'Arbres", "Comet", "Course de Bobsleigh",
"Cumbres", "Le Dragon de Bei Hai", "Euro Mir", "Eurosat", "Expedition Ge Force",
"Le Grand canyon", "Indiana Jones et le Temple du Péril", "Jaguar",
"Cop Car Chase (1)", "Loup Garou", "Magnus Colossus", "Oki Doki",
"SOS Numerobis", "Poseïdon"),
coaster_Data.material_type = structure(c(1L,1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L),
levels = c("Steel", "Wooden", "Hybrid", "na"),
class = "factor"),
coaster_Data.speed = c(75, 45, 30, 85, 55, 70, 80, 60, 76, 48, 30, 76, 36, 60, 64, 65, NA,
NA, 80, 60, 120, 50, 58, 83, 60, 80, 92, 58, 32, 70)),
row.names = c(NA, 30L), class = "data.frame")
barplotting <- function(data, mat= "Steel"){
require(tidyverse)
data %>%
filter(coaster_Data.material_type == mat) %>%
arrange(desc(coaster_Data.speed)) %>%
slice(1:10) %>%
ggplot(aes(x= coaster_Data.name, y= coaster_Data.speed))
geom_col()
}
barplotting(df, "Wooden")
#> Loading required package: tidyverse
Created on 2023-01-11 with reprex v2.0.2