【问题标题】:R: combine mpg trans columns into new dataframe containing two columnsR:将 mpg trans 列组合成包含两列的新数据框
【发布时间】:2016-10-07 09:35:35
【问题描述】:

我正在通过R for Data Science Manual 工作,目前正在完成第 3 章。我正在尝试找到一种方法来生成一个将不同类型的自动和手动变速器组合成两个图的图,而不是我目前的图:

# Install necessary packages
install.packages("tidyverse")
library(tidyverse)

# Create the plot
fuelbytrans <- ggplot(data = mpg) +
  geom_jitter(
    mapping = aes(x = displ, y = hwy, colour = fl),
    size = 0.75) +
  # Change labels for title and x and y axes
  labs(
    title = "Drivstofforbruk iht. datasettet «mpg» fordelt på girkasse og motorvolum",
    x = "Motorvolum",
    y = "Am. mil per gallon")

# Run it
fuelbytrans



# Set colours and labels for fuel legend and position it on the bottom
# e (etanol), d (diesel), r (regular [bensin, lavoktan]), p (premium [bensin, høyoktan]),
# c (CNG)
cols <- c( #kilde: http://colorbrewer2.org/#type=diverging&scheme=PRGn&n=5
  "c" = "yellow",
  "d" = "red",
  "e" = "black",
  "p" = "blue",
  "r" = "darkgreen"
)
labels_fuel <- fuelbytrans +
scale_colour_manual(
    name = "Drivstoff",
    values = cols,
    breaks = c("c", "d", "e", "p", "r"),
    labels = c("CNG",
               "diesel",
               "etanol",
               "bensin,\nhøyoktan",
               "bensin,\nlavoktan")) +
  theme(legend.position = "bottom",
        legend.background = element_rect(
          fill = "gray90",
          size = 2,
          linetype = "dotted"
        ))

# Run it
labels_fuel



# Wrap by transmission type
labels_fuel + facet_wrap(~ trans, nrow = 1)

如您所见,我得到的是 8 列自动变速箱和 2 列手动变速箱;我想要的只是两列,一列用于自动,另一列用于手动,连接图。我目前不知道如何做到这一点,希望得到所有帮助。

如果有任何信息缺失、应该以不同方式书写或可以改进,请告知。

我正在运行 RStudio 0.99.902。我对 R 很陌生。

【问题讨论】:

    标签: r dataframe concatenation facet-wrap


    【解决方案1】:

    您的数据中有两种以上的传输类型:

    table(mpg$trans)
    
    # auto(av)   auto(l3)   auto(l4)   auto(l5)   auto(l6) 
    #        5          2         83         39          6 
    # auto(s4)   auto(s5)   auto(s6) manual(m5) manual(m6) 
    #        3          3         16         58         19
    

    您需要先将它们分成两组,这里有一个选项:

    mpg = mpg %>% 
      mutate(trans2 = if_else(grepl("auto", trans), "auto", "manual"))
    
    table(mpg$trans2)
    
    # auto manual 
    # 157     77
    

    然后,使用新的trans2 变量进行分面(您需要重新运行绘图)。

    另外两个cmets:

    1. 如果您想了解有关 R 函数的更多信息,请在 R 中调用 ?function_name。这将打开该函数的帮助页面。它通常包含一些示例,您可以从 R 中运行这些示例来查看它的实际作用。 (此外,我们在这里使用grepl,因此如果您不熟悉“正则表达式”一词,Google 也会很有用)。

    2. 由于您正在阅读 r4ds,因此您需要尽快熟悉 dplyrtidyr 和其他 tidyverse 软件包中使用的“管道运算符”。它可以以一种易于阅读的方式将多个函数调用链接在一起。谷歌它或看看here。也可以像这样在没有管道的情况下编写调用:

      mpg = mutate(mpg, trans2 = if_else(grepl("auto", trans), "auto", "manual"))
      

    在这种特殊情况下,管道运算符实际上并没有那么有用。我只是太习惯了,我就自动去了。

    【讨论】:

    • 您能否解释一下 mutate 函数的工作原理,特别是以下内容: • 环绕 > 的 %s 有什么作用? • grepl 做什么?非常感谢!
    • 哦,它就像一个魅力!再次,非常感谢!
    • @CannedMan 查看我的更新答案。此外,如果这回答了您的问题,最好“接受”(通过单击复选标记),以便通过 Google 搜索该问题的其他人可以看到该问题已得到解答。
    • 谢谢你,很抱歉耽搁了,我已经接受你的回答并支持它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2018-08-31
    相关资源
    最近更新 更多