gpt4 book ai didi

r - 如何使用ggplot2修改极坐标中堆积条形图的y轴的限制?

转载 作者:行者123 更新时间:2023-12-04 12:27:49 24 4
gpt4 key购买 nike

我在极坐标中绘制了一个带有“位置 = 堆栈”的堆积条形图。

ggplot(d, aes(Component, Freq, fill = Subtype2)) +
geom_bar(position = 'stack', stat = 'identity') +
scale_x_discrete(expand = c(0.03, 0.5)) +
scale_y_continuous('Distribution', expand = c(0, 0)) +
scale_fill_nejm() +
guides(fill = guide_legend(title = 'IHC subtype')) +
coord_polar(theta = 'y') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(size = 0.25),
axis.ticks = element_line(size = 0.25),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10))
enter image description here
要在 y 轴的起点和终点之间添加一个空格,我添加 limits = c(0, 2000)scale_y_continuous .
ggplot(d, aes(Component, Freq, fill = Subtype2)) +
geom_bar(position = 'stack', stat = 'identity') +
scale_x_discrete(expand = c(0.03, 0.5)) +
scale_y_continuous('Distribution', expand = c(0, 0), limits = c(0, 2000)) +
scale_fill_nejm() +
guides(fill = guide_legend(title = 'IHC subtype')) +
coord_polar(theta = 'y') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(size = 0.25),
axis.ticks = element_line(size = 0.25),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10))
enter image description here
接下来,我想在极坐标中绘制一个比例条形图,我尝试了下面的代码:
ggplot(d, aes(Component, Freq, fill = Subtype2)) +
geom_bar(position = 'fill', stat = 'identity') +
scale_x_discrete(expand = c(0.03, 0.5)) +
scale_y_continuous('Distribution', expand = c(0, 0)) +
scale_fill_nejm() +
guides(fill = guide_legend(title = 'IHC subtype')) +
coord_polar(theta = 'y') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(size = 0.25),
axis.ticks = element_line(size = 0.25),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10))
enter image description here
我想使用以下代码在 y 轴的开始和结束之间添加一个空格,但输出很奇怪,带有警告消息。
ggplot(d, aes(Component, Freq, fill = Subtype2)) +
geom_bar(position = 'fill', stat = 'identity') +
scale_x_discrete(expand = c(0.03, 0.5)) +
scale_y_continuous('Distribution', expand = c(0, 0), limits = c(0, 100)) +
scale_fill_nejm() +
guides(fill = guide_legend(title = 'IHC subtype')) +
coord_polar(theta = 'y') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(size = 0.25),
axis.ticks = element_line(size = 0.25),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10))
Warning message:
Removed 6 rows containing missing values (position_stack).
enter image description here
数据
structure(list(Component = structure(c(11L, 10L, 9L, 8L, 11L, 
10L, 9L, 8L, 11L, 10L, 9L, 8L, 11L, 10L, 9L), .Label = c("10",
"9", "8", "7", "6", "5", "4", "3", "2", "1", "0"), class = "factor"),
Subtype2 = structure(c(3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 4L, 4L, 4L), .Label = c("HR+/HER2-", "HR+/HER2+",
"HR-/HER2+", "TNBC"), class = "factor"), Freq = c(12L, 433L,
67L, 66L, 87L, 461L, 287L, 84L, 17L, 332L, 60L, 32L, 44L,
278L, 159L)), row.names = c(1L, 2L, 3L, 4L, 12L, 13L, 14L,
15L, 23L, 24L, 25L, 26L, 34L, 35L, 36L), class = "data.frame")

最佳答案

这比我想象的要棘手一些。 y-scale 似乎想在计算位置之前扔掉超出限制的值。为了防止这种情况,您可以使用 scales::oob_keep()保留这些值。下面的简化示例。

library(ggplot2)

# d <- structure(...) # taken from question, omitted for brevity

ggplot(d, aes(Component, Freq, fill = Subtype2)) +
geom_col(position = "fill") +
scale_y_continuous(limits = c(0, 1.1), oob = scales::oob_keep) +
coord_polar(theta = "y")

关于r - 如何使用ggplot2修改极坐标中堆积条形图的y轴的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69265403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com