使用钻石数据集...
尝试创建一个允许我在x轴上绘制切割或颜色的函数...
...但是首先,我想过滤选定的列以仅显示一定数量的级别。
我已经使过滤器开始工作,但是水平仍然存在...它们将显示在图表中。我需要在所选列上执行fct_drop()
有关可重现的示例,请参见以下代码:
*** buffer overflow detected ***: /home/router/tomcat/webapps/ROOT//WEB-INF/bin/pathload_rcv terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f79239837e5]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f7923a2515c]
/lib/x86_64-linux-gnu/libc.so.6(+0x117160)[0x7f7923a23160]
/lib/x86_64-linux-gnu/libc.so.6(+0x1164b2)[0x7f7923a224b2]
/home/router/tomcat/webapps/ROOT//WEB-INF/bin/pathload_rcv[0x402131]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f792392c830]
/home/router/tomcat/webapps/ROOT//WEB-INF/bin/pathload_rcv[0x402539]
======= Memory map: ========
00400000-00409000 r-xp 00000000 b3:02 268840 /home/router/tomcat/webapps/ROOT/WEB-INF/bin/pathload_rcv
00608000-00609000 r--p 00008000 b3:02 268840 /home/router/tomcat/webapps/ROOT/WEB-INF/bin/pathload_rcv
00609000-0060a000 rw-p 00009000 b3:02 268840 /home/router/tomcat/webapps/ROOT/WEB-INF/bin/pathload_rcv
01946000-01967000 rw-p 00000000 00:00 0 [heap]
7f79236f6000-7f792370c000 r-xp 00000000 b3:02 523918 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f792370c000-7f792390b000 ---p 00016000 b3:02 523918 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f792390b000-7f792390c000 rw-p 00015000 b3:02 523918 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f792390c000-7f7923acc000 r-xp 00000000 b3:02 524505 /lib/x86_64-linux-gnu/libc-2.23.so
7f7923acc000-7f7923ccc000 ---p 001c0000 b3:02 524505 /lib/x86_64-linux-gnu/libc-2.23.so
7f7923ccc000-7f7923cd0000 r--p 001c0000 b3:02 524505 /lib/x86_64-linux-gnu/libc-2.23.so
7f7923cd0000-7f7923cd2000 rw-p 001c4000 b3:02 524505 /lib/x86_64-linux-gnu/libc-2.23.so
7f7923cd2000-7f7923cd6000 rw-p 00000000 00:00 0
7f7923cd6000-7f7923cee000 r-xp 00000000 b3:02 524487 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f7923cee000-7f7923eed000 ---p 00018000 b3:02 524487 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f7923eed000-7f7923eee000 r--p 00017000 b3:02 524487 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f7923eee000-7f7923eef000 rw-p 00018000 b3:02 524487 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f7923eef000-7f7923ef3000 rw-p 00000000 00:00 0
7f7923ef3000-7f7923ffb000 r-xp 00000000 b3:02 524510 /lib/x86_64-linux-gnu/libm-2.23.so
7f7923ffb000-7f79241fa000 ---p 00108000 b3:02 524510 /lib/x86_64-linux-gnu/libm-2.23.so
7f79241fa000-7f79241fb000 r--p 00107000 b3:02 524510 /lib/x86_64-linux-gnu/libm-2.23.so
7f79241fb000-7f79241fc000 rw-p 00108000 b3:02 524510 /lib/x86_64-linux-gnu/libm-2.23.so
7f79241fc000-7f7924222000 r-xp 00000000 b3:02 524486 /lib/x86_64-linux-gnu/ld-2.23.so
7f7924416000-7f792441a000 rw-p 00000000 00:00 0
7f7924420000-7f7924421000 rw-p 00000000 00:00 0
7f7924421000-7f7924422000 r--p 00025000 b3:02 524486 /lib/x86_64-linux-gnu/ld-2.23.so
7f7924422000-7f7924423000 rw-p 00026000 b3:02 524486 /lib/x86_64-linux-gnu/ld-2.23.so
7f7924423000-7f7924424000 rw-p 00000000 00:00 0
7ffdd0add000-7ffdd0afe000 rw-p 00000000 00:00 0 [stack]
7ffdd0be8000-7ffdd0beb000 r--p 00000000 00:00 0 [vvar]
7ffdd0beb000-7ffdd0bed000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
答案 0 :(得分:1)
您快到了!您的代码中的问题是R在!
的LHS上不允许=
。因此,您需要使用伪造的:=
运算符。
reduce_for_plot <- function(data, column, how_many_levels) {
col_expr <- enquo(column)
col_name <- rlang::as_name(col_expr)
of_interest <- unique(data[[col_name]])[1:how_many_levels]
data <- data %>%
filter(!!col_expr %in% of_interest)
value_to_put_in <- fct_drop(data[[col_name]][of_interest])
data %>%
mutate(!!col_name := value_to_put_in)
}
如您所见,我已将所有deparse(substitute(column))
替换为as_name(enquo(column))
。但是,您可以通过在数据上下文中进行计算来完全避免这些情况,我认为这会产生更好的代码:
reduce_for_plot <- function(data, column, how_many_levels) {
column <- enquo(column)
data %>%
filter(!!column %in% unique(!!column)[1:how_many_levels]) %>%
mutate(!!column := fct_drop(!!column))
}