【问题标题】:ggplot does not find the value that is present in the datasetggplot 找不到数据集中存在的值
【发布时间】:2021-07-16 07:07:46
【问题描述】:

我的数据如下:

library(ggplot2)
library(dplyr)
library(tidyverse)
library(ggsignif)

graph <- structure(list(Constraint = structure(c(4L, 2L, 3L, 1L, 5L, 4L, 
2L, 3L, 1L, 5L), .Label = c("Major Constraint", "Minor Constraint", 
"Moderate Constraint", "No Constraint", "Total"), class = "factor"), 
    `Observation for Crime = 0` = c(3124, 2484, 3511, 4646, 13765, 
    3124, 2484, 3511, 4646, 13765), `Observation for Crime = 1` = c(762, 
    629, 1118, 1677, 4186, 762, 629, 1118, 1677, 4186), `Total Observations` = c(3886, 
    3113, 4629, 6323, 17951, 3886, 3113, 4629, 6323, 17951), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L), .Label = c("Crime = 0", "Crime = 1"), class = "factor"), 
    value = c(80.3911477097272, 79.79441053646, 75.847915316483, 
    73.4777795350308, 76.6809648487549, 19.6088522902728, 20.20558946354, 
    24.152084683517, 26.5222204649692, 23.3190351512451)), row.names = c(NA, 
-10L), class = "data.frame")

            Constraint Observation for Crime = 0 Observation for Crime = 1 Total Observations  variable    value
1        No Constraint                      3124                       762               3886 Crime = 0 80.39115
2     Minor Constraint                      2484                       629               3113 Crime = 0 79.79441
3  Moderate Constraint                      3511                      1118               4629 Crime = 0 75.84792
4     Major Constraint                      4646                      1677               6323 Crime = 0 73.47778
5                Total                     13765                      4186              17951 Crime = 0 76.68096
6        No Constraint                      3124                       762               3886 Crime = 1 19.60885
7     Minor Constraint                      2484                       629               3113 Crime = 1 20.20559
8  Moderate Constraint                      3511                      1118               4629 Crime = 1 24.15208
9     Major Constraint                      4646                      1677               6323 Crime = 1 26.52222
10               Total                     13765                      4186              17951 Crime = 1 23.31904

我正在尝试创建这样的东西:

graph %>% 
    mutate(`Constraint` = fct_relevel(`Constraint`, "No Constraint", "Minor Constraint", "Moderate Constraint", "Major Constraint")) %>%
    ggplot(aes(x = `Constraint`, y = value, fill = variable, label=sprintf("%.02f %%", round(value, digits = 1)))) + 
    geom_col(position = 'dodge') + 
    geom_text(position = position_dodge(width = .9),    # move to center of bars
              vjust = -0.5,    # nudge above top of bar
              size = 4) +           
    scale_fill_grey(start = 0.8, end = 0.5) +
    theme_bw(base_size = 15) +
    geom_signif(stat="identity",
              data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),
                              y=c(5.8, 8.5), annotation=c("**", "NS")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) +
    geom_signif(comparisons=list(c("treatment", "control")), annotations="***",
              y_position = 9.3, tip_length = 0, vjust=0.4)

希望外观接近下图:

但是它给出了找不到值的错误,而值在数据中。有谁知道可能是什么问题?

【问题讨论】:

  • geom_signif 来自哪个包?
  • 两个geom_signif在一次通话中做什么?
  • @MonJeanJean 我很抱歉,geom_signif 来自library(gg_signif)。我会添加这个。 (有人已经这样做了)

标签: r ggplot2


【解决方案1】:

geom_colgeom_text 中包含filllabel -

library(tidyverse)
library(ggsignif)

graph %>% 
  mutate(`Constraint` = fct_relevel(`Constraint`, "No Constraint", "Minor Constraint", "Moderate Constraint", "Major Constraint")) %>%
  ggplot(aes(x = `Constraint`, y = value)) + 
  geom_col(position = 'dodge', aes(fill = variable)) + 
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            aes(label=sprintf("%.02f %%", round(value, digits = 1))),
            vjust = -0.5,    # nudge above top of bar
            size = 4) +           
  scale_fill_grey(start = 0.8, end = 0.5) +
  theme_bw(base_size = 15) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),
                              y=c(5.8, 8.5), annotation=c("**", "NS")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))

【讨论】:

  • 非常感谢罗纳克。你知道为什么geom_text 产生的百分比位置在这对条的中间而不是条的中间吗?如果您愿意,我也可以为此提出一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 2019-04-09
  • 2011-10-24
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多