如何测试图在R

我想知道是否有一种方法可以问R图是否具有固定的宽高比。这是两个示例图:

library (ggplot2)

plot_a <- ggplot(iris,aes(Sepal.Length,Sepal.Width,color = Species)) +
          geom_point()+
          theme (aspect.ratio = 1)

plot_b <- ggplot(iris,color = Species)) +
          geom_point()

plot_a的纵横比固定,而plot_b的纵横比没有固定。我正在寻找以下虚函数:

is_fixed_ratio (plot_a) 
TRUE

is_fixed_ratio (plot_b)
FALSE

有没有办法做到这一点?

W450424694 回答:如何测试图在R

假设我们有以下情节:

library (ggplot2)

plot_a <- ggplot(iris,aes(Sepal.Length,Sepal.Width,color = Species)) +
          geom_point()+
          theme (aspect.ratio = 1)

plot_b <- ggplot(iris,color = Species)) +
          geom_point()

plot_c <- ggplot(iris,color = Species)) +
          geom_point() +
          coord_equal()

当前接受的答案将对这些问题进行如下处理:

is_fixed_ratio <- function(plot){
  purrr::map(plot,"aspect.ratio") %>% 
    unlist() %>% 
    is.null() %>% 
    !.
}
> is_fixed_ratio(plot_a)
[1] TRUE
> is_fixed_ratio(plot_b)
[1] FALSE
> is_fixed_ratio(plot_c)
[1] FALSE

plot_c的宽高比是固定的,只是没有通过主题指定。

要对此进行检查,您可以检查图的gtable:

is_fixed_ratio2 <- function(plot) {
  ggplotGrob(plot)$respect
}
> is_fixed_ratio2(plot_a)
[1] TRUE
> is_fixed_ratio2(plot_b)
[1] FALSE
> is_fixed_ratio2(plot_c)
[1] TRUE
,

已编辑以检查任何长宽比

#writing to the file
password="example" 
password=password.encode('cp037') 
password=str(password)
f=open("passwordFile.txt","w+")
f.write(password)
f.close()

#reading from the file
f=open("passwordFile.txt","r")
password=f.read()
#this is where I need to turn password back into bytes
# -------
# we converts the string 
# from "b'\x85\xa7\x81\x94\x97\x93\x85'" 
# to "85a78194979385"
# then pass it to the bytes.fromhex to get the bytes objet.
password=bytes.fromhex(password[2:-1].replace('\\x','')).decode('cp037')
# -------
print(password)
,

处理此类问题的一种非常不错的方法是使用map软件包中的purrr。这样,您不必手动在列表中搜索:

is_fixed_ratio <- function(plot){
  purrr::map(plot,"aspect.ratio") %>% 
    unlist() %>% 
    is.null() %>% 
    !.
}
is_fixed_ratio(plot_a)
is_fixed_ratio(plot_b)
,

查询主题的答案是不可靠的,因为绘图可以具有固定的宽高比,仅仅是因为坐标使它固定,而与主题设置无关。例如,任何基于geom_sf()的图都将具有固定的宽高比。正确的方法是查询ggplot生成的grob。

library(tidyverse)

p_var <- ggplot(iris,Sepal.Width)) +
  geom_point()

p_fixed <- p_var + coord_fixed()


# correct approach: query the grob
is_fixed_ratio <- function(plot) {
  g <- ggplotGrob(plot)
  isTRUE(g$respect)
}

# should return false
is_fixed_ratio(p_var)
#> [1] FALSE

# should return true
is_fixed_ratio(p_fixed)
#> [1] TRUE

相比之下,如果我们尝试使用错误的方法,那么事情将无法按预期进行。

# incorrect approach: rely on a theme setting
is_fixed_ratio_wrong <- function(plot) {
  purrr::map(plot,"aspect.ratio") %>% 
    unlist() %>% 
    is.null() %>% 
    !.
}

# should return false,and does so
is_fixed_ratio_wrong(p_var)
#> [1] FALSE

# should return true,but doesn't
is_fixed_ratio_wrong(p_fixed)
#> [1] FALSE

这也适用于问题中给出的示例:

plot_a <- ggplot(iris,color = Species)) +
  geom_point()+
  theme(aspect.ratio = 1)

plot_b <- ggplot(iris,color = Species)) +
  geom_point()

is_fixed_ratio(plot_a)
#> [1] TRUE

is_fixed_ratio(plot_b)
#> [1] FALSE

另一个例子:

nc <- sf::st_read(system.file("shape/nc.shp",package = "sf"),quiet = TRUE)
p <- ggplot(nc) +
  geom_sf(aes(fill = AREA))

is_fixed_ratio(p)
#> [1] TRUE

is_fixed_ratio_wrong(p)
#> [1] FALSE
,

我不确定功能,但是有一种“快速而肮脏的”方式来检查此功能。如果展开图子窗口,则没有固定长宽比的图将拉伸和扩展,而具有固定长宽比的图将保持相同的宽度。

您还可以直接使用ggsave指定绘图的比例,宽度和高度,例如

var filtered=devices.Where(q => q.GetType() == typeof(GridDevice) && (q as GridDevice).Voltage==0);

您能否指定要如何检查地块?

本文链接:https://www.f2er.com/3151171.html

大家都在问