带晶格的R中的点图:显示垂直轴和误差条

带晶格的R中的点图:显示垂直轴和误差条,r,data-visualization,lattice,R,Data Visualization,Lattice,我正在尝试对R中的库lattice和latticeeextra进行点绘图。但是,没有正确表示垂直y轴上的值。而不是选择数值变量的实际值,R绘制值的秩。也就是说,有值[375,500,625,750,…,3000]和R绘制它们的等级[1,2,3,4。有人遇到过这样的问题吗?我如何在垂直y刻度上使用诸如(0,500,1000,1500,…)之类的记号来管理图像以获得正确的表示 以下是迄今为止的程序代码: df.dose <- read.table("data.csv", sep=",", he

我正在尝试对
R
中的库
lattice
latticeeextra
进行点绘图。但是,没有正确表示垂直y轴上的值。而不是选择数值变量的实际值,
R
绘制值的秩。也就是说,有值
[375,500,625,750,…,3000]
R
绘制它们的等级
[1,2,3,4。有人遇到过这样的问题吗?我如何在垂直y刻度上使用诸如
(0,500,1000,1500,…)
之类的记号来管理图像以获得正确的表示

以下是迄今为止的程序代码:

df.dose <- read.table("data.csv", sep=",", header=TRUE)
library(lattice); library(latticeExtra)

useOuterStrips(dotplot(z ~ sample.size | as.factor(effect.size)*as.factor(true.dose),
               groups=as.factor(type), data=df.dose, as.table=TRUE))

您需要使
z
成为一个因子:
dotplot(因子(z)~…

另外,您可能需要在绘图中添加一些抖动以防止重叠;请尝试添加
jitter.x=TRUE
jitter.y=TRUE
,或两者兼而有之

根据您在下面的评论并再次查看数据,我认为您绘制点图的方式是错误的。我认为您希望线条用于样本大小,而不是用于
z
。如果您确实希望
z
位于垂直轴上,则需要添加
horizontal=TRUE
。您还可以交换hori上的内容水平轴和垂直轴

useOuterStrips(dotplot(z ~ factor(sample.size) | 
                             as.factor(effect.size)*as.factor(true.dose),
                  groups=as.factor(type), data=df.dose,  
                  as.table=TRUE, horizontal=FALSE, jitter.x=TRUE))
要添加一个错误条,它有点复杂,因为面板中有组,所以需要使用
panel.groups
函数;此外,为了避免行重叠,您可能需要将它们左右抖动一点,这最好在自定义
panel
函数中完成

df.dose$se <- 200
df.dose$type <- factor(df.dose$type)
df.dose$sample.size <- factor(df.dose$sample.size)

panel.groups.mydotplot <- function(x, y, subscripts, up, lo, 
                                   col=NA, col.line=NA, ...) {
  panel.points(x, y, ...)
  panel.segments(x, lo[subscripts], x, up[subscripts], col=col.line, ...)
}
panel.mydotplot <- function(x, y, subscripts, groups, ..., jitter=0.1) {
  jitter <- seq(-1,1,len=nlevels(groups))*jitter
  xx <- as.numeric(x) + jitter[as.numeric(groups[subscripts])]
  panel.dotplot(x, y, groups=groups, subscripts=subscripts, pch=NA, ...)
  panel.superpose(xx, y, groups=groups, subscripts=subscripts,  
                  panel.groups=panel.groups.mydotplot, ...)
}
pp <- dotplot(z ~ sample.size | as.factor(effect.size)*as.factor(true.dose),
              groups=type, data=df.dose, as.table=TRUE, horizontal=FALSE,
              up=df.dose$z + df.dose$se, lo=df.dose$z - df.dose$se,
              panel=panel.mydotplot, auto.key=list(space="right"))
useOuterStrips(pp)

df.dose$se我不确定我是否理解这个问题,您要求使用晶格解决方案,但我认为使用ggplot2完成此操作可能会有所帮助:

ggplot(data=df.dose, aes(x=sample.size, y=as.factor(z), colour=type)) +
    geom_point() + facet_grid(true.dose~effect.size)
收益率:

或者我们可以通过以下方式释放天平:

ggplot(data=df.dose, aes(x=sample.size, y=as.factor(z), colour=type)) +
    geom_point() + facet_grid(true.dose~effect.size, scales="free")
屈服:


您也可以从中使用
xYplot
,以获得类似于@Aaron的解决方案,尽管获得与他相同的抖动可能有点棘手:

a <- xYplot(Cbind(z, z-se, z+se) ~ sample.size | as.factor(effect.size) * as.factor(true.dose),
            groups=as.factor(type), data=df.dose, as.table=TRUE, auto.key=list(space="top"))
useOuterStrips(a)

但是那里隐藏着一些东西,由于数据的密度,仍然有太多的噪音。让我们去掉
效应。大小
并绘制回归线,尽管使用如此少的数据点可能是一种罪恶

a <- xyplot(z ~ as.factor(sample.size) | as.factor(type)*as.factor(true.dose), 
            data=df.dose, as.table=TRUE, 
            panel = function(x, y){
               panel.xyplot(x, y, jitter.x = T, col=1);
               panel.lmline(x, y, col=1, lwd=1.5);
            })
useOuterStrips(a)

很好,泰勒。你如何用ggplot添加抖动来显示重叠点?(或者在ggplot中更喜欢其他方法?@Aaron你可以使用
position=position\u jitter(width=.005)
geom\u point
的内部,或者用
alpha
透明度(类似的东西)要点之一。我认为可能有更好的方法来设置一起绘制。也许可以使用不同的刻面(按类型)来代替,或者创建一个合并z和类型的新变量。亲爱的Tyler,感谢ggplot的提示。它看起来非常好。我还没有想到在ggplot中这样做的方法。总之,通过添加“position=position\u jitter(width=5,height=0)”放在geom\u point()的括号中,使用“ggplot2”,可以将错误条添加到图形中吗?我想到了“geom\u段(aes(x=sample.size,xend=sample.size,y=z-100,yend=z+100)),但这似乎不适用于镶嵌面网格。是否可能?对于错误条,您将使用
geom_errorbar
。我不确定第二次请求时您想要的是什么。也许您想要
geom_hline
亲爱的Aaron,非常感谢您的回复!确实,将变量设为“z”一个因素是简单的解决方案!因此谢谢你。但是,当我尝试自定义刻度时,“.R”根本没有显示刻度:刻度=列表(y=列表(勾号=6,relation=“same”,at=c(05001000150020002500)),x=列表(勾号=3,relation=“same”,at=c(40,60,80))是的,现在看起来应该是这样。非常感谢,这真的很有帮助!使用“晶格”,可以将错误条添加到图形中吗?我想到了“up=z+se,lo=z-se,panel.groups=function(x,y,…,up,lo,subscripts){up对于其他参数,它不会查看
数据内部
,因此您必须以
up=df.dose$z+df.dose$se
的形式输入它们。此外,只有在切换轴的情况下,您的段调用才会正确;如果您使用
horizontal=FALSE
,则需要适当地切换参数。实际上,这要稍微多一些很复杂,因为面板中有组。请参阅编辑。感谢您提供Hmisc软件包的提示。我知道,有很多方法可以实现此目标。非常感谢!
a <- xYplot(Cbind(z, z-se, z+se) ~ sample.size | as.factor(effect.size) * as.factor(true.dose),
            groups=as.factor(type), data=df.dose, as.table=TRUE, auto.key=list(space="top"))
useOuterStrips(a)
key.variety <- list(space = "top", 
                    text = list(levels(df.dose$type)),
                    points = list(pch = 0:3, col = "black"))
a <- xyplot(z ~ as.factor(sample.size) | as.factor(effect.size)*as.factor(true.dose),
            df.dose, type = "o", as.table=TRUE, groups = type, key = key.variety, 
            lty = 1, pch = 0:3, col.line = "darkgrey", col.symbol = "black")
useOuterStrips(a)
a <- xyplot(z ~ as.factor(sample.size) | as.factor(type)*as.factor(true.dose), 
            data=df.dose, as.table=TRUE, 
            panel = function(x, y){
               panel.xyplot(x, y, jitter.x = T, col=1);
               panel.lmline(x, y, col=1, lwd=1.5);
            })
useOuterStrips(a)