简体   繁体   English

使用 ggplot 重现 plot

[英]Reproduce a plot using ggplot

I am trying to reproduce a plot using ggplot.我正在尝试使用 ggplot 重现 plot。

The code I got from the textbook:我从教科书中得到的代码:

skeptic<-c(1,1.171,1.4,1.8,2.2,2.6,3,3.4,3.8,3.934,4.2,
4.6,5,5.4,5.8,6.2,6.6,7,7.4,7.8,8.2,8.6,9)

effect<-c(-.361,-.327,-.281,-.200,-.120,-.039,.041,.122,.202,.229,.282,
.363,.443,.524,.604,.685,.765,.846,.926,1.007,1.087,1.168,1.248)

llci<-c(-.702,-.654,-.589,-.481,-.376,-.276,-.184,-.099,-.024,0,.044,.105,
.161,.212,.261,.307,.351,.394,.436,.477,.518,.558,.597)

ulci<-c(-.021,0,.028,.080,.136,.197,.266,.343,.428,.458,.521,.621,.726,.836,
.948,1.063,1.180,1.298,1.417,1.537,1.657,1.778,1.899)

plot(x=skeptic,y=effect,type="l",pch=19,ylim=c(-1,1.5),xlim=c(1,6),lwd=3,
ylab="Conditional effect of disaster frame",
xlab="Climate Change Skepticism (W)")
points(skeptic,llci,lwd=2,lty=2,type="l")
points(skeptic,ulci,lwd=2,lty=2,type="l")
abline(h=0, untf=FALSE,lty=3,lwd=1)
abline(v=1.171,untf=FALSE,lty=3,lwd=1)
abline(v=3.934,untf=FALSE,lty=3,lwd=1)
text(1.171,-1,"1.171",cex=0.8)
text(3.934,-1,"3.934",cex=0.8)

The exemplary plot is示例性 plot 是在此处输入图像描述

I have tried ggplot but I am struggling with the vertical and horizontal dashed line.我试过 ggplot 但我在垂直和水平虚线上苦苦挣扎。 Could anybody reproduce the plot using ggplot?有人可以使用 ggplot 重现 plot 吗? And I have a follow-up question.我有一个后续问题。 How can I mark the area of x < 3.934 and x > 1.171?如何标记 x < 3.934 和 x > 1.171 的面积? Thank you!谢谢!

Constructing on your specific question (horizontal and vertical lines and area) as you said you got already the remaining parts right.正如您所说,根据您的特定问题(水平和垂直线和区域)构建您已经得到了其余部分。

Use geom_hline for horizontal line and geom_vline for vertical one.geom_hline用于水平线, geom_vline用于垂直线。 linetype="dashed" will render dashed lines. linetype="dashed"将呈现虚线。 As you didn't tell how you want the area rendered, here is my guess, a vertical grayed area extending horizontally from abcissa of your vertical lines and vertically from min effect to max effect ( Inf values) drawn using a geom_rect .由于您没有告诉您希望如何渲染该区域,因此我的猜测是,垂直灰色区域从垂直线的横坐标水平延伸,并从使用geom_rect绘制的最小效果到最大效果( Inf值)垂直延伸。

ggplot(data.frame(skeptic,effect))+
  geom_line(aes(skeptic,effect))+
  geom_rect(aes(xmin=1.171,xmax=3.934,ymin=-Inf,ymax=Inf),fill="lightgray")+
  geom_hline(yintercept=0,linetype="dashed") +
  geom_vline(xintercept=c(1.171,3.934),linetype="dashed")

Here is a way to reproduce the posted graph.这是一种重现已发布图表的方法。

library(ggplot2)
library(magrittr)
library(tidyr)

df1 <- data.frame(skeptic, effect, llci, ulci)
vlines <- data.frame(x = c(0, 1.171, 3.934))
vertices <- data.frame(xmin = 1.171, xmax = 3.934,
                        ymin = -Inf, ymax = Inf)

brks <- names(df1)[-1]

df1 %>%
  pivot_longer(-skeptic, names_to = "line") %>%
  ggplot(aes(skeptic, value)) +
  geom_rect(data = vertices,
            mapping = aes(xmin = xmin, xmax = xmax, 
                          ymin = ymin, ymax = ymax),
            fill = "blue", alpha = 0.2,
            inherit.aes = FALSE) +
  geom_line(aes(size = line, linetype = line)) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  geom_vline(data = vlines, 
             mapping = aes(xintercept = x), 
             linetype = "dotted") +
  geom_text(data = subset(vlines, x != 0),
            mapping = aes(x = x, label = x), 
            y = -0.75,
            hjust = 0, vjust = 1) +
  scale_size_manual(breaks = brks, values = c(1, 0.5, 0.5)) +
  scale_linetype_manual(breaks = brks, values = c("solid", "dashed", "dashed")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM