具有离散X的GGPLOT片段:降低未使用的级别并获得与组数成正比的量表

发布于 2025-02-04 16:30:04 字数 853 浏览 2 评论 0 原文

让我们以虹膜数据集为例。但是哦,不!我们忘了测量setosa花的花瓣...

# pivot_longer for plot
i2 <- iris %>%
  pivot_longer(-Species, names_to='parameter', values_to='measure')
i2

# deleting petal measures of setosa
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Length'),]
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Width'),]

现在我们绘制了由物种的不同参数。由于我们没有setosa的花瓣措施,因此我们使用scales ='free_x',因此它会降低未使用的级别。因此:

ggplot(i2, aes(x=parameter, y=measure, colour=parameter)) +
  geom_point() +
  facet_wrap(~Species, nrow=1, scales='free_x')

现在,我的问题是:我希望每个图的宽度与绘制的组数量成正比,即Setosa图应比其他物种薄的两倍,因为仅绘制了两个参数。

我该如何实现?

Let's take the iris dataset as example. But oh, no! We forgot to measure the petals of setosa flowers...

# pivot_longer for plot
i2 <- iris %>%
  pivot_longer(-Species, names_to='parameter', values_to='measure')
i2

# deleting petal measures of setosa
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Length'),]
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Width'),]

Now we are plotting the different parameters, faceted by species. As we do not have the petal measures for setosa, we use scales='free_x' so it drops the unused levels. As so:

ggplot(i2, aes(x=parameter, y=measure, colour=parameter)) +
  geom_point() +
  facet_wrap(~Species, nrow=1, scales='free_x')

Now, my issue is: I want the width of each plot to be proportional to the number of groups plotted, i.e. the setosa plot should be about twice thinner than the other species, as only two parameters are plotted.

How can I achieve this?

enter image description here

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

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

发布评论

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

评论(1

你的笑 2025-02-11 16:30:04

facet_grid()中,您可以设置 space =“ free_x” 具有比例x轴。

library(tidyverse)

# pivot_longer for plot
i2 <- iris %>%
  pivot_longer(-Species, names_to='parameter', values_to='measure')

# deleting petal measures of setosa
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Length'),]
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Width'),]

ggplot(i2, aes(x=parameter, y=measure, colour=parameter)) +
  geom_point() +
  facet_grid(~Species, scales='free_x', space = "free_x")

“”

在2022-06-06上由 reprex软件包(v2.0.1)

In facet_grid(), you can set space = "free_x" to have proportional x-axes.

library(tidyverse)

# pivot_longer for plot
i2 <- iris %>%
  pivot_longer(-Species, names_to='parameter', values_to='measure')

# deleting petal measures of setosa
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Length'),]
i2 <- i2[-which(i2$Species=='setosa' & i2$parameter=='Petal.Width'),]

ggplot(i2, aes(x=parameter, y=measure, colour=parameter)) +
  geom_point() +
  facet_grid(~Species, scales='free_x', space = "free_x")

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

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