无论该图的范围如何

发布于 2025-02-13 14:55:48 字数 1394 浏览 0 评论 0 原文

我需要制作几个相同情节的副本,并希望所有这些副本都在X轴下有一点箭头来帮助读者。但是,我发现放置这样的箭头的唯一方法是GEOM_SEMENT元素,它要求我指定根据数据范围更改的开始和终点。

理想情况下,如果我可以访问GGPLOT2在X轴上放置tickmark的值,那将解决我的问题,因为我可以将其用作起点和终点。

一个最小的示例是:

library(dplyr)
library(tidyr)
library(ggplot2)

data <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

data1 <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  filter(mpg > 15) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

facet_df <- data.frame(
  type = "mpg"
)

plot_func <- function(df) {
  df %>% ggplot +
    geom_point(aes(x = val, y = names)) +
    facet_wrap(facets = vars(type), ncol = 3, scales = "free_x") +
    geom_segment(data = facet_df, x = 11, xend = 14, y = -1, yend = -1,
                 arrow = arrow(length = unit(3, "pt"))) +
    coord_cartesian(clip = "off")
}

plot_func(data)
plot_func(data1)

plot_func(data)

”在此处输入图像描述”

plot_func(data1)

I need to make several copies of the same kind of plot and would like all of them to have a little arrow below the x-axis to help the reader. However, the only way I have found of placing such an arrow is the geom_segment element which requires me to specify start and end points which change according to the range of data.

Ideally, if I could access the values at which ggplot2 places tickmarks along the x-axis, that would solve my problem as I could use those as start and end points.

A minimal example is:

library(dplyr)
library(tidyr)
library(ggplot2)

data <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

data1 <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  filter(mpg > 15) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

facet_df <- data.frame(
  type = "mpg"
)

plot_func <- function(df) {
  df %>% ggplot +
    geom_point(aes(x = val, y = names)) +
    facet_wrap(facets = vars(type), ncol = 3, scales = "free_x") +
    geom_segment(data = facet_df, x = 11, xend = 14, y = -1, yend = -1,
                 arrow = arrow(length = unit(3, "pt"))) +
    coord_cartesian(clip = "off")
}

plot_func(data)
plot_func(data1)

plot_func(data)

enter image description here

plot_func(data1)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2025-02-20 14:55:49
library(dplyr)
library(tidyr)
library(grid)
library(ggplot2)

data <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

data1 <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  filter(mpg > 15) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

facet_df <- data.frame(
  type = "mpg"
)

plot_func <- function(df) {
  p <- df %>% ggplot +
    geom_point(aes(x = val, y = names)) +
    facet_wrap(facets = vars(type), ncol = 3, scales = "free_x") +
    # xlab(expression(atop("val", symbol('\256'))))+
    labs(
     x = NULL
    ) +
    coord_cartesian(clip = "off") +
    theme(
      plot.margin=unit(c(0.3,0.3,1.4,0.3), "cm")
    )
  print(p)
  grid.lines(x = c(0.4, 0.7), 
             y = c(0.08, 0.08),
             arrow = arrow(type="open", ends="last", angle = 40,
                          length=unit(3,"mm")))
  grid.text(x = 0.58, y = 0.035, label = "val", )
}

plot_func(data)

“”

plot_func(data1)

https://i.sstatic.net/qjdxv.png >由 preprex package (v2.0.1)

对您有用吗?

library(dplyr)
library(tidyr)
library(grid)
library(ggplot2)

data <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

data1 <- mtcars %>% 
  mutate(names = rownames(mtcars)) %>% 
  filter(mpg > 15) %>% 
  pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>% 
  select(names:val)

facet_df <- data.frame(
  type = "mpg"
)

plot_func <- function(df) {
  p <- df %>% ggplot +
    geom_point(aes(x = val, y = names)) +
    facet_wrap(facets = vars(type), ncol = 3, scales = "free_x") +
    # xlab(expression(atop("val", symbol('\256'))))+
    labs(
     x = NULL
    ) +
    coord_cartesian(clip = "off") +
    theme(
      plot.margin=unit(c(0.3,0.3,1.4,0.3), "cm")
    )
  print(p)
  grid.lines(x = c(0.4, 0.7), 
             y = c(0.08, 0.08),
             arrow = arrow(type="open", ends="last", angle = 40,
                          length=unit(3,"mm")))
  grid.text(x = 0.58, y = 0.035, label = "val", )
}

plot_func(data)

plot_func(data1)

Created on 2022-07-07 by the reprex package (v2.0.1)

Does this work for You??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文