Defining y limits on a x-zoom graph with facet_zoom

huangapple go评论57阅读模式
英文:

Defining y limits on a x-zoom graph with facet_zoom

问题

我试图制作一个缩放后的图表,使用geom_point()geom_smooth()图表的 x 轴。我可以很容易地获得我想要的图表,只需输入:

facet_zoom(xlim = c(0.05, 0.2))

但我也想有可能定义缩放图表的 ylim,保持相同的配置。问题是,如果我也添加 ylim(),我会在 y 轴上得到缩放后的图表,而不再是在 x 轴上。我阅读了一些在线教程和指南,但我无法在 y 轴图表上获得简单的 ylim 定义。

是否有任何选项可以实现这一点?我向您报告代码和我获得的图表,以及我进行的两次不成功的尝试。

以下是运行的代码:

# 数据集创建和名称分配
Dati <- data.frame(V1 = c(0.2, 0.3, 0.4, 0.5, 0.1, 0.05, 0.1, 0),
                   V2 = c(11.25, 7.15, 5.5, 2.5, 30.75, 55.5, 34.5, 97))
colnames(Dati) <- c("cat", "vix")

# 包加载
library(ggplot2)
attach(Dati)

library(ggforce)
library(tidyverse)

# 创建图表
Graph1 <- ggplot(data=Dati, aes(x=cat, y=vix)) + geom_point(aes(color = "red"), shape = 1, size = 3.0, stroke = 1.5) + 
scale_color_manual(values=c('#f92410', '#644196')) + geom_smooth(aes(level=0.95, span = 0.8))  + 
theme(legend.position = "null") + 
geom_vline(xintercept = 0.2,  linetype="dashed", color = "red") +
facet_zoom(xlim = c(0.05, 0.2))

#facet_zoom(ylim = c(0, 80), xlim = c(0.05, 0.2), split = TRUE) #它会创建一个分割成4个图表的图
#facet_zoom(ylim = c(0, 80), xlim = c(0.05, 0.2)) #它会将缩放转移到y轴

Graph1

输出的图表:

Defining y limits on a x-zoom graph with facet_zoom

我想要通过定义 y 轴限制来获得的图表:

Defining y limits on a x-zoom graph with facet_zoom

英文:

I am trying to make a zoomed graph, on a x-axis of a geom_point() + geom_smooth() graph. I am able to obtain quite easily a graph I am looking for, just typing:

facet_zoom(xlim = c(0.05, 0.2))

But I would like to have the possibility to define also the ylim of the zoomed graph, mantaining the same configuration. The problem is that if I add also ylim() I get the zoomed graph on the y axis and not longer on the x one. I read some tutorial and guides online but I was not able to obtain a simple ylim definition on the y-axis graph.

Is there any options allwing to to that? I report you the code and the graph I got with the one I would like to obtain + two unsuccessfull trials I made.

Below, the running code:

#DATASET CREATION AND NAMES ASSIGNATION
Dati &lt;- data.frame(V1 = c(0.2, 0.3, 0.4, 0.5, 0.1, 0.05, 0.1, 0),
                   V2 = c(11.25, 7.15, 5.5, 2.5, 30.75, 55.5, 34.5, 97))
colnames(Dati) &lt;- c(&quot;cat&quot;, &quot;vix&quot;)


#PACKAGES LOADING
library(ggplot2)
attach(Dati)

library(ggforce)
library(tidyverse)


#GRAPH CREATION
Graph1 &lt;- ggplot(data=Dati, aes(x=`cat`, y=`vix`)) + geom_point(aes(color = &quot;red&quot;), shape = 1, size = 3.0, stroke = 1.5) + 
scale_color_manual(values=c(&#39;#f92410&#39;, &#39;#644196&#39;)) + geom_smooth(aes(level=0.95, span = 0.8))  + 
theme(legend.position = &quot;null&quot;) + 
geom_vline(xintercept = 0.2,  linetype=&quot;dashed&quot;, color = &quot;red&quot;) +
facet_zoom(xlim = c(0.05, 0.2))

#facet_zoom(ylim = c(0, 80), xlim = c(0.05, 0.2), split = TRUE) #it creates a split with 4 graphs
#facet_zoom(ylim = c(0, 80), xlim = c(0.05, 0.2)) #it transfers on the y-axis the zoom

Graph1

Output graph:

Defining y limits on a x-zoom graph with facet_zoom

Graph I would like to get, by defining the y-limits:

Defining y limits on a x-zoom graph with facet_zoom

答案1

得分: 1

你可以通过设置ylim,同时将horizontal = FALSE来将缩放区域放在图形下方,并最后(如果需要的话)添加theme(zoom.y = element_rect(fill = NA))来消除y轴上的缩放填充效果:

library(ggplot2)
library(ggforce)

ggplot(data = Dati, aes(x = `cat`, y = `vix`)) +
  geom_point(aes(color = "red"), shape = 1, size = 3.0, stroke = 1.5) +
  scale_color_manual(values = c("#f92410", "#644196")) +
  geom_smooth(level = 0.95, span = 0.8) +
  theme(legend.position = "null") +
  geom_vline(xintercept = 0.2, linetype = "dashed", color = "red") +
  facet_zoom(xlim = c(0.05, 0.2), ylim = c(0, 80), horizontal = FALSE) +
  theme(zoom.y = element_rect(fill = NA))

Defining y limits on a x-zoom graph with facet_zoom


[![enter image description here][1]][1]

<details>
<summary>英文:</summary>

You could achieve your desires result by setting the `ylim` but also `horizontal = FALSE` to put the zoom area below the plot and finally (if desired) by adding `theme(zoom.y = element_rect(fill = NA))` to get rid of the fill indicating the zoom on the y axis:

library(ggplot2)
library(ggforce)

ggplot(data = Dati, aes(x = cat, y = vix)) +
geom_point(aes(color = "red"), shape = 1, size = 3.0, stroke = 1.5) +
scale_color_manual(values = c("#f92410", "#644196")) +
geom_smooth(level = 0.95, span = 0.8) +
theme(legend.position = "null") +
geom_vline(xintercept = 0.2, linetype = "dashed", color = "red") +
facet_zoom(xlim = c(0.05, 0.2), ylim = c(0, 80), horizontal = FALSE) +
theme(zoom.y = element_rect(fill = NA))


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/scl3u.png

</details>



huangapple
  • 本文由 发表于 2023年5月22日 19:04:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定