【问题标题】:Making multiple bar graphs using gather in R在 R 中使用收集制作多个条形图
【发布时间】:2019-11-19 00:06:14
【问题描述】:

我有名为 Category、BottSold 和 County 的变量。 基本上我想使用gather和ggplot函数一次制作多个条形图。条形图将是 County vs. BottSold,但每个图表只会考虑不同的类别值。类别具有伏特加、杜松子酒、威士忌等值。基本上,输出将是不同县与 Bottsold 的威士忌、伏特加酒、杜松子酒等的图表。到目前为止,这是我尝试做的事情:

Iowa_Gather <- gather(Iowa_Liqour, Category, County, -BottSold) 
head(Iowa_Gather)

【问题讨论】:

  • gather 函数来自哪里?据我所知,将宽数据转换为长数据是在tidyr packge中,因为tidyr 1.0.0来了,你应该改用pivot_longer。您需要提供您的数据样本以供我们处理。
  • 这可能会有所帮助:stackoverflow.com/questions/4877357/…
  • 您能否提供更多关于 Iowa_Liquor 等来自何处的信息?
  • 它来自爱荷华州。
  • 您好,要提供您的数据样本,请使用函数dput(YOURDATA)。如果您的数据很长,那么类似于:dput(head(YOURDATA)) 如果它是一个数据框。我认为您可能在ggplot2 中寻找的功能是facet_gridfacet_wrap

标签: r ggplot2 tidyr


【解决方案1】:

我认为这就是您所描述的。

library(tidyverse)
category <- c("Vodka", "Gin", "Whiskey", "Vodka", "Gin", "Whiskey", "Vodka", "Gin", "Whiskey")
county <- c("1", "1", "1", "2", "2", "2", "3", "3", "3")
bott_sold <- sample(20, 9)

df <- as.data.frame(cbind(category, county, bott_sold)) %>%
  mutate(bott_sold = as.numeric(as.character(bott_sold)))

ggplot(df) +
  facet_wrap(category ~ .) +
  geom_bar(aes(x = county, y = bott_sold), stat = "identity")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2015-11-23
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多