这段代码生成了一个堆叠条形图,这很好。但是我也想做一个普通的条形图,但是我不知道如何修改这段代码来达到这个目的。所以,在那个普通的条形图中,会有三个条形,每个条形代表一个答案选项,分别是“是”,“否”和“可能”。我发现这很难做到。什么是聪明的方法呢?
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))
1条答案
按热度按时间jdzmm42g1#
也许这就是你要找的。为了得到每个值的条形图,你必须将
value
Map到x
或y
上,然后删除position="fill"
。如果你想显示计数,这很好。如果你想显示百分比,那么一种方法是在ggplot之外手动计算计数和百分比: