使用data.frame/tibble指定ggplot中的比例×连续中断?
我有下面的情节:使用data.frame/tibble指定ggplot中的比例×连续中断?,r,ggplot2,R,Ggplot2,我有下面的情节: require(tidyverse) set.seed(123) vary_1 <- c( exp(rnorm(250,5,1)), rnorm(250,10,10), exp(rnorm(250,20,1)), exp(rnorm(250,30,1))) vary_2 <- c( rep('A',250), rep('B',250), rep('C',250), rep('D',250)) d
require(tidyverse)
set.seed(123)
vary_1 <- c(
exp(rnorm(250,5,1)),
rnorm(250,10,10),
exp(rnorm(250,20,1)),
exp(rnorm(250,30,1)))
vary_2 <- c(
rep('A',250),
rep('B',250),
rep('C',250),
rep('D',250))
data_frame(vary_1,vary_2) %>%
ggplot(aes(vary_1,color = vary_2,fill = vary_2))+
geom_density()+
facet_wrap(~vary_2,scales = 'free')
我的解决方案是将q
拆分为列表
并使用该列表指定中断,但ggplot
不喜欢这样,并给出了一个错误。如何使用数据_帧
在ggplot
面
中指定断点?还是有更好的办法
breaks_ls <- q %>%
group_by(PANEL) %>%
summarise_at(vars(x), funs(min, max)) %>%
ungroup() %>%
select(PANEL,min,max) %>%
split(.,.$PANEL)
break_values <- map(breaks_ls,function(x){
as.numeric(x[1,])
})
data_frame(vary_1,vary_2) %>%
ggplot(aes(vary_1,color = vary_2,fill = vary_2))+
geom_density()+
facet_wrap(~vary_2,scales = 'free')+
scale_x_continuous(breaks = map(breaks_ls,function(x){
as.numeric(x[1,])
}))
中断\u ls%
分组人(专家组)%>%
在(变量(x)、函数(最小值、最大值))处汇总%>%
解组()%>%
选择(面板、最小值、最大值)%>%
拆分(,.$面板)
打破_值%
ggplot(aes(变化1,颜色=变化2,填充=变化2))+
几何密度()+
面_包裹(~vary_2,刻度=‘free’)+
比例x连续(中断=映射)(中断,函数(x){
as.numeric(x[1,])
}))
给出了这个错误
Error in x[finite & x < range[1]] <- NA_real_ :
(list) object cannot be coerced to type 'double'
x[finite&x
该方法的工作原理如下:
使用split
创建一个图的列表,df\u list
使用list2env(df_list,globalenv())
根据需要修改每个绘图
使用patchwork
package对绘图进行修补
库(拼凑)
df%
地图(~ggplot(,aes(vary_1))+
几何密度()
list2env(df_list,globalenv())
#我使用了其他值,但您可以根据您的用例进行调整
A如果您想为每个面板指定其他x限制,我理解。但是为什么你必须使用从同一个图中得到的相同的中断呢?FWIW,这似乎是一个功能请求。
Error in x[finite & x < range[1]] <- NA_real_ :
(list) object cannot be coerced to type 'double'
library(patchwork)
df <- data.frame(vary_1, vary_2)
df_list <- df %>%
split(.$vary_2) %>%
map(~ggplot(.,aes(vary_1))+
geom_density())
list2env(df_list, globalenv())
# I used other values, but you can adjust to your use case
A <- A + xlim(0, 2000)
B <- B + xlim(-10, 30)
C <- C + xlim(0, 2e9)
D <- D + xlim(0, 1e14)
(A + B) / (C + D)