我正在嘗試更改 R 中餅圖中切片的順序。我想要 0 度右側的最大值(38,屬于農業集約化),第二大切片(20,屬于森林砍伐)在左邊,第三個最大值(17,城市化)在右邊第二個,最小值(10,濕地或河流改造)在左邊第二個。我使用了下面寫的代碼。非常感謝你幫助我!
親切的問候,
斯蒂芬妮
餅形圖
df <- data.frame(value = c(38, 20, 17, 10),
group = c('Agricultural intensification', 'Deforestation', 'Urbanization', 'Wetland or river modification'))
library(ggplot2)
ggplot(df, aes(x = "", y = value, fill = group))
labs(x='Taxon order', y='Driver', title='Driver per taxon order') theme(plot.title = element_text(hjust = 0.5, size=20, face='bold'))
geom_col(color = "black")
coord_polar("y", start=0)
geom_text(aes(label = paste0(slices, "%")), position = position_stack(vjust=0.5))
labs(x = NULL, y = NULL, fill = NULL) theme(axis.line = element_line(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank())
theme_void()
scale_fill_viridis_d()
uj5u.com熱心網友回復:
將您的group列轉換為 afactor并按levels您想要的順序設定:
df <- data.frame(
value = c(38, 20, 17, 10),
group = c("Agricultural intensification", "Deforestation", "Urbanization", "Wetland or river modification")
)
library(ggplot2)
df$group <- factor(df$group, levels = rev(c("Agricultural intensification", "Urbanization",
"Wetland or river modification", "Deforestation")))
ggplot(df, aes(x = "", y = value, fill = group))
labs(x = "Taxon order", y = "Driver", title = "Driver per taxon order")
theme(plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
geom_col(color = "black")
coord_polar("y", start = 0)
geom_text(aes(label = paste0(value, "%")), position = position_stack(vjust = 0.5))
labs(x = NULL, y = NULL, fill = NULL)
theme(axis.line = element_line(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank())
theme_void()
scale_fill_viridis_d()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416778.html
標籤:
