R 如何重现置信区间图?

R 如何重现置信区间图?,r,graphics,confidence-interval,R,Graphics,Confidence Interval,我需要复制一个类似于所附图像的图形。我希望用这张图来比较比例差异的置信区间。如何使用R生成所附图形?任何指向正确方向的指示都将不胜感激 没有上下文和可重复的例子,很难给出好的答案。但我觉得情节很有趣 这是我对ggplot2的尝试。我仍然有一些阿尔法层的问题,但主要的情节是在这里 一些数据 structure(list(scen = 1:6, name = c("I", "II", "III", "IV", "V","VI"), ym

我需要复制一个类似于所附图像的图形。我希望用这张图来比较比例差异的置信区间。如何使用R生成所附图形?任何指向正确方向的指示都将不胜感激


没有上下文和可重复的例子,很难给出好的答案。但我觉得情节很有趣

这是我对ggplot2的尝试。我仍然有一些阿尔法层的问题,但主要的情节是在这里

一些数据

structure(list(scen = 1:6, 
               name = c("I", "II", "III", "IV", "V","VI"), 
               ymin = c(0.06, -0.102, 0.487, 0.116, -0.436, 0.021), 
               ymax = c(-0.231,0.135, 0.117, 0.338, -0.347, -0.025)), 
          .Names = c("scen", "name", "ymin", "ymax"), 
          row.names = c(NA, 6L), 
          class = "data.frame")
数据是这样的

dat

这就是结果

theme_new <- theme_set(theme_bw())
p <- ggplot(data=dat) +
  geom_segment(aes(x=ymin,y=scen,xend=ymax,yend=scen),
               arrow=arrow(length=unit(0.3,"cm"),
                           ends='both'),size=1) 

p <- p+  geom_rect(xmin=max(dat$ymin)/2,
                    xmax=min(dat$ymax)/2,
                    ymin=0,
                    ymax=max(dat$scen)+1,
                    alpha=0.2,fill='grey')

p <- p + geom_text(aes(x=(ymin+ymax)/2,
                       y=scen+0.2,label =name),size=6)

p<- p + coord_cartesian(ylim=c(0,max(dat$scen)+3))+
  xlab(expression(P[1]-P[0]))+
  theme( 
    axis.ticks = element_blank(),
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_text(face="bold",  size=20))

p <- p + geom_vline(linetype   ='dashed',
                    xintercept = mid.dash)

p <- p + geom_text(aes(x= mid.dash,
                       y = max(dat$scen)+2, 
                       label="Zone of Indifference", 
                       color="NA*"),rotate=180)
p <- p + theme(legend.position = "none")


theme\u new这是agstudy回答的后续内容(请更新他的回答)。评论的时间太长了,也太不一样了,我没有理由编辑他的答案。此外,一些小的东西仍然需要调整,使其看起来像教科书上的数字。下次我做这种事情时,我将尝试使用
tikzDevice
使字体、下标等正常工作。我添加了花括号,但1)我更喜欢没有花括号的图形,2)我无法使
ggsave()
使用正确的字体,括号在PDF中看起来很糟糕(我使用
RStudio
的导出工具将图像保存为PNG)

没有花括号会更漂亮


信用:关于花括号,请参见:

这个问题似乎有点太模糊了。你想用定量数据做点什么吗?否则,您是否要查找
?箭头
?矩形
?还是确认?或者
?binom.test
?如果您尝试过这个问题,并且在过程中遇到了一个特定的问题,那么这个问题会更好。@Aaron:我想您会发现充满活力的答案“在他们潜入并试图将你从折磨着R noob的漩涡中解救出来之前,希望你在R代码中提供一个数据集。我们中的许多人认为为你们完成所有的项目要求太多了。酷问题,等待数据。
[po]
等需要在报价之外。e、 g.
“你好”,beta[0],“再见”
,如果这有助于OP@agstudy这个答案远远超出了——谢谢。我一定能够在我自己的数据集上使用这种方法。努力+10!非常好的回答。更改图层的制作顺序将“修复”alpha问题(如果我正确理解您的意思):在制作“无差别区域”图层后打印箭头图层:第一个
geom\u段()
在第一个
geom\u rect()之后。(我相信你现在知道了,这个答案来自……2012!)@PatrickT谢谢你。是的,我当然知道。如果你想让答案变得更好,可以自由填写以更改答案。
theme_new <- theme_set(theme_bw())
p <- ggplot(data=dat) +
  geom_segment(aes(x=ymin,y=scen,xend=ymax,yend=scen),
               arrow=arrow(length=unit(0.3,"cm"),
                           ends='both'),size=1) 

p <- p+  geom_rect(xmin=max(dat$ymin)/2,
                    xmax=min(dat$ymax)/2,
                    ymin=0,
                    ymax=max(dat$scen)+1,
                    alpha=0.2,fill='grey')

p <- p + geom_text(aes(x=(ymin+ymax)/2,
                       y=scen+0.2,label =name),size=6)

p<- p + coord_cartesian(ylim=c(0,max(dat$scen)+3))+
  xlab(expression(P[1]-P[0]))+
  theme( 
    axis.ticks = element_blank(),
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_text(face="bold",  size=20))

p <- p + geom_vline(linetype   ='dashed',
                    xintercept = mid.dash)

p <- p + geom_text(aes(x= mid.dash,
                       y = max(dat$scen)+2, 
                       label="Zone of Indifference", 
                       color="NA*"),rotate=180)
p <- p + theme(legend.position = "none")
## Confidence interval segments with ggplot 
# https://stackoverflow.com/questions/13773806/

# Data
d = structure(list(index = 1:6, name = c("I", "II", "III", "IV", 
"V", "VI"), xmin = c(0.06, -0.102, 0.487, 0.116, -0.436, 0.121
), xmax = c(-0.231, 0.135, 0.117, 0.338, -0.347, -0.055)), .Names = c("index", 
"name", "xmin", "xmax"), row.names = c(NA, 6L), class = "data.frame")

# Plot Data
x1 = -0.5              # lower limit of x-axis
x2 = +0.5              # upper limit of x-axis
y1 = -0.5              # lower limit of y-axis
y2 = max(d$index)+2    # upper limit of y-axis
z0 = 0                 # center of indifference zone
z1 = -0.25             # lower limit of indifference zone
z2 = +0.25             # upper limit of indifference zone
z3 = min(d$index)-0.5  # lower limit of indifference zone
z4 = max(d$index)+0.5  # lower limit of indifference zone

df = cbind(d, data.frame(z0 = z0, z1 = z1, z2 = z2, x1 = x1, x2 = x2, z3 = z3, z4 = z4)) 

# Plot confidence intervals
library(ggplot2) 
ggplot(data = df) +
    # extend the y-axis limits to make room for label
    coord_cartesian(xlim = c(x1, x2), ylim = c(y1, y2)) +
    # zone of indifference layer:
    geom_rect(aes(xmin = z1, xmax = z2, ymin = z3, ymax = z4),
        alpha = 0.5,
        fill = "grey") +
    # vertical line to indicate the true mean
    geom_segment(aes(x = z0, xend = z0, y = z3, yend = z4), linetype = 2) + 
    # confidence intervals with arrows: 
    geom_segment(aes(x = xmin, xend = xmax, y = index, yend = index),
        arrow = arrow(length = unit(0.3, "cm"), ends = "both"), size = 1) +
    # labels above the confidence intervals: 
    geom_text(aes(x = (xmin+xmax)/2, y = index+0.2, label = name), size = 4) +
    # make curly bracket - tweak vjust, hjust and size manually to center it!
    geom_text(aes(x = z0, y = z4, label = "{"), angle = 270, vjust = 0.38, hjust = 1, size = 80, fontface = "bold", family = 'Helvetica Neue UltraLight') + 
    # label above the indifference layer
    annotate("text", x = z0, y = y2, hjust = 0.5, vjust = 1,
        label = "Zone of Indifference\n", fontface = "bold", size = 5) +
    # label below the indifference layer 
    annotate("text", x = z0, y = y1, hjust = 0.5, 
        label = "P[1]-P[0]", parse = TRUE, # bold.italic does not survive parse=TRUE
        family = "Times", fontface = "bold.italic", size = 5) + 
    # tweak x-axis and ticks | get rid of default axis, ticks, labels, etc.
    scale_x_continuous(breaks = c(x1, x2), 
                    labels = c("-x", "+x")) +  # set breaks + labels
    #theme_void() +  # should have worked, but...
    theme(panel.background = element_rect(fill = NA, colour = NA)) +
    # insert tweaked horizontal x-axis + ticks
    theme(panel.grid = element_blank()) + 
    theme(panel.border = element_blank()) + # may want to keep
    theme(axis.line.y = element_blank()) +
    theme(axis.title.y = element_blank()) +
    theme(axis.ticks.y = element_blank()) +
    theme(axis.text.y = element_blank()) +
    theme(axis.line.x = element_line(size = 1, linetype = "solid")) +
    theme(axis.title.x = element_blank()) +
    theme(axis.ticks.x = element_line(size = 1)) +
    theme(axis.ticks.length = unit(.3, "cm")) +
    theme(axis.text.x = element_text(size = 14)) 

ggsave("ggplot-ci-indifference.pdf", device = cairo_pdf)
## the curly-brace does not print properly!