R:条形图,每个选项一个条形

t98cgbkg  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(187)

这段代码生成了一个堆叠条形图,这很好。但是我也想做一个普通的条形图,但是我不知道如何修改这段代码来达到这个目的。所以,在那个普通的条形图中,会有三个条形,每个条形代表一个答案选项,分别是“是”,“否”和“可能”。我发现这很难做到。什么是聪明的方法呢?

mydata %>%
  
  mutate(
    variable = recode(
      variable,
      'item3' = 'item name here')) %>%
  
  drop_na() %>%
  ggplot(mapping = aes(x = variable)) +
  geom_bar(aes(fill = value),
           position = 'fill',
           width = 0.30)+
  scale_y_continuous(name = 'Percentage',
                     breaks = seq(from = 0, to = 1, by = 0.2),
                     minor_breaks = seq(from = 0, to = 1, by = 0.1),
                     labels = seq(from = 0, to = 100, by = 20),
                     expand = c(0,0)) +
  xlab( element_blank() ) +
  scale_fill_manual(
    values = c(colour="red"), #if this was stacked bar chart, here would be a color palette
    labels = c(
      'yes',
      'no',
      'maybe'
    ),
    drop = FALSE
  ) +
  
  guides(
    fill = guide_legend(title = 'Answer')
  ) + theme(panel.background = element_blank(),
            axis.ticks.y = element_blank(),
            panel.grid.major = element_line(colour = 'grey'),
            axis.ticks.x = element_line(colour = 'grey'),
            panel.grid.minor = element_line(colour = 'lightgrey'),
            axis.text.y = element_text(
              size = 11,
              face = 'bold',
              hjust = 0
            )) +
  coord_flip()

这是我用的数据

structure(list(variable = c("item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3", "item3", "item3", "item3", "item3", 
"item3", "item3", "item3"), value = structure(c(1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("1", 
"2", "3"), class = c("ordered", "factor"))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -147L))
jdzmm42g

jdzmm42g1#

也许这就是你要找的。为了得到每个值的条形图,你必须将valueMap到xy上,然后删除position="fill"。如果你想显示计数,这很好。如果你想显示百分比,那么一种方法是在ggplot之外手动计算计数和百分比:

library(dplyr)
library(tidyr)
library(ggplot2)

mydata <- mydata %>%
  mutate(
    variable = recode(
      variable,
      "item3" = "item name here"
    )
  ) %>%
  drop_na() %>%
  mutate(value = recode(value, "1" = "yes", "2" = "no", "3" = "maybe")) %>%
  count(variable, value) |> 
  group_by(variable) |> 
  mutate(pct = n / sum(n))

ggplot(mydata, mapping = aes(x = value, y = pct)) +
  geom_col(aes(fill = value),
    width = 0.30
  ) +
  scale_y_continuous(
    breaks = seq(from = 0, to = 1, by = 0.2),
    minor_breaks = seq(from = 0, to = 1, by = 0.1),
    labels = seq(from = 0, to = 100, by = 20),
    limits = c(0, 1),
    expand = c(0, 0)
  ) +
  scale_fill_manual(
    values = c(yes = "green", no = "red", maybe = "grey80"),
    drop = FALSE
  ) +
  labs(x = "Answer", y = "Percentage") +
  guides(
    fill = "none"
  ) +
  theme(
    panel.background = element_blank(),
    panel.grid.major = element_line(colour = "grey"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor = element_line(colour = "lightgrey"),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(colour = "grey"),
    axis.text.x = element_text(
      size = 11,
      face = "bold",
      hjust = 0
    )
  )

相关问题