简体   繁体   中英

R and ggplot 2: How to set quantile limits for axes in ggplot2 R plots?

I am trying to use quantiles to limit the x axis the the first 99 quantiles to get a scatter plot looking something similar to the below (for illustration purposes ONLY - teh plot on teh right was created using the fixed limits 0,500 on xlim):

ggplot(aes(x=volume,y=log10(price)),data= diamonds)+
  geom_point()+
  coord_cartesian(xlim = c(0,500))

在此处输入图片说明

ggplot(aes(x=volume,y=log10(price)),data= diamonds)+
  geom_point()+
  coord_cartesian(xlim = c(quantile(diamonds$volume<diamonds$volume, 0.99)))

which produces this:

散点图1。

and

ggplot(aes(x=volume,y=log10(price)),data= diamonds)+
  geom_point()+
  coord_cartesian(xlim = c(quantile(diamonds$volume, 0.99)))

which produces this:

在此处输入图片说明

I want to avoid tampering with the actual data or creating a new dataset. Can anyone point to where I am going wrong?

scale_x_continuous(limits = c(quantile(diamonds$volume, 0.99)))

produces very similar results, so I suspect that the issue is how I am defining the quantiles.

the problem is that xlim expects a vector with two elements: min and max.

max is defined by quantile(diamonds$x, 0.99) while min can be min(diamonds$x)

library(ggplot2)
data(diamonds)

ggplot(aes(x=x,y=log10(price)),data= diamonds)+
  geom_point()+
  coord_cartesian(xlim = c(min(diamonds$x), quantile(diamonds$x, 0.99)))

在此处输入图片说明

I used x instead of volume since the diamonds data set in my version of ggplot2 (2.2.1) does not contain the column volume .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM