shiny
shiny

Reputation: 3502

Adjust the position of labels of vertical lines in ggplot?

I filtered diamond data frame based on the price varible to get prices lower than or equal 10000 and I named the new dataframe df.

Then, I added a new column quantile that have the quantiles of price column. The highest price is in the 1st quantile (top 20%) and the lowest price is in the 5th quantile.

Q1 defines the values to be used in plotting vertical lines between different quantiles.

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

The labels of the vertical lines are not aligned together becuase of the scales in the facet_wrap. In addition, the labels are overlapping with the points as shown below enter image description here

I fixed that by removing scales="free_y" in the facet_wrap and changed y to 3 in geom_text

enter image description here In the previous plot, it worked fine because the y values don't vary that much between the levels of diamond cuts.

However, if I have a data frame that have completely different y values, so I can't fix the y value in geom_text.

Is there any way to align the labels of vertical lines when I have different y values in facet_wrap without deleting scales="free_y"?

Upvotes: 3

Views: 2498

Answers (1)

Mist
Mist

Reputation: 1948

How's this?

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

Upvotes: 3

Related Questions