r - 当 barplot 正确绘制所有内容时,ggplot 将所有数据绘制为单列
问题描述
我的数据是这样的单列:
Number Assigned Row
1 1
2 1
3 2
4 1
5 2
6 3
... ...
当我使用 barplot 绘图时,我得到了我想要的:
然而,当我使用 ggplot + geom_bar 我得到这个:
这是我的 ggplot 代码:
count <- data.frame(alldata[[xaxis]])
ggplot(data=count, aes(x="My X Axis", y="My Y Axis")) +
geom_bar(stat="identity")
与我用于条形图的代码相比:
counts <- table(alldata[[xaxis]])
barplot(counts,
main = xaxis,
xlab = "Percentile",
cex.names = 0.8,
col=c("darkblue","red"), beside = group != "NA")
解决方案
假设这是您的数据:
df <- data.frame(AssRow = sample(1:3, 100, T, c(0.2, 0.5, 0.3)))
head(df)
# AssRow
#1 2
#2 1
#3 2
#4 3
#5 2
#6 2
这将为您提供每个分配行计数的条形图,并为它们着色:
ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) +
geom_bar()
要更改标签,请使用xlab
ylab
/make 背景更漂亮:
ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) +
geom_bar() +
xlab("My X-label") +
ylab("My Y label") +
theme_bw()
输出:
推荐阅读
- python - 如何在 selenium python 中修改 innerHTML
- sql - BigQuery 在带有通配符表的视图中查询 _TABLE_SUFFIX
- android - 在android中创建自定义条形图
- lua - zerobrane 中未捕获的 lua 异常错误,有什么解决方案吗?
- php - 调用未定义的方法 CodeIgniter\Database\MySQLi\Builder::num_rows()
- apache-spark - 如何在 Python 中将 Kafka 主题数据加载到 Spark Dstream 中
- python - 中断和重新启动 python 脚本
- angular - 角度 mdc-select 看起来完全没有样式,按钮看起来很好
- sql - 从 .csv 批量插入并在表中更新、插入和删除
- python - 在下面的代码中需要如何使用 applyMap 的帮助