问题描述
我想将标签放置在靠近图例的位置。
在下面的代码中,我在 (x,y)
中硬编码了 geom_label
值以获得当前数据帧的所需结果:
# Creating dataframe
library(ggplot2)
values <- c(rep(0,2),rep(2,3),rep(3,rep(4,5,rep(6,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",alpha = 1) +
geom_vline(aes(xintercept = value_3),color="purple",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'),alpha = 1) +
geom_label(aes(label = value_2,y = max_frequency * 0.79,color = 'purple'),alpha = 1) +
geom_label(aes(label = value_3,y = max_frequency * 0.75,color = 'red'),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",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))
问题:
如何获得绘图区域的笛卡尔坐标,因此我将替换 max_frequency
中的 max(values)
和 geom_label
,并将标签与图例对齐,假设为 legend.position = c(.90,.80)
。
也欢迎其他替代方案。
解决方法
在“也欢迎替代品”的旗帜下:为什么不为 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,rep(4,5,rep(6,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,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",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 日创建