为何使用ggplot绘制的条形图y轴没有显示刻度?
问题原因
你为y轴调用了适配离散分类变量的scale_y_discrete()函数,但geom_bar()默认生成的y轴数值是计数统计值,属于连续数值类型,标度类型和变量类型不匹配,因此y轴刻度无法正常渲染显示。
修正方案
将原代码中的scale_y_discrete("Number of Individuals")替换为适配连续变量的scale_y_continuous("Number of Individuals")即可,修改后完整可运行代码如下:
ggplot(data = PTC, mapping = aes(x = Genotype, fill = Smoker)) + geom_bar(position = position_dodge()) + ggtitle("Barplot of Genotype and Smoking Status") + scale_fill_discrete(name = "Smoking Status", breaks = c("N", "S"), labels = c("Non-smoker", "Smoker")) + scale_y_continuous("Number of Individuals") + geom_text(aes(label =..count..), stat = "count", position = position_dodge(width = 0.9), vjust = 1.25)
内容的提问来源于stack exchange,提问作者user17370727
相关产品推荐
相关产品推荐

