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

3

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

然后,我添加了一个新列quantile,其中包含价格列的分位数。最高价格在第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"并将y更改为3来解决了这个问题。 enter image description here 在上一个图中,它能正常工作,因为钻石切割等级之间的y值变化不大。
然而,如果我有一个具有完全不同y值的数据框,则无法在geom_text中固定y值。
在facet_wrap中,如果我有不同y值的数据,有没有办法对齐垂直线的标签而不删除scales="free_y"?
1个回答

3

这个怎么样?

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接