如何重现置信区间图?

时间:2012-12-08 02:18:30

标签: r graphics confidence-interval

我需要重现类似于附加图像的图形。我希望使用图形来比较比例之间差异的置信区间。如何使用R生成附加图形?任何指向正确方向的人都会受到赞赏。

enter image description here

2 个答案:

答案 0 :(得分:8)

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

这里我尝试使用ggplot2。我仍然对alpha图层有一些问题,但是这个图的主要思想就在这里。

一些数据

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

  scen name   ymin   ymax   y
1    1    I  0.060 -0.231   I
2    2   II -0.102  0.135  II
3    3  III  0.487  0.117 III
4    4   IV  0.116  0.338  IV
5    5    V -0.436 -0.347   V
6    6   VI  0.021 -0.025  VI

这是结果

enter image description here

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")

P

答案 1 :(得分:2)

这是关于agstudy答案的后续行动(请回答他的回答)。评论太长了,太不同了,无法证明我编辑他的答案。还有一些小问题仍需要调整才能看起来像教科书的形象。下次我做这种事情时,我会尝试使用tikzDevice来获取字体,下标等以使其正常工作。我已经添加了大括号,但1)我更喜欢没有它的数字2)我无法使ggsave()使用正确的字体并且PDF中的大括号看起来很糟糕(我将图像保存为PNG与RStudio的导出工具)。

## 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!

enter image description here

没有花括号会更漂亮。

信用:对于花括号,请参阅:How to add braces to a graph?