4

我需要重现类似于所附图像的图表。我希望用这个图形来比较不同比例的置信区间。如何使用 R 生成附加图形?任何正确方向的指针将不胜感激。

在此处输入图像描述

4

2 回答 2

8

如果没有上下文和可重复的示例,很难给出一个好的答案。但我觉得剧情很有趣。

这是我对ggplot2的尝试。我仍然对 alpha layer 有一些问题,但情节的主要思想就在这里。

一些数据

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

数据看起来像这样

数据

  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

这是结果

在此处输入图像描述

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

于 2012-12-08T06:08:18.060 回答
2

这是对 agstudy 答案的跟进(请投票支持他的答案)。评论太长了,太不同了,无法证明我编辑他的答案是合理的。此外,小事情仍然需要调整以看起来像教科书人物。下次我做这种事情时,我会尝试使用tikzDevice来让字体、下标等正常工作。我已经添加了花括号,但是 1)我更喜欢没有它的图,并且 2)我无法使用正确的字体,并且大括号在 PDF 中看起来很糟糕(我使用导出工具ggsave()将图像保存为 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!

在此处输入图像描述

没有花括号会更漂亮

信用:对于花括号,请参阅:如何在图表中添加大括号?

于 2018-03-28T17:51:02.933 回答