ggplot中还有其他任何可填充的形状吗? [英] Are there any other additional fillable shapes in ggplot?

查看:70
本文介绍了ggplot中还有其他任何可填充的形状吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有除默认形状(可以在 ggpubr :: show_point_shapes()上查看或在

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

解决方案

此处使用GeomPolygon进行水平交叉时的统计数据非常粗糙.这只是为了演示一种非常简单直接的方法如何实现此目的.这不是一个好数据,因为

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

但这可能是一个不错的开始...

 库(tidyverse)StatCross<-ggproto("StatCross",Stat,compute_group = function(data,scales,width = 0.1,params){x<-数据$ xy<-数据$ ynew_x<-c(rep(x + width,2),rep(x + 4 *宽度,2),rep(x + width,2),rep(x-width,2),rep(x-4 * width,2),rep(x-宽度,2))new_y<-c(y + 4 *宽度,rep(y + width,2),rep(y-width,2),rep(y-4 * width,2),rep(y-width,2),rep(y + width,2),y + 4 *宽度)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,宽度= 0.1,...){层(stat = StatCross,data =数据,mapping =映射,geom = geom,position =位置,show.legend = show.legend,Inherit.aes = Inherit.aes,参数=列表(na.rm = na.rm,宽度=宽度,...))}虹膜%>%mutate(index = row_number())%&%ggplot(aes(Sepal.Length,Sepal.Width,group = index))+stat_cross(宽度= 0.01,填充=黄色",颜色=黑色") 

reprex软件包(v1.0.0)创建于2021-03-15 sup>

I am wondering if there are any additional fillable shapes to plot in ggplot, other than the default ones (which can be viewed running ggpubr::show_point_shapes(), or seen here).

According to those sources, the only shapes that can be filled are a square, circle, diamond, and triangle. I would like the ability to plot a fillable cross or "x" shape. By "fillable", I mean the shape should have enough thickness to it where a label could be added inside.

The yellow and green shapes are what I would like to be plottable. Is this possible, or should I look at other options?

解决方案

Here a very crude stat using GeomPolygon for a horizontal cross. This is just to demonstrate a very simple and straight forward approach how to implement this. It is not a great stat, because

  1. the proper proportions depend on the scale (you basically need to set coord_equal),
  2. You need to be very specific with the groups

but it might be a good start...

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")

Created on 2021-03-15 by the reprex package (v1.0.0)

这篇关于ggplot中还有其他任何可填充的形状吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆