gpt4 book ai didi

r - 具有多个组的分组条形图中的标签

转载 作者:行者123 更新时间:2023-12-04 16:36:33 27 4
gpt4 key购买 nike

我想在分组条形图中显示标签。我已经尝试过在 stackoverflow 中对这个问题的许多答案,但它们都不适用于我的应用程序。

我的图表显示了两个季度(2020 年第四季度和 2021 年第一季度)的一组条形图。标签没有按应有的方式出现在条形图的顶部。我发现当我更改图表以仅显示 2020 年第四季度或仅 2021 年第一季度的条形时,标签会正确显示,但是当我同时显示两个季度时,为了比较,标签不再出现在它们应该出现的位置。这是我的代表。感谢帮助。

Date <- as.Date(c("2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2020-10-01","2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2020-10-01","2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01","2021-01-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01","2021-01-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01"))
GEO <- c(
"Canada","Newfoundland and Labrador","Prince Edward Island",
"Nova Scotia","New Brunswick","Quebec","Ontario","Manitoba","Saskatchewan",
"Alberta","British Columbia","Yukon","Northwest Territories","Nunavut",
"Canada","Newfoundland and Labrador","Prince Edward Island","Nova Scotia",
"New Brunswick","Quebec","Ontario","Manitoba","Saskatchewan","Alberta",
"British Columbia","Yukon","Northwest Territories","Nunavut")
JVR <- c(3.5,2.6,3.2,3.3,3.9,4.1,3.4,3.1,2.3,2.4,4.2,4.4,3.0,2.6,
3.6,2.0,2.5,3.2,3.2,4.2,3.3,3.1,2.7,2.8,4.5,4.7,4.2,3.6)
df <- data.frame(Date=as.Date(Date),GEO=GEO,JVR=JVR)
ggplot(filter(df,Date>=as.Date("2020-10-01")),aes(x=Date,y=JVR,fill=GEO)) +
geom_bar(stat="identity",position="dodge") +
geom_text(aes(label=JVR,group=GEO),position=position_dodge(width=0.9),
vjust=-1,size=3)

最佳答案

ggfittext 包可以提供帮助。如果您希望标签的文本大小相同,您可能需要使用 format 转换它们,否则例如数字“3”将显示为大于“3.1”以适合条形。

获取数据

library(tidyverse)

Date <- as.Date(c("2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2020-10-01","2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2020-10-01","2020-10-01","2020-10-01","2020-10-01","2020-10-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01","2021-01-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01","2021-01-01",
"2021-01-01","2021-01-01","2021-01-01","2021-01-01"))
GEO <- c(
"Canada","Newfoundland and Labrador","Prince Edward Island",
"Nova Scotia","New Brunswick","Quebec","Ontario","Manitoba","Saskatchewan",
"Alberta","British Columbia","Yukon","Northwest Territories","Nunavut",
"Canada","Newfoundland and Labrador","Prince Edward Island","Nova Scotia",
"New Brunswick","Quebec","Ontario","Manitoba","Saskatchewan","Alberta",
"British Columbia","Yukon","Northwest Territories","Nunavut")
JVR <- c(3.5,2.6,3.2,3.3,3.9,4.1,3.4,3.1,2.3,2.4,4.2,4.4,3.0,2.6,
3.6,2.0,2.5,3.2,3.2,4.2,3.3,3.1,2.7,2.8,4.5,4.7,4.2,3.6)
df <- data.frame(Date=as.Date(Date),GEO=GEO,JVR=JVR)

绘图数据

library(ggplot2)
library(ggfittext)
d.plot <- filter(df,Date>=as.Date("2020-10-01"))

ggplot(d.plot, aes(x=Date,y=JVR,fill=GEO,label=format(JVR, digits = 2))) +
geom_bar(stat="identity",position="dodge") +
# from `ggfittext`
geom_bar_text(position="dodge",
place="top",grow=F,reflow=F,min.size=0) +
theme_bw()

reprex package 创建于 2021-09-20 (v2.0.1)

关于r - 具有多个组的分组条形图中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69257619/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com