2016-01-18 76 views
2

我過濾了基於價格變化的鑽石數據框架以獲得低於或等於10000的價格,並且我將新的數據框命名爲df。調整ggplot中垂直線標籤的位置?

然後,我添加了一個新的列分位數,它具有價格列的分位數。最高價格位於第一分位數(前20%),最低價格位於第五分位數。

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去除鱗=「free_y」,並在geom_text

enter image description here 改變y以3在前面的圖中,它工作得很好,因爲在鑽石切割水平之間,y值變化不大。但是,如果我有一個具有完全不同的y值的數據框架,所以我不能修復geom_text中的y值。

當facet_wrap中有不同的y值而不刪除scales =「free_y」時,是否有任何方法可以對齊垂直線的標籤?

回答

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") 
+0

非常感謝您的時間和幫助。 – aelwan