我想用自定义标签为断点显示离散比例,并用自定义标签包含限制.我知道当显示限制内的中断时,代码会起作用,但当限制处于活动状态时,标记不再适用于这种方法

以下是有效的方法:

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = c('2', '3', '4', '5'),
                    show.limits = F) # proof that this method works for breaks WITHIN limits

当此绘图的限制被激活时,ggplot会给出错误消息,并且不会呈现绘图

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = c('2', '3', '4', '5'),
                    show.limits = T) # method fails when limits are activated

f()中的错误:

断裂和标签的长度显然相同.也许关于激活限制的某些东西改变了标签向量的形成方式?我知道在哪里可以找到ggplot2中断向量的代码(使用ggplot_build()或直接单击保存的绘图对象).我仔细查看了这些,没有发现任何东西能帮助我理解如何解决这个问题.

我也try 了这些方法,但它返回了相同的错误'breaks and labels are different lengths':

# this did not work

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = c('1', '2', '3', '4', '5', '6')
                    show.limits = T) # added more entries to label vector did not work

# this also did not work

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_x_continuous(
    breaks = c(0, 20, 40),
    labels = c("0", "20", "40"))  + 
  scale_color_binned(breaks = c(1999, 2000, 2002, 2004, 2006, 2008),
                     labels = c('1', '2', '3', '4', '5', '6'),
                     show.limits = F) # add limits directly to breaks did not work

下面列出了一种可以用来标记每个条目的方法.然而,如果离散标度有多个条目,则该方法非常繁琐.

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c('1' = 1999, '6' = 2008),
                    breaks = c('2' = 2000, '3' = 2002, '4' = 2004, '5' = 2006),
                    show.limits = T) # this works, but is a tedious solution when writing several breaks

我想使用scale_color_bined方法(或类似函数),因为我想将连续的zoom 数据转换为15-20个离散范围,然后为不同范围自定义标签或 suppress 标签.

有没有一种方法可以生成一个ggplot,它可以接受装箱连续值的自定义标签,同时显示数据的指定限制?

推荐答案

如果你使用一个函数来标记断点,你就会知道发生了什么:

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = ~ print(.x),
                    show.limits = TRUE)
#> [1] 2000 2002 2004 2006
#> [1] 1999 2008

您可以看到,中断和限制的标签分两批发送到标签函数,因此固定长度的标签向量对于其中一批或另一批总是会失败.实际上,您需要一个能够处理以下任一情况的函数:

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = ~ if(length(.x) == 2) .x else 1:4,
                    show.limits = TRUE)

enter image description here

R相关问答推荐

如何使用TukeyHSD绘制事后概率热图

是否有R代码来判断一个组中的所有值是否与另一个组中的所有值相同?

使用na.locf在长格式数据集中输入具有多个时间点的数据集

如果窗口在CLARME或集团之外,则有条件领先/滞后滚动总和返回NA

在边界外添加注释或标题

更新合适的R mgcv::bam模型报告无效类型(关闭).'';错误

从开始时间和结束时间导出时间

使用case_match()和char数组重新编码值

为什么舍入POSIXct会更改能力以匹配等效的POSIXct?

在另存为PNG之前隐藏htmlwidget绘图元素

在嵌套列表中查找元素路径的最佳方法

R中的哈密顿滤波

如何根据R中其他变量的类别汇总值?

扩展R中包含列表的数据框

使用geom_iles在一个切片中包含多个值

使用函数从R中的列中删除标高

如何在访问之前下载的输入时同时上传和处理所有指定的shiny 输入?

通过比较来自多个数据框的值和R中的条件来添加新列

图中显示错误 colored颜色 的图例geom_sf

在一个multiplot中以非对称的方式在R中绘制多个图