【问题标题】:Are there any other additional fillable shapes in ggplot?ggplot 中还有其他可填充的形状吗?
【发布时间】:2021-03-16 06:35:42
【问题描述】:

我想知道除了默认形状(可以在运行ggpubr::show_point_shapes() 或查看here 时查看)之外,是否还有其他可填充形状可以在 ggplot 中绘制。

根据这些资料,唯一可以填充的形状是正方形、圆形、菱形和三角形。我希望能够绘制一个可填充的十字形或“x”形。通过“可填充”,我的意思是形状应该有足够的厚度,可以在里面添加标签。

黄色和绿色的形状是我想要绘制的。这可能吗,还是我应该看看其他选择?

【问题讨论】:

  • 除了 unicode 方式 - 我还能想到其他两种方式,我猜两者都更稳定和更少平台 - / 设备 - 依赖 1) 将 svg 文件绘制为点 stackoverflow.com/questions/61387217/… 2)创建一个新的几何体

标签: r ggplot2 shapes


【解决方案1】:

如果您找到喜欢的 Unicode 符号,则可以将其用作标签:

library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_text(label = ifelse(mtcars$gear >3, "\u2716", "\u271A"),
              aes(vjust=.5, hjust=.5, family="DejaVu Sans"), 
              size=12, 
              col = ifelse(mtcars$gear >3, "lightgreen", "yellow"))+
    geom_text(aes(label=gear, vjust=.5, hjust=.5))+
    theme_bw()

由reprex package (v1.0.0) 于 2021-03-15 创建

您也可以使用\u2727、\u271C 或其他您喜欢的字符。

【讨论】:

  • 虽然这是一个好主意,+1,但我个人觉得使用 unicode 绘图非常令人沮丧,因为它在我的计算机上无法运行。 Stackoverflow 中的很多线程显示 - 我并不孤单
  • 我还在寻找能够轻松地用黑色或红色勾勒出每个图的能力,具体取决于我表中的另一列。要使用 unicode 执行此操作,您需要在 ggplot 函数中放置另一个形状稍大的 geom_plot - 它通常不适合。
【解决方案2】:

这是一个使用 GeomPolygon 进行水平交叉的非常粗略的统计数据。这只是为了演示一种非常简单直接的方法来实现这一点。这不是一个很好的统计数据,因为

  1. 合适的比例取决于比例(基本上需要设置coord_equal),
  2. 您需要对组非常具体

但这可能是一个好的开始......

library(tidyverse)
StatCross <- ggproto("StatCross", Stat,
                     compute_group = function(data, scales, width = .1, params) {
                       x <- data$x
                       y <- data$y
                       new_x <- c(rep(x + width, 2),
                                      rep(x + 4 * width, 2),
                                      rep(x + width, 2),
                                      rep(x - width, 2),
                                      rep(x - 4 * width, 2),
                                      rep(x - width, 2))
                       new_y <- c(y + 4* width,
                                        rep(y + width, 2),
                                        rep(y - width, 2),
                                        rep(y - 4*width, 2),
                                        rep(y - width, 2),
                                        rep(y + width, 2),
                                        y + 4*width)
                       cross_data <- data.frame(x = new_x, y = new_y)
                       cross_data
                     },
                     
                     required_aes = c("x", "y")
)
stat_cross <- function(mapping = NULL, data = NULL, geom = "polygon",
                       position = "identity", na.rm = FALSE, show.legend = NA, 
                       inherit.aes = TRUE, width = 0.1, ...) {
  layer(
    stat = StatCross, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, width = width, ...)
  )
}
iris %>% 
  mutate(index = row_number()) %>%
ggplot(aes(Sepal.Length, Sepal.Width, group = index)) +
  stat_cross(width = .01,  fill = "yellow", color = "black")

由reprex package (v1.0.0) 于 2021-03-15 创建

【讨论】:

  • 这很有趣,并且可以工作。黑色轮廓的宽度如何控制?
  • 因为它是基于 GeomPolygon 的,你可以通过 size 参数来控制它
  • 这对我有用,所以我会勾选这个答案。对于未来的读者,size 参数应该写在stat_cross() 函数中(我把它放在fill 和color 参数之后)。请注意,我的数据集很小(
猜你喜欢
  • 2013-11-27
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多