我的資料框在這里
data.frame(
stringsAsFactors = FALSE,
Subtype = c("OPC", "Hypopharynx", "Larynx"),
alive = c(88, 22, 100),
dead = c(12, 55, 17),
uncertain = c(10, 2, 2)
)
資料框的標題是 tata4。
當我輸入這段代碼時,我得到了一個很好的百分比圖表。由于某種原因無法嵌入影像!
tata4 %>%
gather(key = "status", value = value, -Subtype) %>%
group_by(Subtype) %>%
mutate(value = value / sum(value) * 100, status = factor(status, c("alive", "dead", "uncertain"))) %>%
ggplot(aes(y = value, x = Subtype, fill = status))
geom_col(width = .6, position = position_dodge(width = .6, preserve = "total"))
geom_text(aes(label = paste(round(value, 1), "%"), y = value 7,group = status),
position = position_dodge(width = .6, preserve = "total"),size=3,angle=90)
labs(y = "percentage", x = "Status")
scale_fill_manual(values = c("alive" = "tan2", "dead" = "red", "uncertain" = "green"),
aesthetics = c("color", "fill"))
theme(text = element_text(size = 15))
scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1),limits = c(0, 100))
我要做的是獲得絕對值以及圖表頂部顯示的百分比。我正在嘗試使用下面的 scales 函式,但下面的代碼似乎不起作用(給我奇怪的值。我做錯了什么?謝謝
tata4 %>%
gather(key = "status", value = value, -Subtype) %>%
group_by(Subtype) %>%
mutate(perc = value / sum(value) * 100, status = factor(status, c("alive", "dead", "uncertain"))) %>%
ggplot(aes(y = value, x = Subtype, fill = status))
geom_col(width = .6, position = position_dodge(width = .6, preserve = "total"))
geom_text(aes(Subtype,value 5,label = glue::glue("{value}({percent(perc)})")))
labs(y = "percentage", x = "Status")
scale_fill_manual(values = c("alive" = "tan2", "dead" = "red", "uncertain" = "green"),
aesthetics = c("color", "fill"))
theme(text = element_text(size = 15))

uj5u.com熱心網友回復:
您獲得的“奇怪”標簽來自您在函式scales::percent(perc)內部的使用glue。這會將已經手動縮放的值再次轉換為 % 100(80 到 8000%)。您可以手動定義標簽,也可以傳遞scale = 1引數。準備資料框中已有的標簽:
df0 <- tata4 %>%
gather(key = "status", value = value, -Subtype) %>%
group_by(Subtype) %>%
mutate(perc = value / sum(value) * 100, status = factor(status, c("alive", "dead", "uncertain")))
# this scales the percentage label twice:
df1 <- df0 %>% mutate(perc_label = glue::glue("{value}({percent(perc)})"))
# Subtype status value perc perc_label
#<chr> <fct> <dbl> <dbl> <glue>
#1 OPC alive 88 80 88(8 000%)
# option 1: manually define % labels
df2 <- df0 %>% mutate(perc_label = glue::glue("{value} ({round(perc, 1)}%)"))
#Subtype status value perc perc_label
#<chr> <fct> <dbl> <dbl> <glue>
#1 OPC alive 88 80 88 (80%)
# option 2: use scale parameter
df3 <- df0 %>% mutate(perc_label = glue::glue("{value}({scales::percent(perc, scale=1)})"))
# Subtype status value perc perc_label
# <chr> <fct> <dbl> <dbl> <glue>
#1 OPC alive 88 80 88(80.0%)
對于繪圖,我們還需要映射perc到 y 軸而不是value(計算的百分比在兩個示例中的不同列中定義)。
df2 %>%
ggplot(aes(y = perc, x = Subtype, fill = status))
geom_col(width = .6, position = position_dodge(width = .6, preserve = "total"))
geom_text(aes(Subtype, perc 8, label = perc_label),
position = position_dodge(width = .6, preserve = "total"),size=3,angle=90)
labs(y = "percentage", x = "Status")
scale_fill_manual(values = c("alive" = "tan2", "dead" = "red", "uncertain" = "green"),
aesthetics = c("color", "fill"))
theme(text = element_text(size = 15))
coord_cartesian(ylim = c(0, 100))
scale_y_continuous(labels = label_percent(scale = 1))

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438214.html
下一篇:ggplot2:不同寬度的條形圖
