我有以下資料,我需要制作一個類似于我在 tableau 中制作的圖表
df<-data.frame(estado=c("Aprobado","Reprobado"),
freq=c(551,7),
porcentaje=c(0.99,1))
帶有 echatr 的圖看起來像這樣
df %>%
head() %>%
mutate(estado = row.names(.)) %>%
e_charts(estado) %>%
e_pie(freq, radius = c("50%", "70%")) %>%
e_title("Donut chart")
我不明白為什么我不能正確放置標簽或標題。
使用 ggplot2 我有以下方法
ggplot(df,aes(x=2,y=porcentaje, fill=estado))
geom_bar(stat = "identity",
color="white")
geom_text(aes(label=percent(porcentaje/100)),
position=position_stack(vjust=0.5),color="white",size=6)
coord_polar(theta = "y")
scale_fill_manual(values=c("salmon","steelblue"))
theme_void()
labs(title="Gráfico de Dona")
xlim(0.5,2.5)
我不喜歡這張圖表的外觀,它的色調看起來很差而且美觀,我正在尋找與我在 Tableau 中所做的結果相似的結果,即使帶有中心標簽
希望有人能告訴我這樣做會更好,以便它的陰影在兩個代碼中的任何一個中都是正確的。
uj5u.com熱心網友回復:
您可以通過更改 中的values
引數來更改顏色scale_fill_manual()
。
將文本放在中間可以使用annotate()
,設定x = -Inf
和y = -Inf
將它放在中間。
library(ggplot2)
library(scales)
df<-data.frame(estado=c("Aprobado","Reprobado"),
freq=c(551,7),
porcentaje=c(0.99,1))
ggplot(df,aes(x=2,y=porcentaje, fill=estado))
geom_bar(stat = "identity",
color="white")
geom_text(aes(label=percent(porcentaje/100)),
position=position_stack(vjust=0.5),color="white",size=6)
annotate(
"text", x = -Inf, y = -Inf, label = paste0("Total\n", sum(df$freq))
)
coord_polar(theta = "y")
scale_fill_manual(values=c("#75A1C7","#F9A655"))
theme_void()
labs(title="Gráfico de Dona")
xlim(0.5,2.5)
由reprex 包于 2022-05-23 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480423.html