向 ggplot2 代码添加分割/断轴

问题描述 投票:0回答:1

我试图在 x 轴(实际上是 y 轴,因为它被翻转)上的 R 代码中添加一个断线/分割轴。然而,经过多次尝试(几个小时),我应用的代码不起作用。

我尝试过的代码:

scale_y_break(c(60,240), scales="free")

scale_y_break(c(60,240), scales='free',expand = expansion(mult = c(0,.5)))

scale_y_cut(breaks=c(60,240), which=c(1, 3), scales=c(3, 5))

我的数据很大但很简单——y 轴上是湖泊名称,x 轴上是叶绿素水平。所附图片是我最接近应用分割轴的图片,但这看起来很奇怪并且无法实现我想要的效果。 Broken axis attempt

我的代码:

PB1_R_Data %>%
ggplot(., aes(x=Name, y = Result)) +

geom_col(aes(x=forcats::fct_rev(reorder(Name,Name)), y=MeanGrowSeason))+ 

(aes(x = Name, y = Result, fill = "Mean Chlorophyll a")) +

scale_fill_manual(name = NULL, values = c("Mean Chlorophyll a" = "#1ca6df")) +

geom_point(mapping=aes(y = `MaxGrowSeason`, pch = 16, color = "black"),  
size = 3, fill = "black", stat = "summary", fun=max)+

geom_hline(yintercept = 0, linetype = 'solid', color = 'black') +

geom_hline(aes(yintercept = 10, linetype = 'Middle'), color = "#d15420", 
size = 1) +

geom_hline(aes(yintercept = 20, linetype = 'Upper'), color = "#d15420", 
size = 1)+

labs(title = "PB1 (2002-2023)
Chlorophyll a for the Growing Season (May-Sep)",
x = "",
y=expression(plain("Chlorophyll ")~italic("a")~~plain("(mg/l)")), family="mono")+

labs(linetype='')+ 

theme_minimal() +
theme(axis.text=element_text(size=11,family = "Calibri", face="plain"),
legend.text = element_text(size=11,family = "Segoe UI", face="plain"),
axis.title.x = element_text(size=11, family = "Segoe UI", face="plain")) +

theme(text=element_text(size=12,family="Segoe UI"))+

theme(axis.text.x = element_text(angle=0))+
coord_flip() + 
 
scale_color_manual(values = c("#004e9a"),
labels = c("Maximum Chlorophyll a")) +
scale_shape_identity()+
labs(colour = "")+
theme(legend.position = "bottom")+
theme(plot.title = element_text(hjust = 0.5))
r ggplot2 axis
1个回答
0
投票

您还没有提供最小的、可重现的示例,但这里有一个可能有帮助的表示:

library(dplyr)
library(ggplot2)
library(ggbreak)

# Sample data
df <- data.frame(Name = c("Ellesworth",
                       "Elmer Thomas",
                       "Frederick",
                       "Lawtonka",
                       "Walters (Dave Boyer)",
                       "Waurika"),
                 Result = c(47, 248, 30, 45, 48, 43))

df$Name <- factor(df$Name)

df %>%
  ggplot(., aes(x = factor(Name, level = rev(Name)), y = Result)) +
  coord_flip() +
  geom_point() +
  scale_y_continuous(breaks = seq(0,250,20), limits = c(0,250)) +
  scale_y_break(c(60,240), space = .2, ticklabels = c(seq(0,250,20), 250)) +
  theme(axis.title = element_blank())

© www.soinside.com 2019 - 2024. All rights reserved.