[英]Spacing and custom x-axis ticks in ggplot
我試圖在我的 x 軸上強制空間和刻度。 我有以下數據
df
Antibiotic timepoint resistant intermediate susceptible
1 Erythromycin0 T0 20.588235 79.411765 0.00000
2 Erythromycin1 T1 32.352941 67.647059 0.00000
3 Doxycycline0 T0 0.000000 0.000000 100.00000
4 Doxycycline1 T1 2.941176 0.000000 97.05882
5 Clindamycin0 T0 2.941176 5.882353 91.17647
6 Clindamycin1 T1 11.764706 5.882353 82.35294
7 Gentamycin0 T0 5.882353 5.882353 88.23529
8 Gentamycin1 T1 17.647059 11.764706 70.58824
9 Clarithromycin0 T0 17.647059 14.705882 67.64706
10 Clarithromycin1 T1 26.470588 23.529412 50.00000
當我使用 geom_bar function 我得到下圖
ggplot(df, aes(fill= variable, y=value, x=Antibiotic, pattern = timepoint)) +
geom_bar_pattern(position="fill", stat="identity",
color = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.025,
pattern_key_scale_factor = 0.6)+
scale_fill_manual(values = c("#0066CC","#FFFFFF","#FF8C00")) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +
scale_pattern_manual(values = c(T0 = "none", T1="stripe")) +
guides(pattern = guide_legend(override.aes = list(fill = "white")),fill = guide_legend(override.aes = list(pattern = "none")))
我想增加第 2 條和第 3 條、第 4 條和第 5 條等之間的空間。還刪除刻度並在第 1 條和第 2 條之間添加帶有標簽的自定義刻度等。感謝所有幫助
您是否考慮過對數據進行刻面處理,而不是對 x 軸做復雜的事情? 通過閱讀您的問題,這完成了非常相似的事情。
library(ggplot2)
library(tidyr)
txt <- "Antibiotic timepoint resistant intermediate susceptible
1 Erythromycin0 T0 20.588235 79.411765 0.00000
2 Erythromycin1 T1 32.352941 67.647059 0.00000
3 Doxycycline0 T0 0.000000 0.000000 100.00000
4 Doxycycline1 T1 2.941176 0.000000 97.05882
5 Clindamycin0 T0 2.941176 5.882353 91.17647
6 Clindamycin1 T1 11.764706 5.882353 82.35294
7 Gentamycin0 T0 5.882353 5.882353 88.23529
8 Gentamycin1 T1 17.647059 11.764706 70.58824
9 Clarithromycin0 T0 17.647059 14.705882 67.64706
10 Clarithromycin1 T1 26.470588 23.529412 50.00000"
df <- read.table(text = txt)
df <- df %>% pivot_longer(3:5)
df$abiotic_name <- gsub("0|1", "", df$Antibiotic)
ggplot(df, aes(timepoint, value, fill = name)) +
geom_col() +
facet_grid(~ abiotic_name, switch = "x") +
theme(strip.placement = "outside")
由reprex package (v0.3.0) 於 2020 年 12 月 5 日創建
在 Teunbrand 對 faceting 的闡述的基礎上,這里有一個更完整的工作示例,展示了如何使用自定義注釋和主題元素更完整地偽造 x 軸:
library(dplyr)
library(tidyr)
library(ggpattern)
df %>%
mutate(Antibiotic = gsub("\\d", "", Antibiotic)) %>%
pivot_longer(3:5) %>%
ggplot(aes(timepoint, value, fill = name, pattern = timepoint)) +
geom_bar_pattern(position="fill",
stat="identity",
color = "black",
pattern_fill = "black",
pattern_angle = 45/4,
pattern_density = 0.1,
pattern_spacing = 0.025,
pattern_key_scale_factor = 0.6)+
scale_fill_manual(values = c("#ffabab","#fff5ba","#dbffd7"),
name = "variable") +
scale_pattern_manual(values = c(T0 = "none", T1="stripe")) +
labs(x = "Antibiotic") +
guides(pattern = guide_legend(override.aes = list(fill = "white")),
fill = guide_legend(override.aes = list(pattern = "none"))) +
facet_grid(.~Antibiotic, switch = "x") +
annotation_custom(grid::linesGrob(), xmin = 1.5, xmax = 1.5,
ymin = -0.075, ymax = -0.05) +
coord_cartesian(clip = "off") +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing = unit(0, "points"),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.