使用r中的ggplot2更改x labs
我有此数据并使用条形图绘制它。我想更改x标签以使其变得简单,
data_bayes <- read.table(text="
Estimation,Cell types,Total
No rank,Cell type 0 ,3468
No rank,Cell type 1 ,3468
No rank,Cell type 2 ,3468
No rank,Cell type 3 ,3468
No rank,Cell type 4 ,3468
No rank,Cell type 5 ,3468
No rank,Cell type 6 ,3468
No rank,Cell type 7 ,3468
No rank,Cell type 8 ,3468
No rank,Cell type 9 ,3468
Best three rank,Cell type 0 ,3
Best three rank,Cell type 1 ,3419
Best three rank,Cell type 2 ,130
Best three rank,Cell type 3 ,0
Best three rank,Cell type 4 ,538
Best three rank,Cell type 5 ,63
Best three rank,Cell type 6 ,3417
Best three rank,Cell type 7 ,2296
Best three rank,Cell type 8 ,536
Best three rank,Cell type 9 ,2
", header=TRUE, sep=",")
我使用此代码获取图,
ggplot(data_bayes, aes(x=Cell.types, y=Total, fill=Estimation)) + scale_fill_manual(breaks = c("Best three rank", "No rank"),
values=c("green", "red")) +
geom_bar(stat="identity", position="dodge")+
theme(axis.text.x = element_text(angle = 90)) + scale_x_discrete(
"Cell types",
labels = c(
"Cell type 0" = "0",
"Cell type 1" = "1",
"Cell type 2" = "2",
"Cell type 3" = "3",
"Cell type 4" = "4",
"Cell type 5" = "5",
"Cell type 6" = "6",
"Cell type 7" = "7",
"Cell type 8" = "8",
"Cell type 9" = "9"
)
)
我使用了参数scale_x_discrete将标签从单元格类型0更改为0等。但是,它不会更改并且保持不变。为什么这样?如何修复它?
I have this data and plot it using a bar plot. I want to change the x labels to make it simple,
data_bayes <- read.table(text="
Estimation,Cell types,Total
No rank,Cell type 0 ,3468
No rank,Cell type 1 ,3468
No rank,Cell type 2 ,3468
No rank,Cell type 3 ,3468
No rank,Cell type 4 ,3468
No rank,Cell type 5 ,3468
No rank,Cell type 6 ,3468
No rank,Cell type 7 ,3468
No rank,Cell type 8 ,3468
No rank,Cell type 9 ,3468
Best three rank,Cell type 0 ,3
Best three rank,Cell type 1 ,3419
Best three rank,Cell type 2 ,130
Best three rank,Cell type 3 ,0
Best three rank,Cell type 4 ,538
Best three rank,Cell type 5 ,63
Best three rank,Cell type 6 ,3417
Best three rank,Cell type 7 ,2296
Best three rank,Cell type 8 ,536
Best three rank,Cell type 9 ,2
", header=TRUE, sep=",")
I use this code to get the plot
ggplot(data_bayes, aes(x=Cell.types, y=Total, fill=Estimation)) + scale_fill_manual(breaks = c("Best three rank", "No rank"),
values=c("green", "red")) +
geom_bar(stat="identity", position="dodge")+
theme(axis.text.x = element_text(angle = 90)) + scale_x_discrete(
"Cell types",
labels = c(
"Cell type 0" = "0",
"Cell type 1" = "1",
"Cell type 2" = "2",
"Cell type 3" = "3",
"Cell type 4" = "4",
"Cell type 5" = "5",
"Cell type 6" = "6",
"Cell type 7" = "7",
"Cell type 8" = "8",
"Cell type 9" = "9"
)
)
I have used the argument scale_x_discrete to change the labels from cell type 0 to 0, etc. However, it does not change and remains same. Why is that the case? How to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的一种方法可能是:
我们在绘制
label_cell.types
之前创建一个标签列
之前创建一个标签列,然后对其进行分组并绘制
One way to achieve this could be:
We create a label column before we plot
label_cell.types
Then factor it and plot