如何根据数量改变ggplot中每个方面的xlim或比例? library(tidyr) 图书馆(GG2) df

如何根据数量改变ggplot中每个方面的xlim或比例? library(tidyr) 图书馆(GG2) df,r,ggplot2,R,Ggplot2,我们可以根据每个键的分位数对值进行过滤然后绘图 library(tidyr) library(ggplot2) df <- data.frame(a = as.numeric(c(1, 2, 3, 4, 5, 6)), b = as.numeric(c(1, 3, 3, 5, 10, 1000)), c = as.numeric(c(0.07, 0.09, 6, 9, 10, 30))) ggplot(gather(

我们可以根据每个
键的
分位数
值进行
过滤
然后绘图

library(tidyr)
library(ggplot2)

df <- data.frame(a = as.numeric(c(1, 2, 3, 4, 5, 6)), 
                 b = as.numeric(c(1, 3, 3, 5, 10, 1000)),
                 c = as.numeric(c(0.07, 0.09, 6, 9, 10, 30)))

ggplot(gather(na.omit(df)), aes(x = value, y = ..density..))+
    geom_histogram(bins = 5, colour = "black", fill = "white") +
    facet_wrap(~key, scales = 'free_x')+
    scale_x_continuous(breaks = scales::pretty_breaks(5))+
    geom_density(alpha = .2, fill = "#FF6666")
库(tidyverse)
df%>%
聚集%>%
分组依据(键)%>%
过滤器(值>分位数(值,0.01)&值<分位数(值,0.99))%>%
解组%>%
ggplot()+
aes(x=值,y=密度)+
几何图形直方图(箱数=5,颜色=“黑色”,填充=“白色”)+
面_包裹(~key,scales='free_x')+
连续缩放(打断=缩放::漂亮的打断(5))+
几何密度(alpha=.2,fill=“#FF6666”)

您可以在
文件中快速地修剪数据

library(tidyverse)

df %>%
  gather %>%
  group_by(key) %>%
  filter(value > quantile(value, 0.01) & value < quantile(value, 0.99)) %>%
  ungroup %>%
  ggplot() +
  aes(x = value, y = ..density..) +
  geom_histogram(bins = 5, colour = "black", fill = "white") +
  facet_wrap(~key, scales = 'free_x')+
  scale_x_continuous(breaks = scales::pretty_breaks(5)) +
  geom_density(alpha = .2, fill = "#FF6666")
那就用你的旧代码吧。我对它做了一点修改,而是在这里使用base R的
堆栈
,它的作用与
聚集
相同,以避免加载太多额外的包

library(data.table)
df2 <- as.data.frame(sapply(df1, function(x)
  x[which(x %between% quantile(x, c(0.01, 0.99)))]))
df2
#   a  b     c
# 1 2  3  0.09
# 2 3  3  6.00
# 3 4  5  9.00
# 4 5 10 10.00
结果

数据


df1我同意
stack
collect
好,在%
之间有没有替代
%的方法?
library(data.table)
df2 <- as.data.frame(sapply(df1, function(x)
  x[which(x %between% quantile(x, c(0.01, 0.99)))]))
df2
#   a  b     c
# 1 2  3  0.09
# 2 3  3  6.00
# 3 4  5  9.00
# 4 5 10 10.00
library(ggplot2)
ggplot(stack(na.omit(df2)), aes(x=values, y=..density..)) +
  geom_histogram(bins=5, colour="black", fill="white") +
  facet_wrap(~ind, scales='free_x') +
  scale_x_continuous(breaks=scales::pretty_breaks(5)) +
  geom_density(alpha=.2, fill="#FF6666")
df1 <- structure(list(a = c(1, 2, 3, 4, 5, 6), b = c(1, 3, 3, 5, 10, 
1000), c = c(0.07, 0.09, 6, 9, 10, 30)), class = "data.frame", row.names = c(NA, 
-6L))