获取绘图区域ggplot2的笛卡尔坐标

Posted

技术标签:

【中文标题】获取绘图区域ggplot2的笛卡尔坐标【英文标题】:Get cartesian coordinates for plot area ggplot2 【发布时间】:2021-04-14 11:58:46 【问题描述】:

我想将标签放置在图例附近。

在下面的代码中,我在geom_label 中硬编码了(x,y) 值,以获得当前数据帧的所需结果:

#  Creating dataframe
library(ggplot2)
values <- c(rep(0,2), rep(2,3), rep(3,3), rep(4,3), 5, rep(6,2), 8, 9, rep(11,2) )
obs_number <- c(rep(18,18))
value_1 <- c(rep(4,18))
value_2 <- c(rep(7,18))
value_3 <- c(rep(3,18))
  
data_to_plot <- data.frame(values, obs_number, value_1, value_2, value_3)
#  Calculate max frequency value for using in `geom_label`

frequency_count <- data_to_plot %>% group_by(values) %>% count()%>% arrange(n)
max_frequency <- max(frequency_count$n)

# Plot
ggplot(data_to_plot, aes(x = values)) +
  geom_histogram(aes(y = ..count..), binwidth = 1, colour= "black", fill = "white") +
  geom_density(aes(y=..count..), fill="blue", alpha = .25)+
  
  
  geom_vline(aes(xintercept = value_1),
             color="red", linetype = "dashed", size = 0.5, alpha = 1) +
  
  geom_vline(aes(xintercept = value_1),
             color="forestgreen", linetype="dashed", size = 0.5, alpha = 1) +
  
  
  geom_vline(aes(xintercept = value_3),
             color="purple", linetype = "dashed", size = 0.5, alpha = 1) +
  
  
  geom_label(aes(label = obs_number, y = max_frequency*0.87, x = (max(values) - 2.2), color = 'blue'), size = 3.5, alpha = 1) +
  geom_label(aes(label = value_1, y = max_frequency * 0.83, x = (max(values) - 2.2 ), color = 'forestgreen'), size = 3.5, alpha = 1) +
  geom_label(aes(label = value_2, y = max_frequency * 0.79, x = (max(values) - 2.2) , color = 'purple'), size = 3.5, alpha = 1) +
  geom_label(aes(label = value_3, y = max_frequency * 0.75, x = (max(values) - 2.2) , color = 'red'), size = 3.5, alpha = 1) +
  
  
  scale_color_manual(name="Values", 
                     labels = c("Observations number",
                                "value_1",
                                "value_2",
                                "value_3"
                     ), 
                     
                     values = c( "blue",
                                 "forestgreen",
                                 "purple",
                                 "red")) +
  
  labs(title = "relevant_title", y = "Distribution fors DLT values", x = "DLT for the route: average values per batch") +
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(colour = "darkblue"),
        axis.text.x = element_text(face="plain", color="black", 
                                   size=10, angle=0),
        axis.title.y = element_text(colour = "darkblue"),
        axis.text.y = element_text(face="plain", color="black", 
                                   size=10, angle=0),
        legend.position = c(.90, .80)
  )+
  
  
  labs(title="DLT values", y = "frequency", x = "days")+
  scale_x_continuous(breaks = seq(0, max(data_to_plot$values), 1))

这是期望的结果:

但这不适用于所有数据集。

问题:

如何获得绘图区域的笛卡尔坐标,因此我将替换 geom_label 中的 max_frequencymax(values) 并将标签与图例对齐,因为 legend.position = c(.90, .80)

也欢迎其他替代品。

【问题讨论】:

欢迎来到 SO。您正在寻找 npc 坐标。检查这个问题和艾伦的回答。我想这就是你要找的。您可以使用“标签”geom 进行注释。 ***.com/a/63742203/7941188 谢谢!我不确定我是否可以将 npc 单位与geom_label 一起使用。我在legend.position 中使用 npc 单位。它应该在 0 和 1 之间。但是在geom_label npc 单位不起作用,这就是为什么我计算max_frequency 以了解最高y-axis 并使用`max(values)` 表示x-axis 话虽如此 - 将标签 geom 与 A​​llan 的函数结合起来可能不是最简单的。也许检查cran.r-project.org/web/packages/ggpmisc/readme/README.html - 特别是ggpmisc::geom_label_npc 您可能会发现对 NPC 单位的描述很有帮助:cran.r-project.org/web/packages/ggpmisc/vignettes/… 【参考方案1】:

在“也欢迎替代品”的旗帜下:为什么不对 geom_vline()s 使用文本字形并覆盖实际标签?

为了自己的理解,我对代码进行了一些重新排列,但这里是一个示例:

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.0.3
#> Warning: package 'tidyr' was built under R version 4.0.3
#> Warning: package 'readr' was built under R version 4.0.3
#> Warning: package 'dplyr' was built under R version 4.0.3
values <- c(rep(0,2), rep(2,3), rep(3,3), rep(4,3), 5, rep(6,2), 8, 9, rep(11,2) )
obs_number <- c(rep(18,18))
value_1 <- c(rep(4,18))
value_2 <- c(rep(7,18))
value_3 <- c(rep(3,18))

data_to_plot <- data.frame(values, obs_number, value_1, value_2, value_3)

# Extra dataframe for storing the xintercepts and labels
vals <- data.frame(xintercept = c(18, 4, 7, 3),
                   label = c("Observations number", "value_1", "value_2", "value_3"))


frequency_count <- data_to_plot %>% group_by(values) %>% count()%>% arrange(n)
max_frequency <- max(frequency_count$n)

ggplot(data_to_plot, aes(x = values)) +
  geom_histogram(aes(y = ..count..), 
                 binwidth = 1, colour= "black", fill = "white") +
  geom_density(aes(y=..count..), 
               fill="blue", alpha = .25)+
  geom_vline(aes(xintercept = xintercept, color = label),
             data = vals[2:nrow(vals), ], 
             linetype = "dashed", size = 0.5, alpha = 1,
             # Give different legend glyph for vlines
             key_glyph = draw_key_text) +
  scale_color_manual(
    name= "Values", 
    limits = vals$label,
    values = c("blue", "forestgreen", "purple", "red"),
    # Override the labels and set size to something sensible
    guide = guide_legend(override.aes = list(label = vals$xintercept, 
                                             size = 3.88))
  ) +
  labs(title = "relevant_title", y = "Distribution fors DLT values", 
       x = "DLT for the route: average values per batch") +
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(colour = "darkblue"),
        axis.text.x = element_text(face="plain", color="black", 
                                   size=10, angle=0),
        axis.title.y = element_text(colour = "darkblue"),
        axis.text.y = element_text(face="plain", color="black", 
                                   size=10, angle=0),
        legend.position = c(.90, .80)
  )+
  labs(title="DLT values", y = "frequency", x = "days")+
  scale_x_continuous(breaks = seq(0, max(data_to_plot$values), 1))

由reprex package (v0.3.0) 于 2021-01-08 创建

【讨论】:

谢谢@teunbrand,这是一个非常好的解决方案。一个问题 - 是否有可能不从 vals 绘制所有行?我只需要 3 行,不需要geom_vline 作为观察编号,我只需要标签。 当然不是data = vals,而是子集您需要的位。然后在比例尺上设置限制,这样它就知道要绘制所有标签。我已经用更新的代码编辑了答案。

以上是关于获取绘图区域ggplot2的笛卡尔坐标的主要内容,如果未能解决你的问题,请参考以下文章

Cesium坐标系及转换

个人笔记三维GIS开发-cesium坐标系统

如何通过坐标给定的点来识别可变大小的区域?

使用从笛卡尔空间和世界文件生成的纬度和经度计算多边形区域

Matlab中的3D绘图

点之间的笛卡尔XY轴承.Net