如何在ggplot2 R中将sec_axis()用于离散数据? [英] How to use sec_axis() for discrete data in ggplot2 R?

查看:433
本文介绍了如何在ggplot2 R中将sec_axis()用于离散数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have discreet data that looks like this:

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轴(体重和身高)的图表.我发现所有示例都试图绘制次级轴(sec_axis),或使用基本图绘制离散数据. 有没有一种简单的方法可以将sec_axis用于ggplot2上的离散数据? 有人在建议中建议我尝试建议的答复.但是,我现在遇到了这个错误

I'm trying to plot a graph with same x-axis(person), and 2 different y-axis (weight and height). All the examples, I find is trying to plot the secondary axis (sec_axis), or discreet data using base plots. Is there an easy way to use sec_axis for discreet data on ggplot2? Someone in the comments suggested I try the suggested reply. However, I run into this error now

这是我当前的代码:

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

或者,现在我已修改示例以匹配此示例已发布.

Alternately, now I have modified the example to match 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?"

我得到了模板,但是没有点被画出

I get the template, but no points get plotted

推荐答案

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

R function arguments are fed in by position if argument names are not specified explicitly. As mentioned by @Z.Lin in the comments, you need sec.axis= before your sec_axis function to indicate that you are feeding this function into the sec.axis argument of scale_y_continuous. If you don't do that, it will be fed into the second argument of scale_y_continuous, which by default, is breaks=. The error message is thus related to you not feeding in an acceptable data type for the breaks argument:

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=)用于 first y标度,而sec.axis=参数用于 second y标度.我更改了您的第一个y比例名称以进行更正.

The first argument (name=) of scale_y_continuous is for the first y scale, where as the sec.axis= argument is for the second y scale. I changed your first y scale name to correct that.

这篇关于如何在ggplot2 R中将sec_axis()用于离散数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆