代码之家  ›  专栏  ›  技术社区  ›  Benjamin Chausse

如何在ggplot(R)中将数字相加成单个条的条形图中获得可靠的刻度?

  •  0
  • Benjamin Chausse  · 技术社区  · 5 年前

    我有一个简单的ggplot栏,它显示了有关学校开支的信息。 它从包含以下列的数据框中检索其信息:

    • 购买地点(有两个经常性地点)
    • 购买金额是多少美元

    您可以在本文末尾(csv格式)仔细查看这些数据。

    我的图中的每个条代表不同的购买位置。每购买一件商品(与数量成比例),这些条会堆叠多种颜色。下面是我的情节:

    Example plot

    如您所见,缩放比例已明显关闭(10.28刻度约为y轴上215.25刻度的三分之一高)。

    我应该如何使缩放精确?是什么导致y轴不精确?

    这是我的原始csv文件:

    "DATE"      ;"MONTANT";"LIEU"                      ;"CAUSE"
    "2020-01-25";    67.17;"Coop Cégep"                ;"Notes de cours"
    "2020-02-24";     7.67;"Coop Cégep"                ;"Notes de cours"
    "2020-01-30";    10.28;"Coop Cégep"                ;"Cahiers d'exercices"
    "2020-03-02";   215.25;"Omnivox (Cégep Lanaudière)";"Frais de scholarité"
    "2020-01-22";   114.60;"Coop Cégep"                ;"Romans, Notes de cours"
    "2020-08-27";    78.33;"Coop Cégep"                ;"Romans, Notes de cours"
    "<++>"      ;     <++>;"<++>"                      ;"<++>"
    

    以下是我用来生成此图像的代码:

    #!/bin/Rscript
    
    # LIBRARIES ----
    
    library(ggplot2)
    library(RColorBrewer)
    
    # CSV's ----
    
    expenses <- head(data.frame(read.csv("paiements.csv", header=TRUE, sep=";")), -1)
    expenses$DATE  <- as.Date(expenses$DATE)
    
    # PLOTS ----
    
    # Bar plot with different expenses sorted by location
    expenses_df <- ggplot(expenses, aes(LIEU, MONTANT, fill=MONTANT)) +
        geom_bar(stat="identity") +
        geom_jitter(width=0.1, height=0, shape=18, size=4) +
        labs(
                 title="Montants de diverses dépenses scholaires",
                 x="Lieu",
                 y="Montant") +
        theme(plot.title = element_text(hjust=0.5))
    
    # JPEG ----
    
    jpeg(
            file="paiements.jpg",
    )
    
    print(expenses_df)
    
    dev.off()
    

    数据输入 dput 格式

    expenses <-
    structure(list(DATE = c("2020-01-25", "2020-02-24", "2020-01-30", 
    "2020-03-02", "2020-01-22", "2020-08-27"), MONTANT = c(67.17, 
    7.67, 10.28, 215.25, 114.6, 78.33), LIEU = c("Coop Cégep", "Coop Cégep", 
    "Coop Cégep", "Omnivox (Cégep Lanaudière)", "Coop Cégep", 
    "Coop Cégep"), CAUSE = c("Notes de cours", "Notes de cours", 
    "Cahiers d'exercices", "Frais de scholarité", "Romans, Notes de cours", 
    "Romans, Notes de cours")), row.names = c(NA, -6L), class = "data.frame")
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   Rui Barradas    5 年前

    问题似乎是最后一行文件。字符串 "<++>" 结束每一列就是搞乱数字列 MONTANT . 这里有一个解决方法。

    1. 强制列 蒙坦特
    2. 不能是数字的矢量元素将变为 NA ,带有警告 "NAs introduced by coercion" ;
    3. 删除带有 !is.na(.)

    代码如下。

    expenses$MONTANT <- as.numeric(expenses$MONTANT)
    expenses <- expenses[!is.na(expenses$MONTANT), ]
    

    现在强制日期列为类 "Date" CAUSE 定义他们的颜色。

    expenses$DATE  <- as.Date(expenses$DATE)
    
    library(ggplot2)
    
    ggplot(expenses, aes(LIEU, MONTANT, fill = CAUSE)) +
      geom_bar(stat="identity") +
      geom_jitter(width=0.1, height=0, shape=18, size=4) +
      labs(
        title="Montants de diverses dépenses scholaires",
        x="Lieu",
        y="Montant") +
      theme(plot.title = element_text(hjust=0.5))
    

    enter image description here