在下面的堆叠条中,我想让条的线条变粗,以区分不同的组。我使用了 size 但它不起作用。
Cum<-structure(list(Age.group = c("00-04", "00-04", "05-14", "05-14",
"15-24", "15-24", "25-49", "25-49", "50-64", "50-64", "65-79",
"65-79", "80+", "80+"), Gender = c("Female", "Male", "Female",
"Male", "Female", "Male", "Female", "Male", "Female", "Male",
"Female", "Male", "Female", "Male"), Cases = c(64578, 70518,
187568, 197015, 414405, 388138, 1342394, 1206168, 792180, 742744,
400232, 414613, 282268, 198026), lab = c("64,578", "70,518",
"187,568", "197,015", "414,405", "388,138", "1,342,394", "1,206,168",
"792,180", "742,744", "400,232", "414,613", "282,268", "198,026"
), Age.group.Sum = c(135096, 135096, 384583, 384583, 802543,
802543, 2548562, 2548562, 1534924, 1534924, 814845, 814845, 480294,
480294), lab2 = c("135,096", "135,096", "384,583", "384,583",
"802,543", "802,543", "2,548,562", "2,548,562", "1,534,924",
"1,534,924", "814,845", "814,845", "480,294", "480,294"), color = c("#4285f4",
"#4285f4", "#90a9e0", "#90a9e0", "#dd9e5f", "#dd9e5f", "#b45f06",
"#b45f06", "#b45f06", "#b45f06", "#dd9e5f", "#dd9e5f", "#aebbd6",
"#90a9e0"), Range = c("LESS THAN 74.5K", "LESS THAN 74.5K", "148.9K - 223.4K",
"148.9K - 223.4K", "372.3K - 446.7K", "372.3K - 446.7K", "MORE THAN 670.1K",
"MORE THAN 670.1K", "MORE THAN 670.1K", "MORE THAN 670.1K", "372.3K - 446.7K",
"372.3K - 446.7K", "223.4K - 297.8K", "148.9K - 223.4K")), class = "data.frame", row.names = c(NA,
-14L))
library(scales)
library(ggplot2)
names(mycols) <- mycols
ylab <- c(0.5,1,1.5,2,2.5)
lbls <- setNames(unique(Cum$color), unique(Cum$Range))
ggplot_obj <- ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender,fill = Range)) +
geom_bar(aes(
text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age.group` ,
"<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
lab2)),
position = "stack", stat = "identity",size=5) +
geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
position = position_dodge(width=0.9),size=2) +
scale_fill_manual(values = lbls) +
coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
theme_bw()+ theme(
# remove the vertical grid lines
panel.grid.major.x = element_blank(),
panel.border = element_blank(), axis.line.x = element_line()
) +
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))+
xlab("Age group")
ggplot_obj
答案 0 :(得分:1)
默认情况下边框和栏的颜色是相同的。对于您的情况,您可能需要在 color
之外指定一个 mapping
。当您为条形绘制标题时,我建议使用 position = "dodge"
而不是堆栈。
ggplot_obj <- ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender,fill = Range)) +
geom_bar(aes(
text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age.group` ,
"<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
lab2)), color = "black",
position = "dodge", stat = "identity",size=0.01) +
geom_text(aes(y = Cases + max(Cum$Cases)*.02, label = Gender), vjust = 1,
position = position_dodge(width=0.9),size=2) +
scale_fill_manual(values = lbls) +
coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
theme_bw()+ theme(
# remove the vertical grid lines
panel.grid.major.x = element_blank(),
panel.border = element_blank(), axis.line.x = element_line()
) +
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))+
xlab("Age group")
#> Warning: Ignoring unknown aesthetics: text
ggplot_obj
#> Warning: Use of `Cum$Cases` is discouraged. Use `Cases` instead.
由 reprex package (v2.0.0) 于 2021 年 6 月 5 日创建
更新:将 geom_text 位置调整为位于栏的顶部。
答案 1 :(得分:0)
size
中的 geom_bar()
美学适用于 col
美学,即条形周围的线条应该有多宽。在您当前的代码中,更改 size
没有任何区别,因为您尚未将任何属性映射到 col
美学。
我建议通过将 position = "stacked"
中的 position = "dodge"
更改为 geom_bar()
将公条和母条并排放置而不是堆叠。
然后在我看来,所有数据都显示在图中。这有点奇怪,因为您的 Range
变量与您的 Age.group
变量几乎完全一致。
这是从 text
中删除了 size
和 geom_bar()
美学的绘图代码,结果如下:
ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender, fill = Range)) +
geom_bar(position = "dodge", stat = "identity") +
geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
position = position_dodge(width=0.9),size=2) +
scale_fill_manual(values = lbls) +
coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
theme_bw()+ theme(
# remove the vertical grid lines
panel.grid.major.x = element_blank(),
panel.border = element_blank(), axis.line.x = element_line()
) +
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))+
xlab("Age group")