width - 如何统一ggplot2中条的宽度?
问题描述
我正在尝试使用 geom_bar(stat="count") 按性别获取条形图,除了条形的宽度不一致之外,一切似乎都很好。例如,如果“F”组中没有得分值为 3 的观察值,则“M”组的条形将变为两倍大。如何使其与示例中的其他条一样窄?
Score = read.table(header=TRUE,text='score sex
1 0 F
2 0 F
3 0 M
4 0 M
5 0 F
6 1 F
7 1 M
8 1 M
9 2 F
10 2 M
11 2 F
12 3 M
13 3 M'
)
b1=ggplot(Score, aes(x=factor(score), fill=sex))+
geom_bar(stat="count", position=position_dodge())+
theme_classic()
b1
解决方案
将 position=position_dodge2(preserve="single") 添加到 geom_bar() 行解决了它:
b1=ggplot(Score, aes(x=factor(score), fill=factor(sex)))+
geom_bar(stat="count", position=position_dodge2(preserve="single"))+
theme_classic()
b1
推荐阅读
- r - 如何在 R 中为这种情况编写循环?
- arrays - 使用已经排序的列表快速排序交换元素
- java - 如何将 2021-05-31T02:40:05.2Z 字符串转换为日期对象时区 (GMT+7) Java android
- scripting - 谷歌文档脚本,返回指定文本中间的文本值
- android - Instagram 应用程序如何在网格视图中显示他们的个人资料,并使用 Flutter 实现它
- python - 如何在 Python 中动态指定 unicode 字符串?
- apache-kafka - 由于组协调器重新发现不成功,Kafka 提交失败
- r - updatePickerInput 即时刷新更新
- r - 如何在 R 中使用 list.files() 读取包含高棉脚本(非 Windows 本机编码)的目录名称?
- android - 网站无法在安卓上正常运行