使用r中的ggplot2更改x labs

发布于 2025-01-29 18:24:28 字数 1564 浏览 4 评论 0原文

我有此数据并使用条形图绘制它。我想更改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"
)
)

It will result this plot
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2025-02-05 18:24:28

实现此目的的一种方法可能是:

  1. 我们在绘制label_cell.types

    之前创建一个标签列

  2. 之前创建一个标签列,然后对其进行分组并绘制

library(tidyverse)

data_bayes %>% 
  mutate(label_cell.types = parse_number(Cell.types)) %>% 
  ggplot(aes(x=factor(label_cell.types), y=Total, fill=Estimation)) + 
  scale_fill_manual(breaks = c("Best three rank", "No rank"), values=c("green", "red")) +
  labs(x = "Cell types")+
  geom_bar(stat="identity", position="dodge")

“在此处输入图像描述”

One way to achieve this could be:

  1. We create a label column before we plot label_cell.types

  2. Then factor it and plot

library(tidyverse)

data_bayes %>% 
  mutate(label_cell.types = parse_number(Cell.types)) %>% 
  ggplot(aes(x=factor(label_cell.types), y=Total, fill=Estimation)) + 
  scale_fill_manual(breaks = c("Best three rank", "No rank"), values=c("green", "red")) +
  labs(x = "Cell types")+
  geom_bar(stat="identity", position="dodge")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文