如何使用条形图获得可靠的刻度刻度,这些条形图将数字汇总到 ggplot (R) 中的单个条形中?
How do I get reliable scale ticks with bar plots that sum up numbers into single bars in ggplot (R)?
我有一个简单的 ggplot 条形图,它显示有关学校费用的信息。
它从具有以下列的数据框中检索信息:
- 购买地点(有两个经常出现的地点)
- 购买金额是多少美元
您可以在本post(csv 格式)末尾仔细查看此数据。
我图中的每个条代表不同的购买地点。条形为每次购买(与其金额成比例)堆叠多种颜色。这是我的情节:
如您所见,缩放比例明显偏离(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")
问题似乎是文件的最后一行。每列结尾的字符串 "<++>"
弄乱了数字列 MONTANT
。这里有一个解决方法。
- 将列
MONTANT
强制转换为数字;
- 不能为数字的矢量元素变为
NA
,并带有警告 "NAs introduced by coercion"
;
- 删除带有
!is.na(.)
的那些行。
代码如下。
expenses$MONTANT <- as.numeric(expenses$MONTANT)
expenses <- expenses[!is.na(expenses$MONTANT), ]
现在将日期列强制为 class "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))
我有一个简单的 ggplot 条形图,它显示有关学校费用的信息。 它从具有以下列的数据框中检索信息:
- 购买地点(有两个经常出现的地点)
- 购买金额是多少美元
您可以在本post(csv 格式)末尾仔细查看此数据。
我图中的每个条代表不同的购买地点。条形为每次购买(与其金额成比例)堆叠多种颜色。这是我的情节:
如您所见,缩放比例明显偏离(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")
问题似乎是文件的最后一行。每列结尾的字符串 "<++>"
弄乱了数字列 MONTANT
。这里有一个解决方法。
- 将列
MONTANT
强制转换为数字; - 不能为数字的矢量元素变为
NA
,并带有警告"NAs introduced by coercion"
; - 删除带有
!is.na(.)
的那些行。
代码如下。
expenses$MONTANT <- as.numeric(expenses$MONTANT)
expenses <- expenses[!is.na(expenses$MONTANT), ]
现在将日期列强制为 class "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))