我试图创建一个以{ggplot2}为单位的图,其中箭头线向外指向,并位于x轴下方.我在{ggplot2}中使用了geom_segment函数,但它已经绘制了情节.我希望这条线有一个指向右侧的箭头,位于x轴下方,标记为内左侧和外右侧,以表示类似于"内"部分的东西.

Data

library(tidyr)
# Define the vectors
st <- c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10")
y1 <- c(5, 1, 4, 6, 2, 3, 7, 8, 2, 8)                   # Create more example data
y2 <- c(3, 3, 3, 3, 4, 4, 5, 5, 7, 7)
# Create a data frame
df <- data.frame(st = st, y1 = y1, y2 = y2)

# Pivot from wide to long
df_long <- df %>%
  pivot_longer(
    cols = c("y1", "y2"),  # Columns to pivot
    names_to = "variable",       # Column name for variable names
    values_to = "value"          # Column name for values
  )

Code

library(ggplot2)
library(grid)  # For arrow customization
# Create the plot
ggplot(data = df_long, aes(x = st, y = value, color = variable, group = variable)) +
  geom_line(size = 1.2) +  # Adjust line thickness
  labs(
    title = "Line Plot with Arrow Beneath X-Axis",
    x = "Identifier",
    y = "Value",
    color = "Variable"
  ) +
  theme_minimal() +  # Use a clean theme for better visualization
  geom_segment(
    aes(x = 1, y = -1, xend = 10, yend = -1),  # Line for "inner" section
    arrow = arrow(type = "closed", length = unit(0.2, "inches")),
    color = "blue",
    size = 1.2
  )  +
  annotate("text", x = 3, y = -1.5, label = "Inner", color = "blue", size = 4) +  # Label for inner
  annotate("text", x = 8, y = -1.5, label = "Outer", color = "red", size = 4)  # Label for outer

enter image description here

我也try 过这个,

+ geom_line(size = 1.2) +  # Adjust line thickness
  geom_hline(yintercept = 0, linetype = "dashed") +  # Add horizontal line at y = 0
  geom_vline(xintercept = -1, linetype = "dashed") +  # Add vertical line at x = 0

但在图外的x轴下方没有箭头线.

推荐答案

根据您的代码,一个快速 Select 是通过coord_cartesian设置限制,以便箭头绘制在面板外部.此外,设置clip="off"并通过增加绘图的底部空白为箭头腾出一些空间:

library(ggplot2)

ggplot(data = df_long, aes(x = st, y = value, color = variable, group = variable)) +
  geom_line(size = 1.2) + # Adjust line thickness
  labs(
    title = "Line Plot with Arrow Beneath X-Axis",
    x = "Identifier",
    y = "Value",
    color = "Variable"
  ) +
  theme_minimal() + # Use a clean theme for better visualization
  geom_segment(
    aes(x = 1, y = -1.5, xend = 10, yend = -1.5), # Line for "inner" section
    arrow = arrow(type = "closed", length = unit(0.2, "inches")),
    color = "blue",
    size = 1.2
  ) +
  annotate("text", x = 3, y = -2, label = "Inner", color = "blue", size = 4) + # Label for inner
  annotate("text", x = 8, y = -2, label = "Outer", color = "red", size = 4) +
  coord_cartesian(clip = "off", ylim = c(0, NA)) +
  theme(plot.margin = margin(5.5, 5.5, 50, 5.5))

R相关问答推荐

显示矩阵的上半部分或下半部分时正确放置重要相关性

为什么predicate.lm给出的是一个长度与我解析的数据集不同的载体?

使用列表列作为case_when LHS的输入

在水平条形图中zoom x_轴

查找满足SpatRaster中条件的单元格位置

从API中抓取R数据SON

过滤器数据.基于两列的帧行和R中的外部向量

如何在xyplot中 for each 面板打印R^2

RStudio中相关数据的分组箱形图

在R中按行按列范围查找最大值的名称

DEN扩展包中的RECT树形图出现异常行为

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

安全地测试文件是否通过R打开

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

在带有`R`中的`ggmosaic`的马赛克图中使用图案而不是 colored颜色

如何对r中包含特定(未知)文本的行求和?

优化从每个面的栅格中提取值

将工作目录子文件夹中的文件批量重命名为顺序

是否可以将线性模型的p值添加到tbl_summary中

如何更改包中函数中的参数?