将ggplot散点图的坐标轴手动设置为与geom_rect元素对齐

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

Setting the axes of a ggplot scatterplot to manually line up with geom_rect elements

问题

I'm trying to make a scatter plot with a log scale that has 3 geom_rect() features laying out different "bins" for some density features.

Whenever I add the geom_rect() features, it increases the limits of the x and y axis. Ideally, I'd like the rectangles to line up with the maximum x and y value displayed on the chart, removing all white space. Whenever I tried to set the limits on the axes using the following plot elements, it just results in an error:

scale_x_continuous(trans='log2', limits = c(0, 8000)) +
scale_y_continuous(trans='log2', limits = c(0, 8000)) +

Here's an example of the chart:

将ggplot散点图的坐标轴手动设置为与geom_rect元素对齐

And here's the code that was used in its production:

ggplot(final_df, aes(x=as.numeric(stroma), y=as.numeric(epi), 
                     label = ID)) +
  geom_rect(aes(xmin=0, xmax=stroma_cutoff, ymin=0, ymax = epi_cutoff),
            alpha = 0.005, fill = "blue2") +
  geom_rect(aes(xmin=stroma_cutoff, xmax=8000, ymin=0, ymax = epi_cutoff),
            alpha = 0.005, fill = "yellow1") +
  geom_rect(aes(xmin=0, xmax=8000, ymin=epi_cutoff, ymax = 4000),
            alpha = 0.005, fill = "red1") +
  geom_point(size = 2) +
  geom_point(data = crc_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'green',
             size = 3) +
  geom_point(data = crc_met_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'purple',
             size = 3) +
  scale_x_continuous(trans='log2') +
  scale_y_continuous(trans='log2') +
  labs(y='Feature1',x='Feature2',
       title = 'Chart\n
       blue2 0.005 - yellow1 0.005 - red1 0.005') +
  theme_bw()

And here's the structure of the dataframes used.

dput(head(final_df))
dput(head(crc_vals))
dput(head(crc_met_vals))

Both epi_cutoff and stroma_cutoff are 200.

英文:

I'm trying to make a scatter plot with a log scale that has 3 geom_rect() features laying out different "bins" for some density features.

Whenever I add the geom_rect() features, it increases the limits of the x and y axis. Ideally, I'd like the rectangles to line up with the maximum x and y value displayed on the chart, removing all white space. Whenever I tried to set the limits on the axes using the following plot elements, it just results in an error:

> Error in seq.default(min, max, by = by) : 'from' must be a finite number"

  scale_x_continuous(trans='log2', limits = c(0, 8000)) +
  scale_y_continuous(trans='log2', limits = c(0, 8000)) +

Here's an example of the chart:

将ggplot散点图的坐标轴手动设置为与geom_rect元素对齐

And here's the code that was used in its production.

ggplot(final_df, aes(x=as.numeric(stroma), y=as.numeric(epi), 
                     label = ID)) +
  geom_rect(aes(xmin=0, xmax=stroma_cutoff, ymin=0, ymax = epi_cutoff),
            alpha = 0.005, fill = "blue2") +
  geom_rect(aes(xmin=stroma_cutoff, xmax=8000, ymin=0, ymax = epi_cutoff),
            alpha = 0.005, fill = "yellow1") +
  geom_rect(aes(xmin=0, xmax=8000, ymin=epi_cutoff, ymax = 4000),
            alpha = 0.005, fill = "red1") +
  geom_point(size = 2) +
  geom_point(data = crc_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'green',
             size = 3) +
  geom_point(data = crc_met_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'purple',
             size = 3) +
  scale_x_continuous(trans='log2') +
  scale_y_continuous(trans='log2') +
  labs(y='Feature1',x='Feature2',
       title = 'Chart\n
       blue2 0.005 - yellow1 0.005 - red1 0.005') +
  theme_bw()

And here's the structure of the dataframes used.

> dput(head(final_df))
structure(list(ID = c("USL-2022-41628", "USL-2022-41637", "USL-2022-41640", 
"USL-2022-41659", "USL-2022-41666", "USL-2022-41669"), stroma = c(639.7364171, 
78.23842193, 484.908185, 263.4609547, 159.693075, 18.38209375
), epi = c(424.6941624, 55.78718892, 103.9890452, 270.4756807, 
47.79603601, 12.29226541), Indication = c("Gastric", "Gastric met", 
"Gastric met", "Esophageal met", "Gastric", "Gastric met")), na.action = structure(c(`USL-2022-42047` = 106L), class = "omit"), row.names = c(NA, 
6L), class = "data.frame")
> dput(head(crc_vals))
structure(list(ID = c("USL-2022-41663", "USL-2022-41696", "USL-2022-41698", 
"USL-2022-41727", "USL-2022-41735"), stroma = c(842.3719975, 
283.5923779, 1073.589824, 306.987757, 1217.564582), epi = c(144.1873575, 
37.71616383, 338.2043632, 52.77961909, 410.411979), Indication = c("CRC", 
"CRC", "CRC", "CRC", "CRC")), na.action = structure(c(`USL-2022-42047` = 106L), class = "omit"), row.names = c(48L, 
53L, 54L, 59L, 61L), class = "data.frame")
> dput(head(crc_met_vals))
structure(list(ID = c("USL-2022-41665", "USL-2022-41667", "USL-2022-41668", 
"USL-2022-41701", "USL-2022-41729"), stroma = c(331.6539946, 
2398.683215, 184.7736286, 8.277558758, 435.3299529), epi = c(153.3781584, 
2022.155922, 10.2111996, 9.725953771, 61.15902551), Indication = c("CRC met", 
"CRC met", "CRC met", "CRC met", "CRC met")), na.action = structure(c(`USL-2022-42047` = 106L), class = "omit"), row.names = c(49L, 
50L, 51L, 55L, 60L), class = "data.frame")

Both epi_cutoff and stroma_cutoff are 200.

答案1

得分: 1

要去除空白,您需要在您的 scale_... 函数中使用 expand(c(0,0)

由于您使用了对数刻度轴,下限不能设置为0。如果您愿意,可以使用 limits = c(1, 8000),但这与空白几乎无关。此外,由于您的 "top" geom_rect 仅到 ymax = 4000,我不会将y的限制设置为 8000。否则,您会再次出现空白。

实际上,我在x轴使用了 limits = c(0.1, 8000),在y轴使用了 limits = c(0.1, 2^12)(同时将 ymax 更改为 2^12),以获得更清晰的轴刻度并避免任何空白。

请记住,如果您的数据中有负值或零,那么在绘图之前应对数据进行变换。否则,您将丢失这些数据点(在绘图后显示的警告中查看)。您可以将每个值的 abs() 加1。然后使用不同的形状来区分负值、正值和零。轴刻度 标签 可以操作以显示正确的值,而不需要添加1。

library(ggplot2)

ggplot(final_df, aes(x=as.numeric(stroma), y=as.numeric(epi), 
                     label = ID)) +
  geom_rect(aes(xmin=0, xmax=stroma_cutoff, ymin=0, ymax = epi_cutoff),
            alpha = 0.1, fill = "blue2") +
  geom_rect(aes(xmin=stroma_cutoff, xmax=8000, ymin=0, ymax = epi_cutoff),
            alpha = 0.1, fill = "yellow1") +
  geom_rect(aes(xmin=0, xmax=8000, ymin=epi_cutoff, ymax = 2^12),
            alpha = 0.1, fill = "red1") +
  geom_point(size = 2) +
  geom_point(data = crc_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'green',
             size = 3) +
  geom_point(data = crc_met_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'purple',
             size = 3) +
  scale_x_continuous(trans='log2',  expand = c(0,0), limits = c(0.1, 8000)) + 
  scale_y_continuous(trans='log2',  expand = c(0,0), limits = c(0.1, 2^12)) + 
  labs(y='Feature1',x='Feature2',
       title = 'Chart\n
       blue2: 0.1 - yellow1: 0.1 - red1: 0.1') +
  theme_bw()
#> 警告: 在连续的x轴上引入了无限值
#> 警告: 在连续的y轴上引入了无限值
#> 在连续的y轴上引入了无限值
#> 警告: 在连续的x轴上引入了无限值

将ggplot散点图的坐标轴手动设置为与geom_rect元素对齐

于2023-05-22使用 reprex v2.0.2 创建

英文:

To remove the whitespace, you need to use expand(c(0,0) in your scale_... functions.

Since you have log-scale axes, lower limit cannot be set to 0. You can use limits = c(1, 8000) if you want to, but that has (almost) nothing to do with the whitespace. Moreover, since your "top" geom_rect only goes to ymax = 4000 I would not set the limit for y to 8000. Otherwise, you end up with whitespace again.

I actually used limits = c(0.1, 8000) for x-axis and limits = c(0.1, 2^12) for y-axis (while changing ymax to 2^12) to get cleaner axes ticks and avoid any whitespace.

Keep in mind that if you have negative values or zeros, then you should transform your data prior to plotting. Otherwise, you'll loose those data points (see the warnings shown below after plotting). You can add 1 to abs() of each value. Then use different shapes to differentiate between negative values, positive ones, and zeros. Axes tick labels can be manipulated to show the right values without that added 1.

library(ggplot2)

ggplot(final_df, aes(x=as.numeric(stroma), y=as.numeric(epi), 
                     label = ID)) +
  geom_rect(aes(xmin=0, xmax=stroma_cutoff, ymin=0, ymax = epi_cutoff),
            alpha = 0.1, fill = "blue2") +
  geom_rect(aes(xmin=stroma_cutoff, xmax=8000, ymin=0, ymax = epi_cutoff),
            alpha = 0.1, fill = "yellow1") +
  geom_rect(aes(xmin=0, xmax=8000, ymin=epi_cutoff, ymax = 2^12),
            alpha = 0.1, fill = "red1") +
  geom_point(size = 2) +
  geom_point(data = crc_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'green',
             size = 3) +
  geom_point(data = crc_met_vals,
             aes(x=as.numeric(stroma), y=as.numeric(epi)),
             color = 'purple',
             size = 3) +
  scale_x_continuous(trans='log2',  expand = c(0,0), limits = c(0.1, 8000)) + 
  scale_y_continuous(trans='log2',  expand = c(0,0), limits = c(0.1, 2^12)) + 
  labs(y='Feature1',x='Feature2',
       title = 'Chart\n
       blue2: 0.1 - yellow1: 0.1 - red1: 0.1') +
  theme_bw()
#> Warning: Transformation introduced infinite values in continuous x-axis
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Transformation introduced infinite values in continuous y-axis
#> Warning: Transformation introduced infinite values in continuous x-axis

将ggplot散点图的坐标轴手动设置为与geom_rect元素对齐<!-- -->

<sup>Created on 2023-05-22 with reprex v2.0.2</sup>

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

发表评论

匿名网友

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

确定