在“ R”中指示重要性的“双”条形图

时间:2019-01-14 12:57:38

标签: r ggplot2 bar-chart significance

我想为三个类别的数据绘制一个“双”条形图,并针对两个“双”条分别显示基于Wilcox检验的显着性水平。

运行以下代码,我看不到y轴上反映的计数,而是所有条形图都在同一高度。

library(dplyr) 
library(ggplot2)
library(ggpubr)
library(reshape2)
theme_set(theme_pubclean())

data = data.frame("cut" = c("type 1","type 1","type 2","type 2","type 3","type 3"), "counts" = c(0.6844,0.5867,0.6297,0.6383,0.7134,0.7075), "color" = c("c","d","c","d","c","d"))
data

df <- data %>%
  filter(color %in% c("c", "d")) %>%
  group_by(cut, color) %>%
  summarise(counts = n()) 

ggdotchart(df, x = "cut", y ="counts",
           color = "color", palette = "jco", size = 3, 
           add = "segment", 
           add.params = list(color = "lightgray", size = 1.5),
           position = position_dodge(0.3),
           ggtheme = theme_pubclean()
)

我要绘制的情节看起来像这样: https://drive.google.com/open?id=1EndiF-sCtXFyUOAPIToRY5hqjp97b1Px

感谢您的帮助: 1.编辑我的代码,使其显示实际计数值 2.增加情节的重要性

1 个答案:

答案 0 :(得分:0)

这是您要寻找的吗?

data %>%
  spread(color, counts) %>%
  mutate(
    lab_pos = pmax(c, d) + .07,
    lab = if_else(cut == 'type 3', '*', 'ns')
  ) %>%
  gather(color, counts, c:d) %>%
  ggplot(aes(
    x = cut,
    y = counts,
    color = color,
    group = color
  )) +
  geom_col(
    position = position_dodge(.4),
    color = 'white',
    width = .05
  ) +
  geom_point(
    position = position_dodge(.4),
    size = 3
  ) +
  geom_text(aes(
      y = lab_pos,
      label = lab
    ),
    check_overlap = TRUE,
    color = rgb(.2, .2, .2)
  ) +
  ylim(0, 1)

enter image description here