我有這些資料并使用條形圖繪制它。我想更改 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 等。但是,它不會改變并且保持不變。為什么會這樣?如何解決?
uj5u.com熱心網友回復:
實作這一目標的一種方法可能是:
我們在繪制之前創建一個標簽列
label_cell.types然后將其分解并繪制
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")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477219.html
上一篇:用R中的df制作矩陣
