ggplot geom_boxplot颜色和组变量

时间:2019-03-27 18:26:11

标签: r ggplot2 colors

我正在尝试在ggplot中制作一个简单的箱线图。我不确定如何获得分组变量和颜色/填充变量。我已尝试收集,但这似乎没有用。有什么想法吗?

 // POST: OrderManagement/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)
    {
        if (ModelState.IsValid)
        {
            db.Entry(iCS_Transactions).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(iCS_Transactions);
    }

2 个答案:

答案 0 :(得分:1)

我不确定这是否是您想要的输出(gear作为x轴和fill),但这是一个有效的示例:

mtcars %>%
  ggplot(
    aes(
      x = factor(gear),
      y = mpg,
      color = factor(vs),
      fill = factor(gear)
    )
  ) + geom_boxplot()

我发现声明自己的美学映射对学习ggplot2很有帮助。

答案 1 :(得分:0)

或者:

mtcars %>% 
  as_tibble() %>% 
  group_by(vs) %>% 
  ggplot(aes(factor(gear), 
             mpg, 
             fill=as.factor(gear))) +
  geom_boxplot()