【问题标题】:How to use sec_axis() for discrete data in ggplot2 R?如何将 sec_axis() 用于 ggplot2 R 中的离散数据?
【发布时间】:2018-02-13 04:13:44
【问题描述】:

我有如下所示的谨慎数据:

height <- c(1,2,3,4,5,6,7,8)
weight <- c(100,200,300,400,500,600,700,800)
person <- c("Jack","Jim","Jill","Tess","Jack","Jim","Jill","Tess")
set <- c(1,1,1,1,2,2,2,2)
dat <- data.frame(set,person,height,weight)

我正在尝试绘制具有相同 x 轴(人)和 2 个不同 y 轴(体重和身高)的图表。我发现所有示例都在尝试绘制secondary axis (sec_axis),或使用基图绘制离散数据。 是否有一种简单的方法可以将 sec_axis 用于 ggplot2 上的离散数据? 编辑: cmets 中有人建议我尝试建议的回复。但是,我现在遇到了这个错误

这是我当前的代码:

p1 <- ggplot(data = dat, aes(x = person, y = weight)) + 
  geom_point(color = "red") + facet_wrap(~set, scales="free") 
p2 <- p1 + scale_y_continuous("height",sec_axis(~.*1.2, name="height"))
p2

I get the error: Error in x < range[1] : 
  comparison (3) is possible only for atomic and list types

另外,现在我修改了示例以匹配this example posted.

p <- ggplot(dat, aes(x = person))
p <- p + geom_line(aes(y = height, colour = "Height"))

# adding the relative weight data, transformed to match roughly the range of the height
p <- p + geom_line(aes(y = weight/100, colour = "Weight"))

# now adding the secondary axis, following the example in the help file ?scale_y_continuous
# and, very important, reverting the above transformation
p <- p + scale_y_continuous(sec.axis = sec_axis(~.*100, name = "Relative weight [%]"))

# modifying colours and theme options
p <- p + scale_colour_manual(values = c("blue", "red"))
p <- p + labs(y = "Height [inches]",
              x = "Person",
              colour = "Parameter")
p <- p + theme(legend.position = c(0.8, 0.9))+ facet_wrap(~set, scales="free") 
p

我收到一条错误提示

"geom_path: Each group consists of only one observation. Do you need to 
 adjust the group aesthetic?"

我得到了模板,但没有绘制任何点

【问题讨论】:

  • 那些是连续的数据(数字),而不是离散的(类别)。
  • 我意识到我链接了错误的来源。如果我使用它,我已经链接了正确的并用错误更新了我的答案
  • 在您的sec_axis(...) 前面添加sec.axis = 。在没有明确命名参数的情况下,它默认为scale_y_continuous() 中的第二个参数,&breaks = sec_axis(~.*1.2, name="height") 会触发该错误,因为它在上下文中没有意义。
  • 喜欢这个p1 + scale_y_continuous("height", sec_axis(sec.axis= ~.*1.2, name="height")) ?这给我一个错误
  • 据我所知ggplot2不支持独立副轴。所有辅助轴都必须基于主轴的一对一变换。

标签: r ggplot2 axes multiple-axes


【解决方案1】:

如果没有明确指定参数名称,R 函数参数将按位置输入。正如 @Z.Lin 在 cmets 中提到的,您需要在 sec_axis 函数之前使用 sec.axis= 以表明您正在将此函数输入到 scale_y_continuous 的 sec.axis 参数中。如果你不这样做,它将被输入scale_y_continuous 的第二个参数,默认情况下是breaks=。因此,错误消息与您没有为 breaks 参数提供可接受的数据类型有关:

p1 <- ggplot(data = dat, aes(x = person, y = weight)) + 
  geom_point(color = "red") + facet_wrap(~set, scales="free") 
p2 <- p1 + scale_y_continuous("weight", sec.axis = sec_axis(~.*1.2, name="height"))
p2

scale_y_continuous 的第一个参数 (name=) 用于 第一个 y 刻度,而sec.axis= 参数用于第二 y 刻度。我更改了您的第一个 y 比例名称以纠正该问题。

【讨论】:

  • 但这似乎没有绘制体重和身高。我需要将体重和身高都绘制在同一个 x 轴上。
  • @Ash 只是标签没有正确指定。请参阅我编辑的答案。
  • 没有。我不认为你明白我的意思。对于每个人,例如。在 Set1 中,对于 Jack,我应该看到两个点,一个对应于他的身高(比如标记为红色),另一个对应于他的体重(比如标记为黑色)。相反,这里的情节产生了一个点,也许是体重和身高之间的关系?这不是我想要的。
  • @Ash 我明白了,您实际上想要在同一个图上使用两个不同的 y 轴。在这种情况下,sec.axis 不是正确的解决方案,因为它只允许第二个轴是第一个轴的线性变换。我认为ggplot2 没有好的解决方案,因为它的设计目的是阻止数据的虚假陈述。请参阅此讨论:stackoverflow.com/questions/3099219/…
猜你喜欢
  • 2021-02-06
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2021-11-29
  • 2017-03-01
相关资源
最近更新 更多