我嘗試繪制條形圖,其 X 軸與平時略有不同。這個想法是用初始變數(代碼)和分組變數(區域)來表示這個軸。每個代碼都有一個屬于它的區域。我想用垂直線來劃定我在情節上的區域。
這是一個reprex
library(dplyr)
library(ggplot2)
data <- tibble::tribble(
~code, ~term, ~estimate, ~std.error, ~statistic, ~p.value, ~region,
"ANTA", "t_chla", 0.0577051712200805, 0.000720537559840254, 80.0862778518777, 1.51491004165028e-36, "Polar",
"ARCH", "t_chla", 0.0528096415790542, 0.00237431411186973, 22.2420619559337, 9.38778083531695e-08, "Equatorial",
"ARCT", "t_chla", 0.0381489725623347, 0.000549351930679462, 69.4435942277484, 7.1661522966813e-84, "Polar",
"BPLR", "t_chla", 0.0334314725309052, 0.000908084662707285, 36.8153696498248, 6.728272457306e-26, "Polar",
"EMED", "t_chla", 0.048713647973686, 0.00179735526256328, 27.1029601038437, 1.37127584382531e-20, "Temperate",
"SANT", "t_chla", 0.0311278181792805, 0.000594175450783383, 52.3882602996143, 4.93281235468381e-67, "Polar",
"SPSG", "t_chla", 0.0676467149977894, 0.00461643829029263, 14.653442923744, 1.64755108560374e-06, "Equatorial",
"WMED", "t_chla", 0.0359293074812375, 0.000954627809780489, 37.6369796826883, 1.88418982949485e-42, "Temperate"
)
data$code <- factor(data$code, levels = c("ANTA", "SANT", "ARCT", "BPLR", "EMED", "WMED", "SPSG", "ARCH"))
ggplot(data)
geom_bar(aes(y = estimate, x = code, fill = code), stat = "identity")
geom_errorbar(aes(ymin = estimate - std.error, ymax = estimate std.error, x = code))
scale_fill_brewer(palette = "Set1", guide = "none")
theme_bw()
ylab("Specifc absorption")
xlab("Oceanic province")
ggtitle("Specific absorption")

所以我們的想法是在 BPLR 和 EMED 之間以及 WMED 和 SPSG 之間有一條垂直線,條形在標簽極地、溫帶和赤道下分組。
預先感謝您的回答。
uj5u.com熱心網友回復:
添加facet_wrap和panel.spacing = unit(0, 'lines')到你的情節怎么樣?
ggplot(data)
geom_bar(aes(y = estimate, x = code, fill = code), stat = "identity")
geom_errorbar(aes(ymin = estimate - std.error, ymax = estimate std.error, x = code))
scale_fill_brewer(palette = "Set1", guide = "none")
theme_bw()
ylab("Specifc absorption")
xlab("Oceanic province")
ggtitle("Specific absorption")
facet_wrap(~region, scales = 'free_x')
theme(panel.spacing = unit(0, 'lines'))

編輯:更新為手動添加垂直線和標簽
這是將分組添加到條形的另一個選項。可以使用文本大小、位置、顏色、字體等進行大量自定義。
ggplot(data)
geom_bar(aes(y = estimate, x = code, fill = code), stat = "identity")
geom_errorbar(aes(ymin = estimate - std.error, ymax = estimate std.error, x = code))
scale_fill_brewer(palette = "Set1", guide = "none")
theme_bw()
ylab("Specifc absorption")
xlab("Oceanic province")
ggtitle("Specific absorption")
geom_vline(xintercept = c(4.5, 6.5))
annotate(geom = 'text', x = 2.5, y = .07, label = 'Polar')
annotate(geom = 'text', x = 5.5, y = .07, label = 'Temperate')
annotate(geom = 'text', x = 8, y = .07, label = 'Equitorial')

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