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

Get cartesian coordinates for plot area ggplot2

我想将标签放置在靠近图例的位置。

在下面的代码中,我在 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).

也欢迎其他选择。

在 'alternatives are also welcome' 的旗帜下:为什么不对 geom_vline() 使用文本字形并覆盖实际标签?

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

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 年 1 月 8 日创建