如何使用 ggplot() geom(bar

问题描述 投票:0回答:1

我正在尝试创建一个条形图,显示组内年龄类别(事件位置)的相对百分比。 我有一个类别在我的观察中不存在(小于 18 岁,即类别 1)。我已经尝试过“drop=FALSE”,请参阅: scale_x_discrete(breaks=c("1", "2","3","4","5","6"), labels=vec.age_cat, drop=FALSE).

这是我到目前为止所做的。如何显示零观察值的第一个类别。

f.s_c_subset \<- data.frame(
study = c(rep(1,10),rep(2,5)),
age_cat = c(3,3,4,2,6,3,2,5,2,4,5,6,5,4,4)
)

table(df.s_c_subset)

vec.age_cat \<- c("\< 18 years", "18-23 years", "24-27 years", "28-35 years", "36-45 years", "\> 45 years")

ggplot(data=df.s_c_subset, aes(x=as.factor(age_cat), fill=as.factor(study)))+
geom_bar(aes(y=..count../tapply(..count.., ..fill.. ,sum)\[..fill..\]), position="dodge") +
scale_fill_manual(values=c("grey40","grey60"),name = "event location", labels = c("university", "outside")) +
geom_text(aes(y=..count../tapply(..count.., ..fill.. ,sum)\[..fill..\],
label=scales::percent(..count../tapply(..count.., ..fill.. ,sum)\[..fill..\], accuracy=1)),
stat="count", position=position_dodge(0.9), vjust=-0.5)+
ylab('percent of audience relative to location') +
xlab("age groups") +
theme(axis.text.x = element_text(angle = 45, hjust = .8)) +
theme(axis.ticks.x = element_blank()) +
scale_y_continuous(labels = scales::percent, limits=c(0,0.45)) +
scale_x_discrete(breaks=c("1", "2","3","4","5","6"), labels=vec.age_cat, drop=FALSE) +
theme(panel.border = element_rect(linetype = "solid", colour="black", linewidth=.5, fill = NA),
panel.grid.minor=element_line(colour="grey80", linewidth=.3),
panel.grid.major.y=element_line(colour="grey80", linewidth=.3),
panel.background=element_rect(fill="grey97")) +
theme(axis.title.x.bottom = element_text(margin = margin(t = .2, unit = "in")))

如果您能分享一些想法那就太好了。 最好的,希尔克

r dataframe ggplot2 null geom-bar
1个回答
0
投票

原样

drop=FALSE
不会产生任何影响,因为您的
factor
不包括缺失的年龄类别。此外,设置
breaks
也不会产生任何效果,因为只会显示落在限制范围内的中断。

相反,要解决您的问题,请在转换为

factor
时设置适当的级别,我还建议在此阶段设置标签。之后你应该就可以了
drop=FALSE

另外请注意,我删除了已弃用的

..
符号,而是切换为
after_stat
。另外,我放弃了
tapply
(我过去也使用过)并使用
ave()
代替。

df.s_c_subset <- data.frame(
  study = c(rep(1, 10), rep(2, 5)),
  age_cat = c(3, 3, 4, 2, 6, 3, 2, 5, 2, 4, 5, 6, 5, 4, 4)
)

vec.age_cat <- c("< 18 years", "18-23 years", "24-27 years", "28-35 years", "36-45 years", "> 45 years")

library(ggplot2)

ggplot(data = df.s_c_subset, aes(
  x = factor(age_cat, levels = 1:6, labels = vec.age_cat),
  fill = factor(study)
)) +
  geom_bar(
    aes(
      y = after_stat(count / ave(count, fill, FUN = sum))
    ),
    position = "dodge"
  ) +
  scale_fill_manual(
    values = c("grey40", "grey60"),
    name = "event location",
    labels = c("university", "outside")
  ) +
  geom_text(
    aes(
      y = after_stat(count / ave(count, fill, FUN = sum)),
      label = after_stat(scales::percent(count / ave(count, fill, FUN = sum), accuracy = 1))
    ),
    stat = "count", position = position_dodge(0.9), vjust = -0.5
  ) +
  ylab("percent of audience relative to location") +
  xlab("age groups") +
  theme(axis.text.x = element_text(angle = 45, hjust = .8)) +
  theme(axis.ticks.x = element_blank()) +
  scale_y_continuous(labels = scales::percent, limits = c(0, 0.45)) +
  scale_x_discrete(drop = FALSE) +
  theme(
    panel.border = element_rect(linetype = "solid", colour = "black", linewidth = .5, fill = NA),
    panel.grid.minor = element_line(colour = "grey80", linewidth = .3),
    panel.grid.major.y = element_line(colour = "grey80", linewidth = .3),
    panel.background = element_rect(fill = "grey97")
  ) +
  theme(axis.title.x.bottom = element_text(margin = margin(t = .2, unit = "in")))

© www.soinside.com 2019 - 2024. All rights reserved.