是否可以像在 Excel 中一样创建具有不同 Y 轴的组合柱形图和折线图?我想自动生成图表,其中折线图是好坏比率,条形图是另一个变量给定间隔的全域百分比。左边的 Y 轴代表好坏比率,右边的 Y 轴代表宇宙的百分比。我想用 lattice 来做这件事,但任何建议都将不胜感激。
问问题
5103 次
3 回答
4
require(plotrix) # followed by a slight variation of the first example of twoord.plot
twoord.plot(2:10,seq(3,7,by=0.5)+rnorm(9), type=c("bar", "l"),
1:15,rev(60:74)+rnorm(15),xlab="Sequence",
ylab="Ascending values",rylab="Descending values",
main="Plot with two ordinates - points and lines")
评论格式似乎有问题,所以我也将代码放在这里。rx 和 lx 向量必须是数字,但 xticklab 参数可用于正确标记:
twoord.plot(lx=1:10, ly= df$Pct, rx=1:10, ry= df$Rate, type=c('bar','l'),
xticklab=df$Segment, xlab='Segment', ylab='Percent of Good', rylab='Good Rate')
于 2011-04-25T01:59:50.130 回答
1
我知道这个问题已经有一个公认的答案,但我只是想添加另一个选项。如果您发现自己处于“基本” R 之外的选择有限的情况(例如,如果您在一个不同的团队中工作并且需要尽可能高的代码兼容性,这是我最近发现自己的情况),您总是可以使用
par(new=TRUE)
命令在另一个上面绘制一个图 - 所以,一个条形图和一个线图。这里的技巧(感谢这篇文章)是对折线图和条形图使用“plot”,只需使用“type='h'”和“lend”和“lwd”选项从折线图创建条形图.
这复制了上面的代码,仅使用“基本”函数:
## Set up data
line.x <- 2:10
bar.x <- 2:10
bar.y <- seq(3,7,by=0.5)+rnorm(9)
bar.x <- 1:15
bar.x <- 2:10
line.x <- 1:15
line.y <- rev(60:74)+rnorm(15)
x.range <- range(bar.x, line.x)
## Plot the data
par(mar=c(5,4,4,4)+0.1) ## Make enough room for both labels
plot(y=bar.y, x=bar.x, type='h', lwd=25, lend=4, xlim=x.range, xlab="Sequence", ylab="", main="Plot with two ordinates - points and lines")
par(new=TRUE)
plot(y=line.y, x=line.x, col='red', type='l', xlim=x.range, axes=FALSE, xlab='', ylab='')
## Set up the axes and labels
axis(side=4, col='red', labels=FALSE)
at = axTicks(4)
mtext(side = 4, text = at, at = at, col = "red", line = 1)
## Add x-axis labels; this allows customization of the how far out labels are
mtext(text='Descending values', side=4, line=2, col='red')
mtext(text='Ascending values', side=2, line=2)
于 2012-02-23T20:35:47.073 回答
0
如果您想手动执行此操作,请查看 TeachingDemos 包中的 updateusr 函数。
这仅适用于基本图形,不适用于晶格。
于 2011-04-25T15:21:04.193 回答