簡體   English   中英

在 R 中使用收集制作多個條形圖

[英]Making multiple bar graphs using gather in R

我有名為 Category、BottSold 和 County 的變量。 基本上我想使用gather和ggplot函數一次制作多個條形圖。 條形圖將是 County vs. BottSold,但每個圖表只會考慮不同的類別值。 類別具有伏特加、杜松子酒、威士忌等值。 基本上,output 將是不同縣與 Bottsold 的威士忌圖,一個用於伏特加,一個用於杜松子酒,依此類推。 到目前為止,這是我嘗試做的事情:

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

我想這就是你所描述的。

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")

iowa_liquor_ggplot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM