我有一个看起来像这样谨慎的数据:如何使用sec_axis()来处理ggplot2 R中的离散数据?
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上的谨慎数据? 编辑:有人在评论建议我尝试建议的答复。但是,我碰到这个错误现在
这里是我当前的代码:
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?"
我得到的模板,但没有得到积分
这些是连续的数据(数字),而不是离散的(类别)。 – Brian
我意识到我链接了不正确的来源。如果我使用了错误,我已经链接了正确的答案并更新了我的答案。 – Ash
在'sec_axis(...)'之前添加'sec.axis ='。没有明确指定参数,它默认为'scale_y_continuous()'中的第二个参数,&breaks = sec_axis(〜。* 1.2,name =“height”)'触发该错误,因为它在上下文中没有意义。 –