使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

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

Trouble making a particular sampling schematic diagram using ggplot2 and ggforce

问题

以下是您要翻译的内容:

Original sampling schematic figure(原始采样示意图)

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

Geom_mark_rect from ggforce has been super helpful for creating boxes around the collections of points.(ggforce的Geom_mark_rect非常有助于在点集周围创建框。)

However, I've been having trouble making the boxes fit more tightly around the points (and not being forced to squares); when I try to make a similarly narrow version of the R figure, the boxes overlap:(但是,我在让框更紧密地适应点(而不是被迫成为正方形)方面遇到了困难;当我尝试创建一个类似狭窄版本的R图时,框会重叠:)

Current R sampling schematic figure(当前的R采样示意图)

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

It would be great if there were a way to make the geom_mark_rect boxes tighter around the points; any advice on parameters to change or things to try would be greatly appreciated. Or, other creative approaches to trying to make this diagram via ggplot would be so helpful!(如果有一种方法可以让geom_mark_rect的框更紧密地包围点,那将会很棒;对于要更改的参数或尝试的事项的任何建议都将不胜感激。或者,通过ggplot尝试制作这个图表的其他创造性方法将非常有帮助!)

The code used to produce the current R figure is below, where data_forchart can be imported from the attached .csv:(用于生成当前R图的代码如下,其中data_forchart可以从附加的.csv文件中导入:)

facet.labs =  c("Tide Pool 1\n (S1)", "Tide Pool 2\n (S2)", "Nearshore\n (N)")
names(facet.labs) = c("S1", "S2", "N")

data_forchart$Location= factor(data_forchart$Location, levels=c("S1", "S2", "N"))
data_forchart$chart_num= factor(data_forchart$chart_num, levels=c("3", "2", "1"))

sample_schema = ggplot(data = data_forchart, aes(x = Time, y = chart_num, shape = Location, color = Time, fill = Time)) + 
  geom_beeswarm(cex= 2, size = 3) + 
  geom_mark_rect(expand = .03) +
  scale_shape_manual(values = c(15, 17, 16)) + 
  viridis::scale_color_viridis(discrete=TRUE, begin = 0, end = .97, direction = -1) +
  viridis::scale_fill_viridis(discrete=TRUE, begin = 0, end = .97, direction = -1) +
  facet_grid(Location ~., scales = "free_y", space = "free_y", switch = "y", labeller = labeller(Location = facet.labs)) +
  theme_void(base_size = 16) + 
  theme(legend.position = "none") +
  theme(strip.text.y.left = element_text(angle = 0, hjust = 1, vjust = .9)) 

data_forchart

英文:

I've been trying to make a reproducible version of a diagram of the sampling scheme for a particular field project, mirroring this version that I made by hand in PowerPoint:

Original sampling schematic figure

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

Geom_mark_rect from ggforce has been super helpful for creating boxes around the collections of points. However, I've been having trouble making the boxes fit more tightly around the points (and not being forced to squares); when I try to make a similarly narrow version of the R figure, the boxes overlap:

Current R sampling schematic figure

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

It would be great if there were a way to make the geom_mark_rect boxes tighter around the points; any advice on parameters to change or things to try would be greatly appreciated. Or, other creative approaches to trying to make this diagram via ggplot would be so helpful!

The code used to produce the current R figure is below, where data_forchart can be imported from the attached .csv:

facet.labs =  c("Tide Pool 1\n (S1)", "Tide Pool 2\n (S2)", "Nearshore\n (N)")
names(facet.labs) = c("S1", "S2", "N")

data_forchart$Location= factor(data_forchart$Location, levels=c("S1", "S2", "N"))
data_forchart$chart_num= factor(data_forchart$chart_num, levels=c("3", "2", "1"))


sample_schema = ggplot(data = data_forchart, aes(x = Time, y = chart_num, shape = Location, color = Time, fill = Time)) + 
  geom_beeswarm(cex= 2, size = 3) + 
  geom_mark_rect(expand = .03) +
  scale_shape_manual(values = c(15, 17, 16)) + 
  viridis::scale_color_viridis(discrete=TRUE, begin = 0, end = .97, direction = -1) +
  viridis::scale_fill_viridis(discrete=TRUE, begin = 0, end = .97, direction = -1) +
  facet_grid(Location ~., scales = "free_y", space = "free_y", switch = "y", labeller = labeller(Location = facet.labs)) +
  theme_void(base_size = 16) + 
  theme(legend.position = "none") +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1, vjust = .9)) 

data_forchart

答案1

得分: 3

看起来调整 geom_mark_rect 的特定纵横比相当繁琐。如果 geom_rect 能够有圆角选项就好了,但实际上没有这个选项。您可以改用 ggh4x 中的 geom_box 代替。

library(ggforce)
library(ggh4x)
library(ggbeeswarm)

ggplot(data = data_forchart, aes(x = Time, y = chart_num, shape = Location,
                                 color = Time, 
                                 fill = after_scale(alpha(colour, 0.05)))) + 
  geom_beeswarm(cex = 2, size = 4) + 
  geom_box(aes(xmin = after_stat(x) - 0.45, 
                             xmax = after_stat(x) + 0.45,
                             ymin = after_stat(y) - 0.4, 
                             ymax = after_stat(y) + 0.45),
                         radius = unit(5, "pt")) +
  scale_shape_manual(values = c(15, 17, 16)) + 
  scale_color_viridis_d(direction = -1) +
  facet_grid(Location ~., scales = "free_y", space = "free_y", 
             switch = "y", labeller = labeller(Location = facet.labs)) +
  theme_void(base_size = 16) + 
  theme(legend.position = "none",
        strip.text.y.left = element_text(angle = 0, hjust = 1, vjust = .9)) 

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

英文:

It seems quite fiddly getting geom_mark_rect to adopt a particular aspect ratio. It would be nice if geom_rect had the option to have rounded corners, but it doesn't. You can instead use geom_box from ggh4x

library(ggforce)
library(ggh4x)
library(ggbeeswarm)

ggplot(data = data_forchart, aes(x = Time, y = chart_num, shape = Location,
                                 color = Time, 
                                 fill = after_scale(alpha(colour, 0.05)))) + 
  geom_beeswarm(cex = 2, size = 4) + 
  geom_box(aes(xmin = after_stat(x) - 0.45, 
                             xmax = after_stat(x) + 0.45,
                             ymin = after_stat(y) - 0.4, 
                             ymax = after_stat(y) + 0.45),
                         radius = unit(5, "pt")) +
  scale_shape_manual(values = c(15, 17, 16)) + 
  scale_color_viridis_d(direction = -1) +
  facet_grid(Location ~., scales = "free_y", space = "free_y", 
             switch = "y", labeller = labeller(Location = facet.labs)) +
  theme_void(base_size = 16) + 
  theme(legend.position = "none",
        strip.text.y.left = element_text(angle = 0, hjust = 1, vjust = .9)) 
 

使用ggplot2和ggforce制作特定抽样示意图时遇到问题。

huangapple
  • 本文由 发表于 2023年7月18日 05:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708274.html
匿名

发表评论

匿名网友

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

确定