R语言 我想创建一个以列为参数的函数,以便在与第三列值进行比较时生成前10个值的条形图

qmelpv7a  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(63)

enter image description here我有一个包含7列的数据集,需要在其中创建一个函数,使得-〉它将一列作为参数,以创建该列中前十个值的条形图-〉具有与第三列进行比较的特定子句
将样本数据视为图像
作为函数和速度参数的材料确定前10个值,其中材料为wodden。
我只需要创建函数,我知道如何创建条形图,它工作正常
示例数据头(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")
e0bqpujr

e0bqpujr1#

从你帖子的描述来看,你还不清楚你想从这个功能中得到什么。因此,下面是我认为你想要的方法。

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

创建于2023年1月11日,使用reprex v2.0.2

相关问题