美文网首页R语言做图绘图技巧做图
ggplot2创建 APA风格的条形图

ggplot2创建 APA风格的条形图

作者: R语言数据分析指南 | 来源:发表于2021-06-29 23:28 被阅读0次

最新的APA 7手册发布了新的样本图,为科研人员如何创建某些图表提供指导,下图展示了典型的条形图应该是什么样子:


下面让我们依据例图来绘制一副典型的条行图

library(tidyverse)
library(ggsci)
viz_data_one <- tibble(
  age_group = c(rep("Adolescent", 3), rep("Young Adult", 3)),
  framing_score = c(0.2, 0.08, 0.09, 0.3, 0.28, 0.15),
  upper = framing_score + .02,
  lower = framing_score - .02,
  reward = rep(c("Small Reward", "Medium Reward", "Large Reward"),2)) %>% 
  mutate(reward = as_factor(reward) %>% fct_relevel("Small Reward",
                                               "Medium Reward",
                                               "Large Reward"))
viz_data_one %>% 
  ggplot(aes(x = age_group,
             y = framing_score,
             fill = reward,
             ymin = lower,
             ymax = upper)) +
  geom_col(width = .5, position = position_dodge(.6),
           color = "black") +
  geom_errorbar(width = .1, position = position_dodge(.6)) +
  scale_fill_jco()+
  scale_y_continuous(expand = expansion(0),
                     limits = c(0, 0.4),
                     breaks = seq(0, .4, .1))+
  labs(
    x = "Age Group",
    y = "Framing Score",
    fill = NULL,
    title = "Low Risk") +
  theme(
    plot.margin = unit(c(1,1,1,1), "cm"),
    panel.background = element_blank(),
    plot.title = element_text(size = 22, face = "bold",
                              hjust = 0.5,
                              margin = margin(b = 15)),
    axis.line = element_line(color = "black"),
    axis.title = element_text(size = 15, color = "black",
                              face = "bold"),
    axis.text = element_text(size = 15,color = "black"),
    axis.text.x = element_text(margin=margin(t = 10)),
    axis.text.y = element_text(size = 15),
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.ticks.x = element_blank(),
    legend.position = c(0.20, 0.8),
    legend.key.size=unit(0.8,"cm"),
    legend.key.width=unit(0.8,"cm"),
    legend.spacing.x=unit(0.3,'cm'),
    legend.background = element_rect(color = "black"),
    legend.text = element_text(size = 15),
    legend.margin = margin(t = 5, l = 5, r = 5, b = 5),
    legend.key=element_rect(color=NA,fill=NA))

喜欢的小伙伴欢迎关注我的公众号

R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

相关文章

网友评论

    本文标题:ggplot2创建 APA风格的条形图

    本文链接:https://www.haomeiwen.com/subject/oprlultx.html