我是R新手,尝试为不同组别的分类变量(度)创建条形图。这是我与迄今使用的代码:使用ggplot2的百分比和图形标签中的条形图
graph_data <- gss %>%
group_by(degree, owngun) %>%
summarise(total = n()) %>%
ungroup() %>%
mutate(percent = total/57061)
ggplot(graph_data, aes(x=degree, fill=owngun, y=percent) +
geom_bar(stat="identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5))
第一部分的作品,我能够创建一个新的变量,gunowners的百分比。然而,当我运行第二部分图表中的变量,我得到以下错误信息:
> ggplot(aes(x=degree, fill=owngun, y=percent) +
+ geom_bar(stat="identity")
+ ggplot(graph_data, aes(x=degree, fill=owngun, y=percent) +
Error: unexpected symbol in:
" geom_bar(stat="identity")
ggplot"
> geom_bar(stat="identity") +
+ geom_text(size = 3, position = position_stack(vjust = 0.5))
Error in position_stack(vjust = 0.5) : unused argument (vjust = 0.5)
'ggplot(aes())'中缺少一个闭括号# – beetroot