R中图形的折线图?
Line graph for graphic in R?
我在 R 中有一个 csv,其中包含以下 table:
df1 <- data.frame(
"Pool 1" = c("F1a", "R1a", "F2a", "R2a"),
"Start Pool 1" = c("10", "20", "25", "35"),
"Pool 2" = c("F1b", "R1b", "F2b", "R2b"),
"Start Pool 2" = c("15", "21", "23", "35"),
stringsAsFactors = F,check.names=FALSE)
Pool 1
Start Pool 1
Pool 2
Start Pool 2
F1a
10
F1b
15
R1a
20
R1b
21
F2a
25
F2b
23
R2a
35
R2b
35
我想在 R 中创建一个如下所示的图形:
你给 Pool 2 的标签好像有误,应该是 R1b
如果您不想使用任何包,并且只有 2 行,您可以通过使用 plot(NULL...)
指定一个空白图来慢慢构建它,然后您将在 [=13 上放置一行=], 另一个在 y=2
上并添加点, 基于这些坐标的文本:
plot(NULL,xlim=c(10,35),ylim=c(0,3),xaxt="n",yaxt="n",
bty="n",xlab="",ylab="")
abline(h=1:2,col="#c0fefc")
points(x = df1[,"Start Pool 1"], y = rep(2,nrow(df1)),
pch=20,cex=2,col="#77acf1")
text(x = df1[,"Start Pool 1"], y = rep(2.5,nrow(df1)),
labels = df1[,"Pool 1"])
points(x = df1[,"Start Pool 2"], y = rep(1,nrow(df1)),
pch=20,cex=2,col="#77acf1")
text(x = df1[,"Start Pool 2"], y = rep(1.5,nrow(df1)),
labels = df1[,"Pool 2"])
axis(side=2,at=1:2,labels=c("Pool2","Pool1"),las=2,col=NA,col.ticks = 1)
否则使用 ggplot2
:
df = data.frame(
label = c(df1[,"Pool 1"],df1[,"Pool 2"]),
value = c(df1[,"Start Pool 1"],df1[,"Start Pool 2"]),
pool = rep(c("Pool1","Pool2"),each=nrow(df1))
)
ggplot(df,aes(x=value,y=pool,label=label)) +
geom_point(size=4,col="#77acf1") +
geom_text(nudge_y=0.2) +
theme_minimal() +
xlab("") + ylab("") +
theme(panel.grid.major.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
我在 R 中有一个 csv,其中包含以下 table:
df1 <- data.frame(
"Pool 1" = c("F1a", "R1a", "F2a", "R2a"),
"Start Pool 1" = c("10", "20", "25", "35"),
"Pool 2" = c("F1b", "R1b", "F2b", "R2b"),
"Start Pool 2" = c("15", "21", "23", "35"),
stringsAsFactors = F,check.names=FALSE)
Pool 1 | Start Pool 1 | Pool 2 | Start Pool 2 |
---|---|---|---|
F1a | 10 | F1b | 15 |
R1a | 20 | R1b | 21 |
F2a | 25 | F2b | 23 |
R2a | 35 | R2b | 35 |
我想在 R 中创建一个如下所示的图形:
你给 Pool 2 的标签好像有误,应该是 R1b
如果您不想使用任何包,并且只有 2 行,您可以通过使用 plot(NULL...)
指定一个空白图来慢慢构建它,然后您将在 [=13 上放置一行=], 另一个在 y=2
上并添加点, 基于这些坐标的文本:
plot(NULL,xlim=c(10,35),ylim=c(0,3),xaxt="n",yaxt="n",
bty="n",xlab="",ylab="")
abline(h=1:2,col="#c0fefc")
points(x = df1[,"Start Pool 1"], y = rep(2,nrow(df1)),
pch=20,cex=2,col="#77acf1")
text(x = df1[,"Start Pool 1"], y = rep(2.5,nrow(df1)),
labels = df1[,"Pool 1"])
points(x = df1[,"Start Pool 2"], y = rep(1,nrow(df1)),
pch=20,cex=2,col="#77acf1")
text(x = df1[,"Start Pool 2"], y = rep(1.5,nrow(df1)),
labels = df1[,"Pool 2"])
axis(side=2,at=1:2,labels=c("Pool2","Pool1"),las=2,col=NA,col.ticks = 1)
否则使用 ggplot2
:
df = data.frame(
label = c(df1[,"Pool 1"],df1[,"Pool 2"]),
value = c(df1[,"Start Pool 1"],df1[,"Start Pool 2"]),
pool = rep(c("Pool1","Pool2"),each=nrow(df1))
)
ggplot(df,aes(x=value,y=pool,label=label)) +
geom_point(size=4,col="#77acf1") +
geom_text(nudge_y=0.2) +
theme_minimal() +
xlab("") + ylab("") +
theme(panel.grid.major.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())