我画了一张带有置信区间的线图,如下所示:
下面是我使用的代码:
Data_prob = read.table("group1.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
p<-ggplot(Data_prob, aes(x=Trial, y=Data)) + theme_bw() + xlim(1, 49) + ylim(0.3, .95) +
theme(plot.title = element_text(color="black", size=14, face="bold.italic"), # plot title
axis.title.x = element_text(color="Black", size=25), # x label
axis.title.y = element_text(color="Black", size=25), # y label
axis.text.x = element_text(color = "black",face = "bold", # text style for x axis (removed face = "bold")
size = 22, angle = 0),
axis.text.y = element_text( color = "black",face = "bold", # text style for y axis
size = 22, angle = 0),
axis.line = element_line(color = "black", # line for x and y axis
size = 1, linetype = "solid"),
plot.caption = element_text(color = "black", size =11, face = "bold.italic"), # text style for caption
axis.ticks.length=unit(.25, "cm")
#axis.ticks = element_blank()
) +
theme(panel.border = element_blank(), # remove grid and background color
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
geom_ribbon(aes(ymin=Data-CI, ymax=Data+CI),
alpha=0.2, fill = "gold",color = "gray35", size = 0.1) +
geom_line(size=2, color="black")
print(p)
# Change axis labels
p <- p + labs(x = "Trial", y = "Go probability")
p
下面是数据的样子:
structure(list(Trial = 1:20, Data = c(0.500000027777778, 0.529220307827301,
0.519090892380421, 0.532167908861151, 0.598987738691911, 0.631452312399153,
0.669892859606973, 0.649994605133695, 0.675688232680895, 0.708304011267941,
0.720449809771278, 0.748081634160322, 0.761387966998141, 0.784275979741743,
0.786897508201414, 0.794196638795235, 0.785998558171792, 0.766138054176142,
0.790409435001615, 0.778745578955544), CI = c(5.44644605948509e-08,
0.073455696656296, 0.080875149623623, 0.073926617913334, 0.085753364225061,
0.068666346495005, 0.079656617789649, 0.077652237751934, 0.070180261163008,
0.071432599780653, 0.064943477844168, 0.064645277387821, 0.065096219183598,
0.065766579701286, 0.064325292909355, 0.066878706963396, 0.067698913200129,
0.07105300711211, 0.067063389995005, 0.069931673037628)), row.names = c(NA,
20L), class = "data.frame")
我想绘制来自另一组(group2.csv)的数据,因此基本上是同一图上具有置信区间的另一条线,以便直观地比较两组,理想情况下使用不同的线条颜色。我在这里看到了一些例子,但我不能让它工作。有人能帮上忙吗?谢谢!
发布于 2021-10-09 19:22:50
假设您从csv文件中读取的数据多达两个tibble
。
library(tidyverse)
Data_prob1 =tibble(
Trial = 1:50,
Data = c(seq(0.5,0.8, length.out = 20), rep(0.8, 30))+rnorm(50,0,0.05),
CI = 0.1,
)
Data_prob2 =tibble(
Trial = 1:50,
Data = c(seq(0.8,1.2, length.out = 25), rep(1.2, 25))+rnorm(50,0,0.05),
CI = 0.08
)
您可以像这样组合这样的数据
Data_prob = Data_prob1 %>% mutate(probe = "1") %>%
bind_rows(Data_prob2 %>% mutate(probe = "2")) %>%
mutate(probe = probe %>% fct_inorder())
最后,创建一个图表
Data_prob %>% ggplot(aes(Trial, Data, fill=probe, color = probe))+
geom_line()+
geom_ribbon(aes(ymin=Data-CI, ymax=Data+CI, color=NULL), alpha=0.2)
https://stackoverflow.com/questions/69509110
复制相似问题