调整ggplot中垂直线标签的位置?

时间:2016-01-18 01:13:36

标签: r ggplot2 geom-text

我根据价格变量过滤了钻石数据框,以获得低于或等于10000的价格,并将新数据框命名为df。

然后,我添加了一个具有price列分位数的新列分位数。最高价格是第1个分位数(前20%),最低价格是第5个分位数。

Q1定义用于绘制不同分位数之间的垂直线的值。

library(ggplot2)
library(dplyr)
df <- diamonds %>% filter(price <= 10000)
df <- within(df, quantile <- 6 - as.integer(cut(price, quantile(price, probs=0:5/5), include.lowest=TRUE)))
df$quantile<-factor(df$quantile, levels=c("1", "2", "3", "4", "5","6", "7", "8", "9","10"))
Q1 <- quantile(df$price, 1:4/5)


ggplot(df, aes(x=price, y= carat, color=quantile))+
       geom_point(alpha=0.4, size=1)+ 
       geom_vline(xintercept=Q1, alpha=0.5, linetype="longdash")+ 
       geom_text(aes(x=5000, y=2,
                 label="80th %ile"), hjust=1, vjust= 1, angle =90, colour="blue") +
       geom_text(aes(x=2850, y=2,
                 label="60th %ile"),
                 hjust=1, vjust= 1, angle =90, colour="blue")+
       geom_text(aes(x=820, y=2,
                 label="20th %ile"),
                 hjust=1, vjust= 1, angle =90, colour="blue")+
       facet_wrap(~cut, ncol=2, scales="free_y")+
       theme_bw()+
       labs(x="Price ($)", y="Carat")

由于facet_wrap中的刻度,垂直线的标签未对齐在一起。此外,标签与点重叠,如下所示 enter image description here

我通过删除facet_wrap中的scales =“free_y”并在geom_text中将y更改为3来解决此问题

enter image description here 在之前的图中,它工作正常,因为在钻石切割水平之间y值的变化不大。

但是,如果我的数据框具有完全不同的y值,那么我无法在geom_text中修复y值。

当facet_wrap中的y值不同而没有删除scale =“free_y”时,有没有办法对齐垂直线的标签?

1 个答案:

答案 0 :(得分:2)

这是怎么回事?

library(ggplot2)
library(dplyr)
df <- diamonds %>% filter(price <= 10000)
df <- within(df, quantile <- 6 - as.integer(cut(price, quantile(price, probs=0:5/5), include.lowest=TRUE)))
df$quantile<-factor(df$quantile, levels=c("1", "2", "3", "4", "5","6", "7", "8", "9","10"))
Q1 <- quantile(df$price, 1:4/5)

lbl <- data.frame(cut = c("Ideal", "Premium", "Very Good", "Good", "Fair"),
                  y_offset = c(max(df$carat[df$cut == "Ideal"]) * 0.6,
                               max(df$carat[df$cut == "Premium"]) * 0.6,
                               max(df$carat[df$cut == "Very Good"]) * 0.6,
                               max(df$carat[df$cut == "Good"]) * 0.6,
                               max(df$carat[df$cut == "Fair"]) * 0.6))

ggplot()+
  geom_point(data = df, aes(x=price, y= carat, color=quantile), alpha=0.4, size=1)+ 
  geom_vline(data = df, xintercept=Q1, alpha=0.5, linetype="longdash")+ 
  geom_text(data = lbl, aes(x=5000, y=y_offset,
                            label="80th %ile"), hjust=1, vjust= 1, angle =90, colour="blue") +
  geom_text(data = lbl, aes(x=2850, y=y_offset,
                            label="60th %ile"),
            hjust=1, vjust= 1, angle =90, colour="blue")+
  geom_text(data = lbl, aes(x=820, y=y_offset,
                            label="20th %ile"),
            hjust=1, vjust= 1, angle =90, colour="blue")+
  facet_wrap(~cut, ncol=2, scales="free_y")+
  theme_bw()+
  labs(x="Price ($)", y="Carat")